Is there any way to resize a gif shape with turtle in Python?

后端 未结 2 1962
南方客
南方客 2021-01-27 03:16

I\'m using turtle to make a little game and realized I could use image files with turtle.registershape(filename). I know you can resize the default shapes with

2条回答
  •  清歌不尽
    2021-01-27 03:31

    I reviewed the applicable turtle code and I believe the answer is, "No, not within turtle itself."

    Bringing in tkinter, which underlies turtle, gives us some limited (integral) turtle image expansion and reduction capability:

    from tkinter import PhotoImage
    from turtle import Turtle, Screen, Shape
    
    screen = Screen()
    
    # substitute 'subsample' for 'zoom' if you want to go smaller:
    larger = PhotoImage(file="example.gif").zoom(2, 2)
    
    screen.addshape("larger", Shape("image", larger))
    
    tortoise = Turtle("larger")
    
    tortoise.stamp()
    
    tortoise.hideturtle()
    
    screen.exitonclick()
    

    If you want more flexibility, the standard approach seems to be to either resize the graphic outside of turtle/tkinter or use the PIL module to resize the graphic dynamically and hand it to turtle/tkinter.

提交回复
热议问题