This series is intended to document some interesting programs and summarise them.
Source of the problem:
https://github.com/Yixiaohan/show-me-the-code
https://github.com/HT524/500LineorLess_CN
Today, this program adds numbers to a picture, similar to the little red dots on the QQ avatar, but this is static.
The first image library used is pillow.
The general idea is to open the image through Image.open(), set the format of the information to be drawn, ImageDraw.Draw() to generate the modified instance, and then modify it through the text() method.
The procedure is as follows:
1 from PIL import Image, ImageDraw, ImageFont 2 3 4 def pic_add_num(image): 5 my_font = ImageFont.truetype(r"C:\windows\Fonts\simsun.ttc", size=40) 6 color = "red" 7 width, height = image.size 8 position = (width-40, 0) 9 draw = ImageDraw.Draw(image) 10 11 draw.text(position, "99", font=my_font, fill=color) 12 image.save("add_num.jpg") 13 14 if __name__ == "__main__": 15 img = Image.open("universe.jpg") 16 pic_add_num(img)