Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
relent95 opened this issue Jul 28, 2022 · 3 comments
Open

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

relent95 opened this issue Jul 28, 2022 · 3 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@relent95
Copy link
Contributor

relent95 commented Jul 28, 2022

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

@relent95 relent95 added the type-feature A feature request or enhancement label Jul 28, 2022
@hugovk hugovk added the stdlib Python modules in the Lib dir label Jul 28, 2022
@hugovk
Copy link
Member

hugovk commented Jul 28, 2022

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"

@relent95
Copy link
Contributor Author

relent95 commented Jul 28, 2022

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.

@relent95
Copy link
Contributor Author

relent95 commented Feb 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

2 participants