In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use Python to draw pictures to commemorate Huang Jiaju". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use Python to draw pictures to commemorate Huang Jiaju" can help you solve the problem.
Train of thought:
Use the Image module and turtle module of PIL to read and redraw the image
Is to read a picture with a portrait, and then through circular comparison, record the pixel coordinates of each pedestrian, and then draw the outline of the portrait through turtle.
The Image module has the function to directly display the outline, and it is too difficult for rookies to identify portraits from the image, so choose black and white or single color images with portraits to recognize.
Step: step to operate the library 1 used to read the image, by traversing each row of pixel records and comparison, get the portrait 'coordinates' PIL2 according to the obtained coordinates, call the turtle library drawing turtle the first step, read the picture, and get the drawing coordinates
Obtaining coordinates is a process of traversing pixels line by line, and it is convenient to record the beginning and end coordinates of non-white (white as the background color) pixel bars in each row of pixels (X1 ~ X2). When comparing, the current pixel is compared with the next pixel of the same line, and if the values of the two pixels are different and one pixel is the background color, the Abscissa of the non-background color is recorded. When recording, the data of the whole picture is temporarily stored in a list, which consists of n lists (n is the number of rows of image pixels), which stores the beginning and end coordinates of all non-background pixel bars of the corresponding rows.
Source code from PIL import Imageimport turtle as tdef get_lst (img_path): imgf = Image.open (img_path) # read image global size print (size:= imgf.size) # get image size / size pix = imgf.load () lst = [[] for i in range (size [1])] # construct empty list for y in range (0 Size [1]): # cycle index = 0 for x in range (0, size [0]-1) from the first line: # cycle every pixel in line y # if the current pixel is different from the next pixel and one of them is the background color Then record coordinates if pix [x, y]! = pix [x, y] and (255,255,255,255) in [pix [x, y], pix [x, y]]: if index = = 0: # index value 0 indicates that it is the starting coordinate of pixel bar LST [y] .append ([x = 1]) ]) index + = 1 else: # index value of 1 means recording the pixel bar end coordinate LST [y] [- 1] .append (x) index = 0 return lst second step Draw a picture
After getting the beginning and end coordinates of each pixel bar, you can draw the picture line by line.
Source code def paint (lst): fontc = 'whitesmoke' # right word color manc =' black' # 'dimgrey' portrait color t.setup (width=size [0] + 40) Height=size [1]) # drawing window size t.screensize (bg='oldlace') # canvas background color t.bgpic (rascc:\\ users\\ pxo\\ desktop\\ bg.png') # canvas background image t.speed (333) # drawing speed is said to range [1m 10] for y in range (0 Size [1]): # iterate through each line t.pencolor (manc) for line in lst [y]: # traverse each pixel bar if line [0] > 364 and 144
< y < 495: # 这个是判断是否是右边的字 t.pencolor(fontc) # 下面是画像素条 t.penup() t.goto(line[0]-size[0]//2, (size[1]-y)-size[1]//2) t.pendown() t.goto(line[1]-size[0]//2, (size[1]-y)-size[1]//2) t.mainloop()开始:if __name__ == '__main__': img_path = r'c:\\users\\pxo\\desktop\\jh.png' # 图像地址 lst = get_lst(img_path) paint(lst) 这样就实现了使用 Python Turtle+PIL 绘制黄家驹效果。 随机线条实现方式:from PIL import Imageimport turtle as tfrom random import choicedef get_lst(img_path): imgf = Image.open(img_path) global size print(size:= imgf.size) pix = imgf.load() lst = [[] for i in range(size[1])] for y in range(0, size[1]): index = 0 for x in range(0, size[0]-1): if pix[x, y] != pix[x+1, y] and (255, 255, 255, 255) in [pix[x, y], pix[x+1, y]]: if index == 0: lst[y].append([y, x+1, ]) # 这里在每一个像素条的数据中加入了其纵坐标y index += 1 else: lst[y][-1].append(x) index = 0 # 这里也改了,返回的数据列表的子元素是所有单独的像素条,而不是某一行像素条的总和 lt = [] for i in lst: for j in i: if j: lt.append(j) return ltdef paint(lst): fontc = 'darkorange' manc = 'black' #'dimgrey' t.setup(width=size[0]+40, height=size[1]+40) t.screensize(bg='oldlace') #t.bgpic(r'c:\\users\\pxo\\desktop\\bg.png') t.speed(330) cnt = len(lst) lt = [i for i in range(cnt)] # 弄一个种子列表,理解为存取位置 flst = [] for i in range(cnt): del lt[lt.index(index:=choice(lt))] #获取并删除一个随机位置 line = lst[index] # 从所有线条中得到一条随机线条(像素条) y = line[0] x1, x2 = line[1], line[2] t.pencolor(manc) if x1 >Flst.append (line) continue t.penup () t.goto (x1-size [0] / 2, (size [1]-y)-size [1] / / 2) t.pendown () t.goto (x2-size [0] / / 2, (size [1]-y)-size [1] / / 2) new_flst = sorted (flst [:-80] Reverse=True) for line in new_flst: y = line [0] x1, x2 = line [1], line [2] t.pencolor (fontc) t.penup () t.goto (x1-size [0] / / 2, (size [1]-y)-size [1] / / 2) t.pendown () t.goto (x2-size [0] / / 2 (size [1]-y)-size [1] / / 2) t.hideturtle () t.mainloop () if _ _ name__ ='_ _ main__': img_path = Renewc:\\ users\\ pxo\\ desktop\\ jh.png' lst = get_lst (img_path) paint (lst) on "how to use Python to draw in memory of Wong Ka Kui" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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: 278
*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.