Use Python to add a small flag (small moon cake) to your avatar


Today is National Day and Mid-Autumn Festival. First of all, I wish you all a happy holiday. In this article, we use Python to add a small 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 turtle that everyone is more familiar with. The elements of our five-star red flag include: a red background, a yellow primary star and four yellow secondary stars.

First draw a rectangular red background, the code implementation is as follows:

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

The effect is as follows:

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

turtle.fillcolor("yellow")
turtle.color('yellow')
turtle.speed(10)
# 主星
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 implemented as follows:

# 副星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()
# 副星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()
# 副星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()
# 副星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

For mooncakes, you can also use Python to draw one. Mooncakes' constituent elements mainly include: outer circular pattern outline, inner outline and text.

First, draw the outline of the outer circle. The code is implemented as follows:

turtle.speed(100)
turtle.color("#F5E16F")
for i in range(20):
    # 顺时针移动18度
    turtle.right(18)
    turtle.begin_fill()
    # 向前移动的距离
    turtle.forward(220)
    # 画半径为 40 的半圆
    turtle.circle(40, 180)
    # 画完半圆之后回到(0,0)
    turtle.goto(0, 0)
    turtle.right(360)
    turtle.end_fill()

The effect is as follows:

Then draw the inner contour, the code is implemented as follows:

# 设置画笔粗细
turtle.pensize(20)
# 填充颜色(外部、内部)
turtle.color("#F5E16F", "#FF9933")
goto(0, -200)
# 准备开始填充
turtle.begin_fill()
turtle.circle(200)
# 填充结束
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 red bean paste for bean paste filling. The code implementation is as follows:

turtle.color("#F5E16F")
turtle.write("豆沙", font=("隶书", 60, "bold"))

The final effect is as follows:

Head and flag (moon cake)

Finally, we will just draw a good moon cake added to the flag or your own avatar, used Python library is OpenCV, installation pip install opencv-pythoncan be ordered, if the speed is too slow, can be used pip install -i https://mirrors.aliyun.com/pypi/simple/ opencv-pythonto accelerate download and install.

This function is also relatively simple to implement. It can be done with only a dozen lines of Python code. The code is implemented as follows:

# 读取头像和国旗图案
img_head = cv2.imread('head.jpg')
img_flag = cv2.imread('flag.png')
# 获取头像和国旗图案宽度
w_head, h_head = img_head.shape[:2]
w_flag, h_flag = img_flag.shape[:2]
# 计算图案缩放比例
scale = w_head / w_flag / 4
# 缩放图案
img_flag = cv2.resize(img_flag, (0, 0), fx=scale, fy=scale)
# 获取缩放后新宽度
w_flag, h_flag = img_flag.shape[:2]
# 按 3 个通道合并图片
for c in range(0, 3):
    img_head[w_head - w_flag:, h_head - h_flag:, c] = img_flag[:, :, c]
# 保存最终结果
cv2.imwrite('new_head.jpg', img_head)

If my original avatar 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:

Guess you like

Origin blog.csdn.net/ityard/article/details/108892864