This article is a new version of the New Year's Countdown poster based on the idea of Automating Countdown Images in Python and combining some design elements with the book Automating Python Tedious Work.000. Effect previewCopy the code





001.Image creation RGBA values are a set of numbers represented by red, green, blue, and alpha (transparency)0Not at all255The (highest) integer in Pillow, the RGBA value is represented as a tuple of four integer values. For example, red is (255.0.0.255) This color has the largest red value, no green or blue, and the largest alpha value, which means it's completely opaque. Green: (0.255.0.255) blue :(0.0.255.255White is a combination of colors255.255.255.255Black has no color at all0.0.0.255) Inspiration:2019New Year countdown dynamic poster series Pillow provides the imagecolor.getColor () function, so you don't have to remember the RGBA value of the color you want to use. This function takes a color name string as its first argument, string'RGBA'As the second argument, return an RGBA tuple. from PILimport ImageColor
ImageColor.getcolor('red'.'RGBA') Operating results: (255.0.0.255) Image size and color in this example:1000*2160Color: (174.60.58.255The relevant code is as follows: from PILimport Image, ImageDraw, ImageFont
importOS # creates the Image and sets the size and color im = Image.new('RGBA', (1000.2160), (174.60.58.255))
draw = ImageDraw.Draw(im)
Copy the code




002To set the font and size, we first save the folder name in fontsFolde. Then call imagefont.trueType (), passing in the font we want. TTF file, followed by an integer indicating the font size. Save the Font object returned by Imagefont.trueType () in a variable like arialFont, and pass that variable to Text () as the final keyword argument.Copy the code
Use the font and size used
fontsFolder = 'D:/05.python_code/00.py_projects/new_year_last'
font1 = ImageFont.truetype(os.path.join(fontsFolder, 'wenzangshufang.ttf'), 580)
font2 = ImageFont.truetype(os.path.join(fontsFolder, 'SourceHanSerifCN-SemiBold.otf'), 90)
font3 = ImageFont.truetype(os.path.join(fontsFolder, 'SourceHanSerifCN-SemiBold.otf'), 180)
Copy the code



003The rectangle(xy, fill, outline) argument is a rectangle tuple of the form (left,top, right, bottom). The left and top values specify the x and y coordinates at the top left corner of the rectangle, and the right and bottom values specify the bottom right corner of the rectangle. The optional fill parameter is the color that will fill the interior of the rectangle. The optional outline argument is the color of the rectangle outline.Copy the code
# draw rectangle
left = pos_x_3
top = 1750
right = pos_x_3 + txtSize_3[0]
bottom = 1700 + txtSize_3[1]
draw.rectangle((left, top, right, bottom), fill=(217.217.217.255))
Copy the code



004The ImageDraw object also has the text() method, which is used to draw text on the image. The text () method4A parameter: The xy argument is a tuple of two integers specifying the upper left corner of the text area and the text argument is the string of text that you want to write and the optional argument fill is the color of the text and the optional argument font is an ImageFont object that sets the font and size of the text because it's often hard to know in advance how big a piece of text will be in a given font, So the ImageDraw module also provides the textSize () method. The textSize () method returns a two-integer tuple representing the width and height of the text if the image is written in the specified font.Copy the code
# calculate the position of each text
txtSize_1 = draw.textsize('Not yet till the New Year.', font2)
pos_x_1 = (1000 - txtSize_1[0) /2
txtSize_2 = draw.textsize('day', font2)
pos_x_2 = (1000 - txtSize_2[0) /2

wenhou = ["New Year's Eve"."Sticking Spring Festival couplets"."Hair the noodles."."Buy new clothes"."Cook meat."."Shopping for Chinese New Year"."Clean the house"."Sacrifice hearth"]
txtSize_3 = draw.textsize(wenhou[i-1], font3)
pos_x_3 = (1000 - txtSize_3[0) /2

# Set the text placement position, centered
draw.text((pos_x_1, 200), 'Not yet till the New Year.', fill=(217.217.217.255), font=font2)
draw.text((pos_x_2, 1400), 'day', fill=(217.217.217.255), font=font2)
draw.text((pos_x_3, 1700), wenhou[i-1], fill=im_color[i-1], font=font3)

# Set the text properties of the change
txtSize_4 = draw.textsize(str(i), font1)
pos_x_4 = (1000 - txtSize_4[0) /2
draw.text((pos_x_4, 600), str(i), fill=(255.192.0.255), font=font1)
Copy the code




005. Save the image locallyCopy the code
Save the image to the current directory named dayx.pngCopy the code
# Save image
filename = 'day' + str(i) + '.png'
im.save(filename)
Copy the code




006. GitHub project address HTTPS://github.com/wwtm/gitpython_examplesClick to become a registered member of the communityCopy the code