The windmill, creaking and turning,

The scenery here is so beautiful! The sky is beautiful, the earth is beautiful…

Has a familiar song taken you back to your childhood?

This song is estimated to be every 80, 90 after the childhood memory!

When I was a child, I watched the TV at home. As long as I heard the theme song, I would have all kinds of nice cartoons on behalf of me.

It’s June 1st Children’s Day again, and as a Python, of course you want to think about what you can do with Python

A while back, we drew a Turtle GIF of Byshim on May 20.

So today we’re going to try it out and draw a moving windmill in Turtle to see if we can bring back our childhood memories.

Turtle draws a windmill

The first step is to look at the important components of the windmill, the four blades and a support rod.

The fan blade can be obtained by building a function and repeatedly drawing 4 times.

Here is the part of the code that draws the windmill fan blade.

def draw_sector(col1,col2) :
    
    turtle.color(col1,col1)
    turtle.circle(30.90)
    turtle.right(90)
    turtle.begin_fill()
    turtle.fd(120)
    turtle.right(90)
    turtle.fd(150)
    turtle.right(135)
    turtle.fd(150* (1.414) -30)
    turtle.end_fill()
    
    turtle.color(col2,col2)
    turtle.begin_fill()
    turtle.right(90)
    turtle.circle(30.90)
    turtle.right(90)
    turtle.fd(75*1.414-30)
    turtle.right(90)       
    turtle.fd(150/1.414)
    turtle.right(135)
    turtle.fd(120)
    turtle.end_fill()
    
    turtle.right(90)
Copy the code

And we only need four cycles to get to the heart of the windmill.

Draw four sectors
turtle.pensize(3)
draw_sector('green'.'darkgreen')
draw_sector((26.188.156), (22.160.133))
draw_sector((241.196.15), (243.156.18))
draw_sector((231.76.60), (192.57.43))
Copy the code

The specific drawing process is shown in the figure below

Note: In order to use RGB color notation in the Turtle library, Five added an extra line to the code:

turtle.Screen().colormode(255)

Now that the core is drawn, the struts are much simpler.

Just set the appropriate width and color and draw a line segment.

turtle.pensize(50)
turtle.goto(0.32)
turtle.right(90)
turtle.color('lightgray')
turtle.fd(350)
Copy the code

Final appearance 👇

So we have a windmill that we can hold, but the story has just begun.

How to make the windmill, squeak to turn, is today’s article the most important.

The approach I adopted here is:

  1. Draw large windmills that rotate at different angles using Turtle

  2. Save the turtle-drawn interface image

  3. Combine all the angles into a GIF

All of these steps can be handled automatically in Python, which makes it much easier.

Save as JPG image

First, the Turtle Graphics library is a very useful vector drawing tool.

But it takes several steps to save the result as a local image.

# save eps
ts = turtle.getscreen()
ts.getcanvas().postscript(file=R "eps" results.)
Copy the code

The above two lines of code can only save Turtle drawings in EPS vector format.

Eps format pictures can be opened with PS, and then manually saved as PNG, JPG, etc. But it’s a hassle, and python batch conversion is definitely a priority once you’ve learned Python.

PIL can be used for picture format conversion through search.

from PIL import Image
im = Image.open("Eps" results.)
im.save("The results. JPG"."JPEG")
Copy the code

OSError: Unable to locate Ghostscript on paths: Unable to locate Ghostscript on Paths

Ghostscript is not found in the environment variable, which requires you to download and install the software

Official website to download address: www.ghostscript.com/download/gs…

Install the version that matches your computer, and then configure the environment variables.

C: Program Files\gs\ gs9.54.0bin = path

Finally, restart the IDE or command line again, and you will be able to use it.

After setting up the above steps, we will be able to download turtle drawing results directly in the future without having to take screenshots.

Draw pictures from different angles

There are many ways to draw pictures from different angles, but I’m only going to talk about one.

Circumference is PI times radius times 2, which is C is equal to 2 PI r.

My solution: the turtle arrow goes around a circle of radius 30, a different circumference each time, and then draws normally. So you’re kind of spinning around this center.

With the save Turtle for JPG, you can generate 360° windmill pictures from different angles.

for i in range(116):
    draw_windmill(i)
Copy the code

Batch draw + save as

So we generated about 100 images.

Image composite GIF

How to combine images into GIFs?

I directly refer to huang’s previous article code to use, the key steps are annotated.

This allows you to compose images into GIFs using Python 👇

def func(DURATION) :
    Get the current working path
    path = os.getcwd()
    Get the list of files in the current working path
    file_list = os.listdir(path)
    Copy the file list to another list
    jpg_list = file_list.copy()
    # Remove images that do not have PNG endings, keep only images that have PNG endings
    for file in file_list:
        if file[-3:)! ="jpg":
            jpg_list.remove(file)
    # Arrange the numbers in the picture in ascending order
    jpg_list.sort(key=lambda x: float(x[:-4]))
    # Get the absolute path of each picture, and get the RGB channel value of each photo, save the RGB channel value of 7 photos in a list
    
    print(jpg_list)
    frames = []
    for png in jpg_list:
        image_path = os.path.join(path, png)
        frames.append(imageio.imread(image_path))
    # Save the image as a GIF and set the interval
    gif_path = os.path.join(path, "my_gif.gif")
    imageio.mimsave(gif_path, frames, 'GIF', duration=DURATION)
Copy the code

Set the interval and do it

func(0.008)
Copy the code

See the effect

A large hand-held windmill that spins

Pond on the banyan tree, cicadas in the sound of summer 6

I hope this big windmill I drew can bring you memories of your childhood.

The code download

If you are interested in the code for this article, click on the card below 👇 to follow “Learn Python” and reply to “61” in the background to get all the code!