How can I add image to my Turtle Screen
using turtle graphics?
whenever I use the function addshape
I keep getting errors.
does turtle graphics got any other way loading/importing images?
for example:
import turtle
screen = turtle.Screen()
image = r"C:\Users\myUser\Desktop\Python\rocketship.png"
screen.addshape(image)
turtle.shape(image)
Firstly, we will import turtle module. We will create a screen object by using “wn = turtle. The bgpic() function is used to set the background image, and it requires only one argument. To save the image on the turtle screen it should be in “gif” form.
The turtle
module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape
say:
name is the name of a gif-file and shape is
None
: Install the corresponding image shape.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With