├── .gitignore ├── README.md ├── image.py ├── tupian.jpg ├── tupian1.jpg ├── tupian2.jpg ├── tupian3.jpg ├── tupianHD.jpg ├── tupianHD1.jpg ├── tupianHD2.jpg └── tupianHD3.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 用Python实现图像的手绘化 2 | 3 | * 对于不同的图片可以通过修改深度值depth,光照的俯视角el和方位角az以获得最好的效果 4 | 5 | * 效果示例1: 6 | 7 | 8 | * 效果示例2: 9 | 10 | 11 | 12 | * 效果示例3: 13 | 14 | 15 | * 效果示例4: 16 | 17 | 18 | -------------------------------------------------------------------------------- /image.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import numpy as np 3 | 4 | L = np.asarray(Image.open(r'F:\python\Image-Freehand\tupian.jpg').convert('L')).astype('float') #取得图像灰度 5 | 6 | depth = 10. # (0-100) 7 | grad = np.gradient(L) # 取图像灰度的梯度值 8 | grad_x, grad_y = grad # 分别取横纵图像梯度值 9 | grad_x = grad_x*depth/100. 10 | grad_y = grad_y*depth/100. 11 | A = np.sqrt(grad_x**2 + grad_y**2 + 1.) 12 | uni_x = grad_x/A 13 | uni_y = grad_y/A 14 | uni_z = 1./A 15 | 16 | el = np.pi/2.2 # 光源的俯视角度,弧度值 17 | az = np.pi/4 # 光源的方位角度,弧度值 18 | dx = np.cos(el)*np.cos(az) # 光源对x轴的影响 19 | dy = np.cos(el)*np.sin(az) # 光源对y轴的影响 20 | dz = np.sin(el) # 光源对z轴的影响 21 | 22 | gd = 255*(dx*uni_x + dy*uni_y + dz*uni_z) # 光源归一化 23 | gd = gd.clip(0,255) #避免数据越界,将生成的灰度值裁剪至0-255之间 24 | 25 | im = Image.fromarray(gd.astype('uint8')) # 重构图像 26 | im.save(r'F:\python\Image-Freehand\tupianHD.jpg') # 保存图像 27 | -------------------------------------------------------------------------------- /tupian.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupian.jpg -------------------------------------------------------------------------------- /tupian1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupian1.jpg -------------------------------------------------------------------------------- /tupian2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupian2.jpg -------------------------------------------------------------------------------- /tupian3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupian3.jpg -------------------------------------------------------------------------------- /tupianHD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupianHD.jpg -------------------------------------------------------------------------------- /tupianHD1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupianHD1.jpg -------------------------------------------------------------------------------- /tupianHD2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupianHD2.jpg -------------------------------------------------------------------------------- /tupianHD3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmliang/Image-Freehand/63168a97374273d5b64fa42409903fb57326167f/tupianHD3.jpg --------------------------------------------------------------------------------