将图像添加到 Turtle Screen
adding an image to the Turtle Screen
如何使用海龟图形将图像添加到我的 Turtle Screen
?
每当我使用函数 addshape
时,我总是出错。
乌龟图形是否有其他方式loading/importing图像?
例如:
import turtle
screen = turtle.Screen()
image = r"C:\Users\myUser\Desktop\Python\rocketship.png"
screen.addshape(image)
turtle.shape(image)
turtle
module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape
说:
name is the name of a gif-file and shape is None
: Install the corresponding image shape.
如果您查看 the source,他们对 "gif-file" 是认真的:它决定您是尝试添加图像还是多边形的方式是调用 data.lower().endswith(".gif")
,这显然不适用于 .png
个文件。
而且,即使您修复了该问题,它仍然只能处理 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
的文件格式。
此时,您已经超越了人们通常使用 turtle
所做的事情。这可能值得追求(这样做你会学到很多东西),但使用图像转换程序将 .png
文件转换为 .gif
文件可能更简单,这样它就可以工作使用您现有的代码。
您只能将 gif 文件与 Python Turtle 一起使用。在 ezgif.com.
免费拍摄任何照片和 convert/resize
背景:
win = turtle.Screen()
win.bgpic('background.gif')
使用形状:
win.register_shape('pic1.gif')
sprite = turtle.Turtle()
sprite.shape('pic1.gif')
如何使用海龟图形将图像添加到我的 Turtle Screen
?
每当我使用函数 addshape
时,我总是出错。
乌龟图形是否有其他方式loading/importing图像?
例如:
import turtle
screen = turtle.Screen()
image = r"C:\Users\myUser\Desktop\Python\rocketship.png"
screen.addshape(image)
turtle.shape(image)
turtle
module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape
说:
name is the name of a gif-file and shape is
None
: Install the corresponding image shape.
如果您查看 the source,他们对 "gif-file" 是认真的:它决定您是尝试添加图像还是多边形的方式是调用 data.lower().endswith(".gif")
,这显然不适用于 .png
个文件。
而且,即使您修复了该问题,它仍然只能处理 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
的文件格式。
此时,您已经超越了人们通常使用 turtle
所做的事情。这可能值得追求(这样做你会学到很多东西),但使用图像转换程序将 .png
文件转换为 .gif
文件可能更简单,这样它就可以工作使用您现有的代码。
您只能将 gif 文件与 Python Turtle 一起使用。在 ezgif.com.
免费拍摄任何照片和 convert/resize背景:
win = turtle.Screen()
win.bgpic('background.gif')
使用形状:
win.register_shape('pic1.gif')
sprite = turtle.Turtle()
sprite.shape('pic1.gif')