Make "New Year's Eve Countdown" poster with python. Are you ready for the new year?

Yun Qi Hao: https://yqh.aliyun.comThe first-hand cloud information, the selected cloud enterprise case base of different industries, and the best prac...

Yun Qi Hao: https://yqh.aliyun.com
The first-hand cloud information, the selected cloud enterprise case base of different industries, and the best practices extracted from many successful cases help you to make cloud decision!

000. Effect preview

001. Create image

The RGBA value is a set of numbers,

Red, green, blue and alpha (transparency)

An integer representing 0 (not at all) to 255 (highest)

In pilot,
RGBA values are represented as tuples of four integer values.

For example, red means (255, 0, 0255)

In this color, the value of red is the largest, without green and blue,
And alpha is the highest, which means it's completely opaque.

Green: (0255, 0255)

Blue: (0, 0255255)

White is a combination of colors: (255255255255)

And black doesn't have any color: (0, 0, 0255)

Inspiration: 2019 New Year Countdown dynamic poster series

Pilot 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 the color name string as the first argument,
String 'RGBA' as the second parameter,
Returns an RGBA tuple.

from PIL import ImageColor ImageColor.getcolor('red', 'RGBA')

Operation result:
(255, 0, 0, 255)

Image size and color in this example:
Size: 1000 * 2160

Color: (174,60,58255)

The relevant codes are as follows:

from PIL import Image, ImageDraw, ImageFont import os # Create an image and set the size and color im = Image.new('RGBA', (1000, 2160), (174,60,58,255)) draw = ImageDraw.Draw(im)

002. Set font

To set the font and size,

Let's first save the folder name in fontsfolder.

Then call ImageFont.truetype().

Pass in the. TTF file of the font we want,

Followed by an integer representing the font size.

The Font object returned by ImageFont.truetype()

It's stored in variables like arialFont,

Then pass the variable into text(),

As the last key parameter.

# 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)

003. Draw rectangle

rectangle(xy, fill, outline) method
Draw a rectangle

The xy parameter is a rectangular tuple,

The form is (left,top, right, bottom).

The left and top values specify the x and y coordinates of the upper left corner of the rectangle,

Right and bottom specify the lower right corner of the rectangle.

The optional fill parameter is color, which fills the inside of the rectangle.

The optional outline parameter is the color of the rectangular outline.

# 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))

004. Draw text

The ImageDraw object also has a text() method,
Used to draw text on an image.

The text() method has four parameters:

The xy parameter is a tuple of two integers, specifying the upper left corner of the text area

The text parameter is the text string you want to write

The optional parameter fill is the color of the text

The optional parameter font is an ImageFont object,

Used to set the font and size of text

Because it's usually hard to know a piece of text in advance
The size under the given font,
So the ImageDraw module also provides the textsize() method.

The textsize() method returns a two integer tuple,
Indicates that if the image is written in the specified font,
The width and height of the text.

# Calculate the placement of each text txtSize_1 = draw.textsize('New year's Eve', 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", "Post new year's scrolls", "Facial hair", "Put on new clothes", "Boiled meat", "Do Spring Festival shopping", "General cleaning", "Sacrifice stove"] txtSize_3 = draw.textsize(wenhou[i-1], font3) pos_x_3 = (1000 - txtSize_3[0]) / 2 # Set text placement, center draw.text((pos_x_1, 200), 'New year's Eve', 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 changing text properties 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)

005. Save image to local

Save image to current directory
Named: dayx.png

# Save image filename = 'day' + str(i) + '.png' im.save(filename)

006. I am the summary

Referring to some of the big guy's code,
With the help of the pilot module on image processing,

Some new design elements have been added,
A new version of the New Year Countdown poster has been produced.

Yun Qi Hao: https://yqh.aliyun.com
The first-hand cloud information, the selected cloud enterprise case base of different industries, and the best practices extracted from many successful cases help you to make cloud decision!

Original release time: January 17, 2020
Author: GitPython
This article is from Alibaba cloud Qihao partner“ Qingfeng Python ”, you can pay attention to“ Qingfeng Python"

18 January 2020, 04:48 | Views: 3570

Add new comment

For adding a comment, please log in
or create account

0 comments