Python 中有什么方法可以调整带有乌龟的 gif 形状的大小?

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

我正在使用 turtle 制作一个小游戏,然后意识到我可以将图像文件与 turtle.registershape(filename) 一起使用。我知道您可以使用 turtle.shapesizeturtle.resizemode("auto") 调整默认形状的大小并更改 pensize,但是有什么方法可以使用这些方法调整 gif 文件的大小吗?

import turtle

turtle.addshape("example.gif")

t = turtle.Turtle()

t.shape("example.gif")

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

turtle.exitonclick()

我想要这样的东西工作,但是海龟显示正常,没有调整大小。

我查看了适用的乌龟代码,我相信答案是,"No, not within turtle itself."

引入作为 turtle 基础的 tkinter,为我们提供了一些有限的(完整的)turtle 图像扩展和缩小功能:

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()

如果您想要更大的灵活性,标准方法似乎是在 turtle/tkinter 之外调整图形大小使用PIL模块动态调整图形大小并交给turtle/tkinter.

答案是否定的。我浏览了 cpython 库的源代码并跟踪到绘制图像方法调用。请参阅 cpython 库的 _drawturtle 方法,其中调用 drawImage 方法时没有 pensizeshapesize 等变量的信息。我同意@cdlane 的观点,你将不得不使用其他库来调整大小,但是有一个提示,如果你事先知道你需要的图像大小集,你可以在游戏开始时生成它们并加载它们,而不是在运行时调整大小.