向海龟屏幕添加图像 [英] adding an image to the Turtle Screen
问题描述
如何使用乌龟图形将图像添加到Turtle Screen
中?
How can I add image to my Turtle Screen
using turtle graphics?
每当我使用功能addshape
时,我都会不断收到错误消息.
whenever I use the function addshape
I keep getting errors.
乌龟图形还有其他加载/导入图像的方式吗?
does turtle graphics got any other way loading/importing images?
例如:
import turtle
screen = turtle.Screen()
image = r"C:\Users\myUser\Desktop\Python\rocketship.png"
screen.addshape(image)
turtle.shape(image)
推荐答案
turtle
模块确实支持图像,但仅支持GIF图像,不支持PNG或任何其他格式.作为 addshape
的文档说:
The turtle
module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape
say:
名称是gif文件的名称,形状为
None
:安装相应的图像形状.
name is the name of a gif-file and shape is
None
: Install the corresponding image shape.
如果您查看的是来源,他们对"gif文件"很认真:它决定是要添加图像还是要通过调用data.lower().endswith(".gif")
来确定多边形,这显然不适用于.png
文件.
And if you look at the source, they're serious about "gif-file": the way it decides whether you're trying to add an image or a polygon is by calling data.lower().endswith(".gif")
, which obviously won't work for .png
files.
而且,即使您修复了该错误,它也仍然只能处理 Pillow
.
And, even if you fix that, it will still only be able to handle the file formats that Tkinter
supports out of the box, which includes some extra things like PPM/PGM/PBM, but still not PNG. If you want to support PNG files, you'll want to install Pillow
as well.
在这一点上,您已经超越了人们通常使用turtle
所做的事情.可能值得追求(这样做会学到很多东西),但是使用图像转换程序将.png
文件转换为.gif
文件可能更简单,因此它将与您现有的代码一起使用
At this point, you're getting beyond what people usually do with turtle
. That might be worth pursuing (you'll learn a lot by doing so), but it may be simpler to use an image-converting program to convert the .png
file to a .gif
file so it will work with your existing code.
这篇关于向海龟屏幕添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!