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

13.3k views Asked by At

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 turtle.shapesize or turtle.resizemode("auto") and changing pensize, but is there any way to resize a gif file using these methods?

import turtle

turtle.addshape("example.gif")

t = turtle.Turtle()

t.shape("example.gif")

t.resizemode("auto")
t.pensize(24)
t.stamp()

turtle.exitonclick()

I want something like this to work, but the turtle is displayed normally, not resized.

3

There are 3 answers

0
cdlane On BEST ANSWER

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.

0
codebae On

No, there is no way to resize gif's shape via turtle explicitly. However, you can resize the shape manually using this website: GIF RESIZER

Further, use the optimized gif (length & width wise) in your code. I also use this approach and is very helpful.

Thanks!

0
saurabheights On

The answer is no. I went through the source code of cpython library and tracked to the draw image method call. See _drawturtle method of cpython library, where drawImage method is called with no information of variables like pensize or shapesize. I agree with @cdlane that you will have to use other resize using other libraries, however one tip, if you know beforehand the set of image sizes you will need, you can generate them and load during the game start instead of resizing at the runtime.