This year's National Day is also the Mid-Autumn Festival, so first of all, I wish you all a happy holiday, and in this article we use Python to add a little national flag or mooncake to our avatars.
flag (of a country)
For the national flag, we can use Python to draw one. The Python library used is the familiar turtle, and our five-starred red flag consists of the following elements: a red background, a yellow main star, and four yellow secondary stars.
First draw a rectangle with a red background, the code is implemented as follows:
(600, 400, 0, 0) ("red")
The effect is as follows:
Next draw a main star and the code is implemented as follows:
("yellow") ('yellow') (10) # The main star turtle.begin_fill() () (-280,100) () for i in range (5): (150) (144) turtle.end_fill()
The effect is as follows:
Finally four secondary stars are drawn and the code is implemented as follows:
# Adjunct 1 turtle.begin_fill() () (-100,180) (305) () for i in range (5): (50) (144) turtle.end_fill() # Adjunct 2 turtle.begin_fill() () (-50,110) (30) () for i in range (5): (50) (144) turtle.end_fill() # Adjunct 3 turtle.begin_fill() () (-40,50) (5) () for i in range (5): (50) (144) turtle.end_fill() # Substar 4 turtle.begin_fill() () (-100,10) (300) () for i in range (5): (50) (144) turtle.end_fill()
The final result is as follows:
mooncake (esp. for the Mid-Autumn Festival)
For a mooncake, you can also draw one in Python. The mooncake composition consists of the following elements: an outer circular patterned outline, an inner outline, and text.
First of all, let's draw the pattern outline of the outer circle, the code is implemented as follows:
(100) ("#F5E16F") for i in range(20): # Move 18 degrees clockwise (18) turtle.begin_fill() # Distance moved forward (220) # Draw a semicircle with a radius of 40 (40, 180) # Return to (0, 0) after drawing the semicircle # (0, 0) (360) turtle.end_fill()
The effect is as follows:
Then proceed to draw the inner contour, the code is implemented as follows:
# Set the brush thickness (20) # Fill colors (external, internal) ("#F5E16F", "#FF9933") goto(0, -200) # Ready to start filling turtle.begin_fill() (200) # End of filling turtle.end_fill() (360) ('#F5E16F') goto(0, -180) for i in range(12): turtle.begin_fill() (60, 120) (180) (60, 120) turtle.end_fill()
The effect is as follows:
Finally to add text, such as bean paste filling to add the word bean paste, the code to achieve the following:
("#F5E16F") ("Douzha.", font=("Scribe.", 60, "bold"))
The final result is as follows:
Avatar plus flag (mooncake)
Finally, we add the flag or mooncake we just drew to our avatar, using the Python library OpenCV, which is installed using thepip install opencv-python
command is sufficient; if it is too slow, you can use thepip install -i /pypi/simple/ opencv-python
Accelerated download and installation.
This function is also relatively simple to implement, only a dozen lines of Python code can be done, the code implementation is as follows:
# Reading avatars and flag patterns img_head = ('') img_flag = ('') # Get avatar and flag pattern widths 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 Patterns img_flag = (img_flag, (0, 0), fx=scale, fy=scale) # Get 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 the final result ('new_head.jpg', img_head)
Suppose my original avatar was as follows:
Add a small flag to the avatar with the following effect:
Add a small mooncake to the avatar with the following effect:
summarize
to this article on how to use Python to their avatar plus a small flag (small moon cake) of the article is introduced to this, more related Python to the avatar plus a small flag (small moon cake) content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future!