https://github.com/python/cpython/commit/e1baa778f602ede66831eb34b9ef17f21e4d4347
commit: e1baa778f602ede66831eb34b9ef17f21e4d4347
branch: main
author: Shin-myoung-serp <[email protected]>
committer: hugovk <[email protected]>
date: 2025-01-02T11:45:07+02:00
summary:

gh-95371: Add support for other image formats(e.g. PNG) to the turtle… (#95378)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Oleg Iarygin <[email protected]>

files:
A Misc/NEWS.d/next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst
M Doc/library/turtle.rst
M Lib/turtle.py

diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst
index 8eb4f8271fcfae..512647f5f6e01f 100644
--- a/Doc/library/turtle.rst
+++ b/Doc/library/turtle.rst
@@ -1823,7 +1823,8 @@ Window control
 
 .. function:: bgpic(picname=None)
 
-   :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
+   :param picname: a string, name of an image file (PNG, GIF, PGM, and PPM)
+                   or ``"nopic"``, or ``None``
 
    Set background image or return name of current backgroundimage.  If 
*picname*
    is a filename, set the corresponding image as background.  If *picname* is
@@ -2200,9 +2201,9 @@ Settings and special methods
 .. function:: register_shape(name, shape=None)
               addshape(name, shape=None)
 
-   There are three different ways to call this function:
+   There are four different ways to call this function:
 
-   (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
+   (1) *name* is the name of an image file (PNG, GIF, PGM, and PPM) and 
*shape* is ``None``: Install the
        corresponding image shape. ::
 
        >>> screen.register_shape("turtle.gif")
@@ -2211,7 +2212,16 @@ Settings and special methods
           Image shapes *do not* rotate when turning the turtle, so they do not
           display the heading of the turtle!
 
-   (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
+   (2) *name* is an arbitrary string and *shape* is the name of an image file 
(PNG, GIF, PGM, and PPM): Install the
+       corresponding image shape. ::
+
+       >>> screen.register_shape("turtle", "turtle.gif")
+
+       .. note::
+          Image shapes *do not* rotate when turning the turtle, so they do not
+          display the heading of the turtle!
+
+   (3) *name* is an arbitrary string and *shape* is a tuple of pairs of
        coordinates: Install the corresponding polygon shape.
 
        .. doctest::
@@ -2219,12 +2229,16 @@ Settings and special methods
 
           >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
 
-   (3) *name* is an arbitrary string and *shape* is a (compound) :class:`Shape`
+   (4) *name* is an arbitrary string and *shape* is a (compound) :class:`Shape`
        object: Install the corresponding compound shape.
 
    Add a turtle shape to TurtleScreen's shapelist.  Only thusly registered
    shapes can be used by issuing the command ``shape(shapename)``.
 
+   .. versionchanged:: next
+      Added support for PNG, PGM, and PPM image formats.
+      Both a shape name and an image file name can be specified.
+
 
 .. function:: turtles()
 
diff --git a/Lib/turtle.py b/Lib/turtle.py
index 8a5801f2efe625..1320cfd93fd6db 100644
--- a/Lib/turtle.py
+++ b/Lib/turtle.py
@@ -51,7 +51,7 @@
   turtle. So the turtles can more easily be used as a visual feedback
   instrument by the (beginning) programmer.
 
-- Different turtle shapes, gif-images as turtle shapes, user defined
+- Different turtle shapes, image files as turtle shapes, user defined
   and user controllable turtle shapes, among them compound
   (multicolored) shapes. Turtle shapes can be stretched and tilted, which
   makes turtles very versatile geometrical objects.
@@ -468,7 +468,7 @@ def _blankimage(self):
 
     def _image(self, filename):
         """return an image object containing the
-        imagedata from a gif-file named filename.
+        imagedata from an image file named filename.
         """
         return TK.PhotoImage(file=filename, master=self.cv)
 
@@ -872,10 +872,7 @@ def __init__(self, type_, data=None):
             if isinstance(data, list):
                 data = tuple(data)
         elif type_ == "image":
-            if isinstance(data, str):
-                if data.lower().endswith(".gif") and isfile(data):
-                    data = TurtleScreen._image(data)
-                # else data assumed to be PhotoImage
+            assert(isinstance(data, TK.PhotoImage))
         elif type_ == "compound":
             data = []
         else:
@@ -1100,14 +1097,18 @@ def register_shape(self, name, shape=None):
         """Adds a turtle shape to TurtleScreen's shapelist.
 
         Arguments:
-        (1) name is the name of a gif-file and shape is None.
+        (1) name is the name of an image file (PNG, GIF, PGM, and PPM) and 
shape is None.
             Installs the corresponding image shape.
             !! Image-shapes DO NOT rotate when turning the turtle,
             !! so they do not display the heading of the turtle!
-        (2) name is an arbitrary string and shape is a tuple
+        (2) name is an arbitrary string and shape is the name of an image file 
(PNG, GIF, PGM, and PPM).
+            Installs the corresponding image shape.
+            !! Image-shapes DO NOT rotate when turning the turtle,
+            !! so they do not display the heading of the turtle!
+        (3) name is an arbitrary string and shape is a tuple
             of pairs of coordinates. Installs the corresponding
             polygon shape
-        (3) name is an arbitrary string and shape is a
+        (4) name is an arbitrary string and shape is a
             (compound) Shape object. Installs the corresponding
             compound shape.
         To use a shape, you have to issue the command shape(shapename).
@@ -1120,12 +1121,9 @@ 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, str):
+            shape = Shape("image", self._image(shape))
         elif isinstance(shape, tuple):
             shape = Shape("polygon", shape)
         ## else shape assumed to be Shape-instance
@@ -1454,7 +1452,7 @@ def bgpic(self, picname=None):
         """Set background image or return name of current backgroundimage.
 
         Optional argument:
-        picname -- a string, name of a gif-file or "nopic".
+        picname -- a string, name of an image file (PNG, GIF, PGM, and PPM) or 
"nopic".
 
         If picname is a filename, set the corresponding image as background.
         If picname is "nopic", delete backgroundimage, if present.
diff --git 
a/Misc/NEWS.d/next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst 
b/Misc/NEWS.d/next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst
new file mode 100644
index 00000000000000..4a62aaed78b425
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst
@@ -0,0 +1 @@
+Added support for other image formats (PNG, PGM, and PPM) to the turtle 
module. Patch by Shin-myoung-serp.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to