本文介绍了有什么办法可以在Python中用乌龟调整gif形状的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用乌龟做一些小游戏,并且意识到我可以在turtle.registershape(filename)中使用图像文件.我知道您可以使用turtle.shapesizeturtle.resizemode("auto")并更改pensize来调整默认形状的大小,但是有什么方法可以使用这些方法来调整gif文件的大小?

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.

推荐答案

我查看了适用的乌龟代码,我相信答案是不,不在乌龟本身之内."

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

引入乌龟下面的tkinter可以使我们获得一些有限的(整体)乌龟图像扩展和缩小功能:

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

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

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.

这篇关于有什么办法可以在Python中用乌龟调整gif形状的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 18:24