How to display images with Python Turtle

How do you add an image to the output or aka the turtle graphics?

You have to create a screen and add a background.
AFAIK Turtle only accepts GIF format images, so make sure that your image is a .gif extension.

And what is the code to use it as a background I may ask?

There’s plenty of examples around the internet, a simple one would be this:

import turtle

# You first create the screen
win = turtle.Screen()

# Set the screen dimensions (only if you want)
win.setup(width=800, height=600)

# Set the background
win.bgpic("path_to_your_image.gif")

# Continue with your turtle drawing or just keep the window open
turtle.mainloop()

Oh wow this is easier then I thought, When I looked online it wanted me to use stuff like Pillow or matplotlib and add some crazy code that was hard to understand. Thanks for all your help :).

When I run the program it keeps coming up with the same error

Traceback (most recent call last):
  File "/home/runner/119-Capstone/driver.py", line 26, in <module>
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/runner/119-Capstone/main.py", line 16, in <module>
    exec(file.read())
  File "<string>", line 12, in <module>
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/turtle.py", line 1482, in bgpic
    self._bgpics[picname] = self._image(picname)
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/turtle.py", line 478, in _image
    return TK.PhotoImage(file=filename, master=self.cv)
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/tkinter/__init__.py", line 4093, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/tkinter/__init__.py", line 4038, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "./sheet.gif"
repl process died unexpectedly: exit status 1

Here is my code

import turtle as turl
win = turl.Screen()
p = turl.Turtle()




# Set the screen dimensions (only if you want)
win.setup(width=1280, height=720)

# Set the background
win.bgpic("sheet.gif")





base = input("What kind of flavor do you want your cookies to be")

num_dots = 1
xpos = 0
ypos = 0
p.pensize(10)
p.color(base)
# draw two sets of dots
while (num_dots <= 2 ):
  p.penup()
  p.goto(xpos, ypos)
  p.pendown()
  p.circle(3)
  p.penup()
  p.goto(xpos + 30, ypos + 20)
  p.pendown()
  p.circle(2)

  # position next dots
  xpos = ypos + 25
  xpos = xpos + 5
  num_dot = num_dots + 1


p.mainloop()
1 Like

Ensure that the sheet.gif file is in the same directory as your Python script. Double-check the spelling and capitalization of the file name, including the file extension (.gif). If the image file is not in the same directory, provide the correct file path when setting the background.

Can you please explain this to me like I am 5, I don’t understand what same directory means.

(A directory is a folder in the file system.)
Your sheet.gif file should be at the same ‘level’ as the main.py file, not a different folder, according to Sky.

Everything is in the same folder meaning on the user view it’s not in any folder is that a problem or am I doing something wrong.

Maybe your gif file is actually not a gif? The file name is not necessarily the file format.

Turtle uses tkinter, as seen in the error message.

So it has to be a gif format. Should I just use a file converter or how should I solve this problem.

Either use an actual gif file, or convert your image to a gif, here’s one way:

So you can do this: Open your image with mspaint, then go to File > Save As and from the “Save As Type” dropdown, choose GIF.

Tkinter doesn’t support a lot of file formats which is why using gif is important.

I also have another question is there any way around from using star imports replit does not like it when I use a * when importing.

Yes, there are two ways:

(easiest)
put a comment to tell code linter to ignore

from turtle import *  # type: ignore

(configuration, only have to do once)
Go into pyproject.toml and add "F403" to the ignore list variable.

Do you mean this list its the only list on myproject.toml

replit-python-lsp-server = {extras = ["yapf", "rope", "pyflakes"], version = "^1.5.9"}

100%. Please do not use star imports in your code. Just use from turtle import <functions/classes you will use>

1 Like

Nevermind I guess, your repl is made with an old python template which doesn’t have it.

And yes, it is recommended to use from turtle import thing1, thing2, or just use import turtle.