Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding an image to the Turtle Screen

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)
like image 764
Liron Lavi Avatar asked May 24 '15 19:05

Liron Lavi


People also ask

How do you put a background picture on a turtle?

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.


1 Answers

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.

like image 50
abarnert Avatar answered Oct 13 '22 03:10

abarnert