-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Comments
Testing with Python 3.11, 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 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 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
|
Thanks hugovk, you are fast. I was working on a PR when you posted. My approach for register_shape coincides with your second one. |
Can anyone review the above PR? Because its base version is quite old, I can prepare a new PR if it's required. |
#95378) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
…turtle… (python#95378) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
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
The text was updated successfully, but these errors were encountered: