Skip to content

Add support for other image formats(e.g. PNG) to the turtle module #95371

Closed

Description

@relent95

Feature or enhancement

Add support for PGM, PPM, and PNG formats to turtle.bgpic() and turtle.register_shape().

Pitch

Currently, turtle only supports GIF format for an image file,
where as the backend tkinter.PhotoImage supports PGM, PPM, and PNG formats in addition to GIF format.
If turtle supports PNG format, you can animate true color images.
(It helps teaching Python with turtle, because you can search PNG images more easily than GIF ones on Internet.)
Also it would be consistent if turtle supports all formats that tkinter supports.

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jul 28, 2022
  2. hugovk commented on Jul 28, 2022

    @hugovk
    Member

    Testing with Python 3.11, bgpic already accepts a PGM, PPM and PNG (but docs would need to be updated):

    import turtle
    
    tr = turtle.Turtle()
    wn = turtle.Screen()
    
    # filename = "./Lib/test/imghdrdata/python.gif"
    # filename = "./Lib/test/imghdrdata/python.pgm"
    # filename = "./Lib/test/imghdrdata/python.ppm"
    filename = "./Lib/test/imghdrdata/python.png"
    
    wn.bgpic(filename)
    wn.mainloop()

    Testing register_shape:

    from turtle import Screen, Turtle
    
    screen = Screen()
    screen.screensize(800, 800)
    
    # filename = "./Lib/test/imghdrdata/python.gif"
    # filename = "./Lib/test/imghdrdata/python.pgm"
    # filename = "./Lib/test/imghdrdata/python.ppm"
    filename = "./Lib/test/imghdrdata/python.png"
    
    screen.register_shape(filename)
    turtle = Turtle(filename)
    turtle.penup()
    turtle.goto(-280, -280)
    
    screen.mainloop()

    Works with a change something like this (plus docs would need updating):

    diff --git a/Lib/turtle.py b/Lib/turtle.py
    index a8876e76bc..3a9082c92a 100644
    --- a/Lib/turtle.py
    +++ b/Lib/turtle.py
    @@ -1131,7 +1131,12 @@ def register_shape(self, name, shape=None):
             """
             if shape is None:
                 # image
    -            if name.lower().endswith(".gif"):
    +            if (
    +                    name.lower().endswith(".gif")
    +                    or name.lower().endswith(".png")
    +                    or name.lower().endswith(".pgm")
    +                    or name.lower().endswith(".ppm")
    +            ):
                     shape = Shape("image", self._image(name))
                 else:
                     raise TurtleGraphicsError("Bad arguments for register_shape.\n"

    Alternatively, the extension check could be removed, and just let tkinter handle it, as is the case with bgpic:

    diff --git a/Lib/turtle.py b/Lib/turtle.py
    index a8876e76bc..cc33d37c34 100644
    --- a/Lib/turtle.py
    +++ b/Lib/turtle.py
    @@ -1131,11 +1131,7 @@ def register_shape(self, name, shape=None):
             """
             if shape is None:
                 # image
    -            if name.lower().endswith(".gif"):
    -                shape = Shape("image", self._image(name))
    -            else:
    -                raise TurtleGraphicsError("Bad arguments for register_shape.\n"
    -                                          + "Use  help(register_shape)" )
    +            shape = Shape("image", self._image(name))
             elif isinstance(shape, tuple):
                 shape = Shape("polygon", shape)
             ## else shape assumed to be Shape-instance
    _tkinter.TclError: couldn't recognize data in image file "1.py"
    
  3. added a commit that references this issue on Jul 28, 2022
  4. relent95 commented on Jul 28, 2022

    @relent95
    ContributorAuthor

    Thanks hugovk, you are fast. I was working on a PR when you posted. My approach for register_shape coincides with your second one.
    You may ignore my PR if you are working on your own PR.

  5. relent95 commented on Feb 3, 2023

    @relent95
    ContributorAuthor

    Can anyone review the above PR? Because its base version is quite old, I can prepare a new PR if it's required.

  6. added a commit that references this issue on Jan 2, 2025
  7. added a commit that references this issue on Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions