Python海龟画图每步截屏合成gif源代码

Python海龟画图每步截屏合成gif源代码

import turtle
from PIL import Image,ImageTk

def save():    
    left = screen._root.winfo_rootx()
    top = screen._root.winfo_rooty()
    width = screen.window_width()
    height = screen.window_height()
    box = (left,top,left+width,top+height)
    im = ImageGrab.grab(box)
    return im

screen = turtle.getscreen()
screen.setup(480,360)
screen.delay(0)
turtle.speed(0)
frames = []
for i in range(4):
    frame = save()
    frames.append(frame)
    turtle.fd(100)
    frame = save()
    frames.append(frame)
    turtle.right(90)
# frames形成后,可以保存到gif了 frames[0].save()即可
zx = [turtle.Shape('image',ImageTK.PhotoImage(im)) for im in frames]

screen.mainloop()
李兴球

李兴球的博客是Python创意编程原创博客