Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 874
The_Basset_Hound Avatar asked Sep 12 '25 07:09

The_Basset_Hound


1 Answers

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.

like image 195
cdlane Avatar answered Sep 13 '25 19:09

cdlane