In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use Python to draw a group of hand drawings, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
These days, many cities have ushered in the first snow in 2019.
On the morning of the 13th, when Beijing residents opened the curtains, they found that snowflakes were falling in the air outside the window.
And it gets bigger and bigger, on the trees, on the grass, on the roof, on the road. All covered with snowflakes.
Beijing is dressed in white, which is the second snowfall in Beijing since this winter
As soon as it snows, Beijing becomes Peiping and the Forbidden City becomes the Forbidden City.
Eighty thousand tickets were booked long before the snowflakes fell.
Looking at the moments and Weibo friends are posting pictures one after another, the editor can only envy.
However, it suddenly occurred to Python that the pictures of the buildings of the Imperial Palace can be converted into hand drawings (sketch effects) through Python. The effect picture is as follows:
I. concept and principle
We all know that the main characteristics of hand drawing are:
Black, white and gray; heavy boundary lines; the same or similar colors tend to be white; slightly light source effect
Core principle: use the gradient value and virtual depth value between pixels to reconstruct the image, and simulate the simulation degree of human vision according to the change of grayscale.
The image is regarded as a two-dimensional discrete function, and the gray gradient is actually the derivative of the two-dimensional discrete function. The difference is used instead of the differential to obtain the gray gradient of the image. Some commonly used grayscale gradient templates are: Roberts gradient, Sobel gradient, Prewitt gradient, Laplacian gradient.
Explained by Sobel gradient calculation:
First of all, calculate
、
And then calculate the gradient angle
The gradient direction and the increasing direction of image grayscale, in which the gradient angle in the gradient direction is larger than that in the flat region. As shown in the following figure, the direction gradient angle in which the gray value increases is large, and the direction in which the gradient angle is large is the gradient direction. Corresponding to finding the gradient direction of a point in the image, that is, by calculating the gradient angle between the point and its 8 neighborhood points, the maximum gradient angle is the gradient direction.
2. Array form and transformation of images
Among them, the methods that need to be used:
Image.open (): open the picture
Np.array (): converts an image to an array
Convert ("L"): converts a picture into a two-dimensional grayscale picture
Image.fromarray (): restore the array to the image uint8 format
The code is as follows:
From PIL import Image
Import numpy as np
Im = Image.open (r "C:\ Users\ Administrator\ Desktop\ gugong\ Wechat picture _ 20190216152248.jpg") .convert ('L')
A=np.asarray (im) .astype ('float')
Print (a.shape.dtype)
(1080, 608) float64
# (1080) denotes height and width respectively
Third, the hand-drawn effect processing of the image.
The steps to realize the idea:
1. Reconstruction of gradient.
Introduction of gradient function of numpy
Np.gradient (a): calculates the gradient of elements in the array a, and returns the gradient of each dimension when f is multidimensional
Discrete gradient: the y-axis values corresponding to three consecutive x-axis coordinates of the xy axis: a, b, c, where the gradient of b is (Cmura) / 2
And the gradient of c is: (cmurb) / 1
When it is a two-dimensional array, np.gradient (a) produces two arrays, the first array corresponding to the gradient of the outermost dimension and the second array corresponding to the gradient of the second dimension.
The code is as follows:
Grad=np.gradient (a)
Grad_x,grad_y=grad
Grad_x = grad_x * depth / 100.# normalizes the grad_ x value
Grad_y = grad_y * depth / 100.# normalizes the grad_ y value
2. Construct guan light source effect.
Design a virtual light source located at the top of the oblique image
The viewing angle of the light source relative to the image is Elevation, and the azimuth is Azimuth.
Establish the influence function of light source on the gradient value of each point.
Calculate the new pixel values of each point
Where:
Np.cos (evc.el): the projection length of unit light on the ground plane
Dx,dy,dz: the degree of influence of light source on the three directions of x ~ (th) and y ~ (th)
3. Gradient normalization
Constructing three-dimensional normalized unit coordinate system of x-axis and y-axis gradient
The gradient interacts with the light source to convert the gradient into grayscale.
4. Image generation
The code for details is as follows:
From PIL import Image
Import numpy as np
Import os
Import join
Import time
Def image (sta,end,depths=10):
A = np.asarray (Image.open (sta). Convert ('L')) .astype ('float')
Depth = depths # depth range (0-100), standard is 10
Grad = np.gradient (a) # take the grayscale value of the image
Grad_x, grad_y = grad # take the gradient values of horizontal and vertical images respectively
Grad_x = grad_x * depth / 100.# normalizes the grad_ x value
Grad_y = grad_y * depth / 100.# normalizes the grad_ y value
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 # the overlooking angle, arc value of the light source
Vec_az = np.pi / 4. # Azimuth angle, arc value of the light source
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) # normalization of light source
B = b.clip (0,255)
Im = Image.fromarray (b.astype ('uint8')) # reconstructed image
Im.save (end)
Def main ():
Xs=10
Start_time = time.clock ()
Startss = os.listdir (r "C:\ Users\ Administrator\ Desktop\ gugong")
Time.sleep (2)
For starts in startss:
Start = '.join (starts)
Sta = 'CjouxamUsersActuator' + start
End = 'Cjoux HD_' administrators' + 'gugong start'
Image (sta=sta,end=end,depths=xs)
End_time = time.clock ()
Print ('the program runs -' + str (end_time-start_time) + 'seconds')
Time.sleep (3)
Main ()
The program runs-43.01828205879955 seconds # A total of 35 pictures
Final effect picture comparison:
Other pictures are not listed one by one.
After reading the above, do you have any further understanding of how to draw a group of hand drawings with Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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: 276
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.