[Share Python code] Convert pictures into sketches

Hello, everyone, I am Mu Yixiang~

Code generation renderings

Original picture:

picture

Generate graph:

picture

Original picture:

picture

Generate graph:

picture

Preparation

Python programming first needs to install the environment. The following are the detailed steps:

Friends who know it can skip it by themselves, the code is at the end.

1. Install Python tutorial

Step 1. Double-click to open the Python.exe file, and the following interface will appear.

picture

Note: Check the √ Add Python 3.9 PATH option

This option will automatically add a variable environment for Python to the computer (must be selected)

Step 2. Select Customize installation (Cutormize installation)

picture

Step 3: Check the default content, no need to change it, press the Next button

picture

 Step 4. Check the Install for all users option

picture

Browse is to select the installation path. You can customize the installation path to D drive or C drive.

Step 5. After selecting, you can press the - button (Install) to install.

picture

Check whether Python is installed normally and successfully

1. Open CMD (Ctrl+R keys), enter cmd and then enter Python in the terminal

picture

2. When the following content appears, the installation is successful.

picture

3. Enter exit() after >>> to exit.

4. Then enter pip show pip in the terminal to check whether pip is installed successfully.

picture

5. When the above picture style appears, the installation is successful! congratulations! Python installed successfully!

2. Pycharm usage tutorial

Preparation:

Download and install the Python interpreter and Python development tool Pycharm.

When you are ready, start writing your first python program using Pycharm.

Step 1. Open Pycharm, select Create New Project, and create a new project.

picture

Step 2. Set the Python project storage directory, select the interpreter version to be used, and click Create.

picture

Step 3. Right-click the project, select New, and then select Python File to create a Python file.

picture

Step 4. After clicking Python File, enter the Python file name in the pop-up box and press Enter to create the Python file successfully.

picture

Step 5. After displaying 01-First Python Program.py, the file is created successfully. After entering the code in the blank area on the right, right-click Run to run the program.

picture

Step 6. After running successfully, the Pycharm Console window will display our output results.

picture

Successful operation means that there are no problems with the environment and configuration!

from PIL import Image
import numpy as np
 
a = np.asarray(Image.open(r".\原图.jpg").convert('L')).astype('float')
 
depth = 10.                        # (0-100)
grad = np.gradient(a)              #取图像灰度的梯度值
grad_x, grad_y =grad               #分别取横纵图像梯度值
grad_x = grad_x*depth/100.
grad_y = grad_y*depth/100.
A = np.sqrt(grad_x**2 + grad_y**2 + 1.)
uni_x = grad_x/A
uni_y = grad_y/A
uni_z = 1./A
 
vec_el = np.pi/2.2                   # 光源的俯视角度,弧度值
vec_az = np.pi/4.                    # 光源的方位角度,弧度值
dx = np.cos(vec_el)*np.cos(vec_az)   #光源对x 轴的影响
dy = np.cos(vec_el)*np.sin(vec_az)   #光源对y 轴的影响
dz = np.sin(vec_el)                  #光源对z 轴的影响
 
b = 255*(dx*uni_x + dy*uni_y + dz*uni_z)     #光源归一化
b = b.clip(0,255)
 
im = Image.fromarray(b.astype('uint8'))      #重构图像
im.save(r".\手绘.jpg")
print("保存成功,请查看")

Look at the code carefully! ! Pay attention to the picture naming and the path! !

--END--

Guess you like

Origin blog.csdn.net/qq_44794321/article/details/133884275