Add a small flag (small moon cake) to your avatar in Python

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

national flag

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

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

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

The effect is as follows:

Then draw a main star, and 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 effect is as follows:

Finally, draw four secondary stars. The code is as follows:

# Accessory star 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()
# Secondary star 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()
# Accessory Star 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()
# Accessory star 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 effect is as follows:

Moon Cake

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

First, draw the pattern outline of the outer circle. The code 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 with a radius of 40
    turtle.circle(40, 180)
    # Return to (0, 0) after drawing the semicircle
    turtle.goto(0, 0)
    turtle.right(360)
    turtle.end_fill()

The effect is as follows:

Then draw the inner contour, and the code 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 effect is as follows:

Finally, add text. For example, add two words of bean paste for bean paste filling. The code is as follows:

turtle.color("#F5E16F")
turtle.write("Red bean paste", font=("official script", 60, "bold"))

The final effect is as follows:

Avatar plus national flag (moon cake)

Finally, we add the flag or moon cake we just drew to our avatar. The python library used is OpenCV. You can use PIP install opencv Python command for installation. If the speed is too slow, you can use PIP install - I https://mirrors.aliyun.com/pypi/simple/ Opencv Python accelerates download and installation.

The implementation of this function is also relatively simple. It can be done in just a dozen lines of Python code. The code implementation is as follows:

# Read avatar and flag pattern
img_head = cv2.imread('head.jpg')
img_flag = cv2.imread('flag.png')
# Get the width of avatar and flag pattern
w_head, h_head = img_head.shape[:2]
w_flag, h_flag = img_flag.shape[:2]
# Calculate pattern scaling
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 pictures 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 to the avatar. The effect is as follows:

Add a small moon cake to the avatar. The effect is as follows:

The source code is obtained in the background reply 201001 of the public Python sophomore.

Welcome to search Python sophomore on wechat, read and obtain the source code at the first time, and reply to keyword 1024. You can get all kinds of programming language learning materials sorted out by yourself for free.

Tags: Python

Posted by bouwob on Sat, 14 May 2022 06:46:30 +0300