Python exercise book, a small program every day-question 0,000th, python0000
Question
Question 0,000th: Add a red number to the upper right corner of your QQ profile picture (or Weibo profile picture), which is similar to the number of unread messages. Similar to the figure
Code:
1 from PIL import Image, ImageDraw, ImageFont 2 # path = "F: \ Python Learning \ Python_Exercise "3 # reading Image data 4 iconPath = r" Image \ WeChart.png "5 # labelPath = r" Image \ circle .png "6 iconImage = Image. open (iconPath, 'R') 7 # labelImage = Image. open (labelPath, "r") 8 9 # Write 10 on the tag # user_font = ImageFont. truetype ('simsun. ttc ', 80) 11 # draw = ImageDraw. draw (labelImage) 12 # draw. text (int (labelImage. size [0]/4), int (labelImage. size [1]/7), u '24', font = user_font) 13 # labelImage. save ("final.png", "PNG") 14 15 # combined Icon16 icon = Image. new ("RGBA", (iconImage. size [0] + 105, iconImage. size [1] + 105) 17 # iconImage. paste (labelImage, (int (iconImage. size [0]-labelImage. size [0]/2-1), 0) 18 # iconImage. save ("final.png", "PNG") 19 # The number label in the upper right corner will increase the image size by 20 icon. paste (iconImage, (0, 54) 21 draw = ImageDraw. draw (icon) 22 # draw a circle 23 Draw directly on the image. ellipse ([(iconImage. size [0]-100,5), (iconImage. size [1] + 100,205)], "red") 24 user_font = ImageFont. truetype ('simsun. ttc ', 100) 25 # mark the number 26 draw. text (iconImage. size [0]-50, 50), u '24', "white", font = user_font) 27 icon. save ("final.png", "PNG ")
Result:
Summary:
Pillow API Learning
API document path: http://pillow-cn.readthedocs.io/zh_CN/latest/reference/index.html
(1) PIL. Image. new (mode, size, color)
Mode: the mode of the new image, such as RGB. The grayscale image is equal.
Size: the size of the new image. The object (a, B)
Color: fill object of the image
Return image object
(2) Image. size
Obtain the image size. The returned object is a tuples.
The returned result is Image. size [0].
(3) ImageDraw. Draw (im, mode = None)
Create a Draw object that can be drawn on an image
(4) Image. paste (sourceImg, box = None, mode = None)
Paste an image to another image
The Image object refers to the pasted Image.
SourceImg: pasted source Image
Box: 2 tuples indicates the starting position in the upper left corner of the position of the pasted image.
The 4-tuples are in the upper left corner, and the lower right corner must be within the size of the source image.
Mode: image mode
(5) ImageFont. trueType (font = None, size = None, index = 0, filename = None)
Set the font of the image
Filename: Specifies the font file. If not, the font of the Window is used.
Size: font size
(6) ImageDraw. Draw. eclipse (xy, fill = None, outline = 0)
Draw an ellipse on the Image
Xy: Two Methods
[(X0, y0), (x1, y1)], [x0, y0, x1, y1] upper left corner, coordinate in the upper right corner
To draw a circle, ensure that the length of the two axes is equal.
Fill: whether to fill in the interior of the elliptical shape. fill color
Outline: elliptical contour width
(7) ImageDraw. text (xy, text, fill = None, font = None)
Xy: the position in the upper-right corner of the word
Text: writing problems. Pay attention to the encoding format.
Fill: text color
Font: introducing Fonts
Summary of coding experience
(1) If it is not a vector image, it may cause the superimposed image to be unmasked. Therefore, the two images will be superimposed, but the circle will be drawn directly on the image.
(2) We still need to learn more about the image font, because I want to introduce, and the result is not successful. How can I bold it?
(3) how to make the text adaptive in the middle of the image, C ++ has to get the pixel size of the font, and whether Pillow has this function?
Expansion
(1) how to erase the preceding numeric tag and simulate read
(2) how to update the above numbers, simulate continuous acceptance of new messages, unread
Ideas:
Code adapted to function, input parameter is unread message data, get the paint brush of the original image
Input parameters in the Event Response