Add a small flag (mooncake) to your avatar in Python

Time:2021-3-5

Today is the national day and the Mid Autumn Festival. First of all, I wish you a happy holiday. In this article, we use Python to add a small flag or mooncake to our avatar.

national flag

For the national flag, we can use Python to draw one. The python library we use is the familiar turtle. Our five-star red flag elements include: red background, a yellow main star and four yellow secondary stars.

First, draw a rectangular red background. The code is as follows:

turtle.setup(600, 400, 0, 0)
turtle.bgcolor("red")

The results are as follows

Add a small flag (mooncake) to your avatar in Python

Then draw a main star, the code is as follows:

turtle.fillcolor("yellow")
turtle.color('yellow')
turtle.speed(10)
#Main star
turtle.begin_fill()
turtle.up()
turtle.goto(-280,100)
turtle.down()
for i in range (5):
    turtle.forward(150)
    turtle.right(144)
turtle.end_fill()

The results are as follows

Add a small flag (mooncake) to your avatar in Python

Finally, draw four sub stars, the code is as follows:

#Parastar 1
turtle.begin_fill()
turtle.up()
turtle.goto(-100,180)
turtle.setheading(305)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.left(144)
turtle.end_fill()
#Parastar 2
turtle.begin_fill()
turtle.up()
turtle.goto(-50,110)
turtle.setheading(30)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()
#Parastar 3
turtle.begin_fill()
turtle.up()
turtle.goto(-40,50)
turtle.setheading(5)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()
#Parastar 4
turtle.begin_fill()
turtle.up()
turtle.goto(-100,10)
turtle.setheading(300)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.left(144)
turtle.end_fill()

The final results are as follows:

Add a small flag (mooncake) to your avatar in Python

Moon Cake

Python can also be used to draw a moon cake. The elements of the moon cake mainly include: the outline of the outer circular pattern, the outline of the inner layer and the text.

First of all, to draw the outline of the outer circular pattern, the code implementation is as follows:

turtle.speed(100)
turtle.color("#F5E16F")
for i in range(20):
    #Move 18 degrees clockwise
    turtle.right(18)
    turtle.begin_fill()
    #Distance to move forward
    turtle.forward(220)
    #Draw a semicircle of radius 40
    turtle.circle(40, 180)
    #After drawing the semicircle, return to (0, 0)
    turtle.goto(0, 0)
    turtle.right(360)
    turtle.end_fill()

The results are as follows

Add a small flag (mooncake) to your avatar in Python

Then draw the inner contour, and the code implementation is as follows:

#Set brush thickness
turtle.pensize(20)
#Fill color (outside, inside)
turtle.color("#F5E16F", "#FF9933")
goto(0, -200)
#Ready to start filling
turtle.begin_fill()
turtle.circle(200)
#End of filling
turtle.end_fill()
turtle.right(360)
turtle.color('#F5E16F')
goto(0, -180)
for i in range(12):
    turtle.begin_fill()
    turtle.circle(60, 120)
    turtle.left(180)
    turtle.circle(60, 120)
    turtle.end_fill()

The results are as follows

Add a small flag (mooncake) to your avatar in Python

Finally, to add text, for example, to add the word Dousha for Dousha stuffing, the code implementation is as follows:

turtle.color("#F5E16F")
turtle.write (Dousha, font = (Lishu, 60, bold))

The final results are as follows:

Add a small flag (mooncake) to your avatar in Python

Head portrait and national flag (moon cake)

Finally, we will just draw a good flag or moon cake added to their own avatar, using the python library is opencv, installation and usepip install opencv-pythonIf the speed is too slow, you can use thepip install -i https://mirrors.aliyun.com/pypi/simple/ opencv-pythonSpeed up download and installation.

This function is also relatively simple to implement. It only needs a dozen lines of Python code. The code implementation is as follows:

#Read head portrait and national flag pattern
img_head = cv2.imread('head.jpg')
img_flag = cv2.imread('flag.png')
#Get the width of head portrait and national flag pattern
w_head, h_head = img_head.shape[:2]
w_flag, h_flag = img_flag.shape[:2]
#Calculate pattern scale
scale = w_head / w_flag / 4
#Zoom pattern
img_flag = cv2.resize(img_flag, (0, 0), fx=scale, fy=scale)
#Gets the new width after scaling
w_flag, h_flag = img_flag.shape[:2]
#Merge images by 3 channels
for c in range(0, 3):
    img_head[w_head - w_flag:, h_head - h_flag:, c] = img_flag[:, :, c]
#Save final results
cv2.imwrite('new_head.jpg', img_head)

If my original head is as follows:

Add a small flag (mooncake) to your avatar in Python

Add a small national flag to the picture. The effect is as follows:

Add a small flag (mooncake) to your avatar in Python

Add a small moon cake to the picture, and the effect is as follows:

Add a small flag (mooncake) to your avatar in Python

Source code in the public numberPython sophomoreBackstage reply201001obtain.

Welcome to wechat searchPython sophomore, the first time to read, access to source code, reply keywords1024You can get all kinds of programming language learning materials for free.