Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert pictures into hand-drawn by using Numpy and PIL libraries in Python programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you Python programming how to use Numpy and PIL library to convert pictures into hand-drawn, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The main technical points adopted

Python + Numpy + PIL

Before the start of the text code, let's take a look at the original picture and the comparison before and after the conversion of the hand-drawn picture.

Of course, I first checked the three basic features of hand painting:

The picture can be a single channel grayscale image.

Heavy edge lines can be regarded as black, and the values of the same or similar pixels tend to be white.

Under the effect of light source, the grayscale change is similar to the distance of human vision.

Let's start with the implementation steps of hand-drawn photos:

Read pictures and convert them to an array

Because you want to calculate the pixels of an image, you can first convert the picture into an array. The code is as follows:

Depth = 10. # (0-100) grad = np.gradient (a) # take the gradients of image grayscale grad_x, grad_y = grad # take the gradient values of horizontal and vertical images grad_x = grad_x * depth / 100.grad_y = grad_y * depth / 100. Calculate the gradient value of x ~ (th) y ~ (th) z axis and normalize it.

The photo pays more attention to the edge region, and calculating the gradient is the most effective way to locate the edge of the picture. The grayscale change is used to simulate the effect of the far and near of the picture. Depth represents the preset depth, and the default gradient of the z axis is 1.

Depth = 10. # (0-100) grad = np.gradient (a) # take the gradients of image grayscale grad_x, grad_y = grad # take the gradient values of horizontal and vertical images grad_x = grad_x * depth / 100.grad_y = grad_y * depth / 100.

Normalize the gradient value

A = np.sqrt (grad_x * * 2 + grad_y * * 2 + 1.) uni_x = grad_x / Auni_y = grad_y / Auni_z = 1. / An add light source effect

According to the different incident angle of the light source, it has different influence on the gradient value on each axis of XMagneyMagnez. A simulated light source is added and placed on the top of the slope, which forms two angles with the xQuery y respectively. Finally, the new pixel value is calculated by using the sine and cosine function.

Vec_el = np.pi / 2.2 # overlooking angle of the light source, arc value vec_az = np.pi / 4. # azimuth angle of the light source Radian value dx = np.cos (vec_el) * np.cos (vec_az) # influence of light source on x-axis dy = np.cos (vec_el) * np.sin (vec_az) # influence of light source on y-axis dz = np.sin (vec_el) # influence of light source on z-axis b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # light source normalized, 8 255b = b.clip (0 255) # pairs of pixel values less than 0 Truncate and export the picture and save the im.save ("man_shouhui.jpg")

Use Python to convert a picture into a hand-painted style, so easily done!

These are all the contents of the article "how Python programming uses Numpy and PIL libraries to convert pictures into hand-drawn". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report