From 1e303493c735a70cff2cfb8396db0674bcfff96d Mon Sep 17 00:00:00 2001 From: Shin-myoung-serp Date: Thu, 28 Jul 2022 21:00:19 +0900 Subject: [PATCH 1/7] gh-95371: Add support for other image formats(e.g. PNG) to the turtle module --- Doc/library/turtle.rst | 15 ++++++++++++--- Lib/turtle.py | 30 ++++++++++++++---------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index 17bf8829a9fed14..04562157609453b 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -1610,7 +1610,7 @@ 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(PGM, PPM, GIF, and PNG) 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 @@ -1998,7 +1998,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(PGM, PPM, GIF, and PNG): 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:: @@ -2006,7 +2015,7 @@ 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 diff --git a/Lib/turtle.py b/Lib/turtle.py index a8876e76bce40a5..358e103b0d1d6bd 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -52,7 +52,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. @@ -474,7 +474,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) @@ -882,10 +882,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: @@ -1110,14 +1107,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(PGM, PPM, GIF, and PNG) 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(PGM, PPM, GIF, and PNG). + 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). @@ -1130,12 +1131,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 @@ -1464,7 +1462,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(PGM, PPM, GIF, and PNG) or "nopic". If picname is a filename, set the corresponding image as background. If picname is "nopic", delete backgroundimage, if present. From 79fa157981f90e4dcb8c0b534fc3f2c31c35ea88 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 28 Jul 2022 12:33:00 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst 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 000000000000000..9531e71eea897b2 --- /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(e.g. PNG) to the turtle module. Patch by Shin-myoung-serp. From 76c4c121862e04a6464900f14892974811ba9ce2 Mon Sep 17 00:00:00 2001 From: Shin-myoung-serp Date: Mon, 1 Aug 2022 09:56:00 +0900 Subject: [PATCH 3/7] Change the document to list popular image formats first --- Doc/library/turtle.rst | 6 +++--- Lib/turtle.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index 04562157609453b..b89d4f12201c667 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -1610,7 +1610,7 @@ Window control .. function:: bgpic(picname=None) - :param picname: a string, name of an image file(PGM, PPM, GIF, and PNG) 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 @@ -1989,7 +1989,7 @@ Settings and special methods There are three 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") @@ -1998,7 +1998,7 @@ 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 the name of an image file(PGM, PPM, GIF, and PNG): Install the + (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") diff --git a/Lib/turtle.py b/Lib/turtle.py index 358e103b0d1d6bd..073d19dc89370d5 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -1107,11 +1107,11 @@ def register_shape(self, name, shape=None): """Adds a turtle shape to TurtleScreen's shapelist. Arguments: - (1) name is the name of an image file(PGM, PPM, GIF, and PNG) 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 the name of an image file(PGM, PPM, GIF, and PNG). + (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! @@ -1462,7 +1462,7 @@ def bgpic(self, picname=None): """Set background image or return name of current backgroundimage. Optional argument: - picname -- a string, name of an image file(PGM, PPM, GIF, and PNG) 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. From 782d596c9936a512e041f3a88bf03548a3921851 Mon Sep 17 00:00:00 2001 From: Shin-myoung-serp Date: Mon, 1 Aug 2022 10:44:14 +0900 Subject: [PATCH 4/7] Add a space before '(' in the documentation --- .../next/Library/2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9531e71eea897b2..ddef2fe40eb8715 100644 --- 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 @@ -1 +1 @@ -Added support for other image formats(e.g. PNG) to the turtle module. Patch by Shin-myoung-serp. +Added support for other image formats (e.g. PNG) to the turtle module. Patch by Shin-myoung-serp. From d7bfc33534c42e033fad90f08f36198edf8d7da6 Mon Sep 17 00:00:00 2001 From: Shin-myoung-serp Date: Sat, 28 Dec 2024 18:23:56 +0900 Subject: [PATCH 5/7] Added versionchanged information to the two functions in the document --- Doc/library/turtle.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index c8b433ca0251f90..f4f45fb94202fb0 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -1836,6 +1836,9 @@ Window control >>> screen.bgpic() "landscape.gif" + .. versionchanged:: next + Documented supported image file formats. + .. function:: clear() :noindex: @@ -2200,7 +2203,7 @@ 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 an image file (PNG, GIF, PGM, and PPM) and *shape* is ``None``: Install the corresponding image shape. :: @@ -2234,6 +2237,10 @@ Settings and special methods Add a turtle shape to TurtleScreen's shapelist. Only thusly registered shapes can be used by issuing the command ``shape(shapename)``. + .. versionchanged:: next + Added a support for PNG, PGM, and PPM image formats. + Both a shape name and an image file name can be specified.(Second case) + .. function:: turtles() From a107c680404328cdd3f7e2c76199ff4cdef1ae8d Mon Sep 17 00:00:00 2001 From: Shin-myoung-serp Date: Mon, 30 Dec 2024 10:24:06 +0900 Subject: [PATCH 6/7] Refined the document as suggested by hugovk --- Doc/library/turtle.rst | 10 ++++------ .../2022-07-28-12-32-59.gh-issue-95371.F24IFC.rst | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index f4f45fb94202fb0..d5521e0c2c2f758 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 an image file (PNG, GIF, PGM, and PPM) 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 @@ -1836,9 +1837,6 @@ Window control >>> screen.bgpic() "landscape.gif" - .. versionchanged:: next - Documented supported image file formats. - .. function:: clear() :noindex: @@ -2238,8 +2236,8 @@ Settings and special methods shapes can be used by issuing the command ``shape(shapename)``. .. versionchanged:: next - Added a support for PNG, PGM, and PPM image formats. - Both a shape name and an image file name can be specified.(Second case) + 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/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 index ddef2fe40eb8715..4a62aaed78b4257 100644 --- 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 @@ -1 +1 @@ -Added support for other image formats (e.g. PNG) to the turtle module. Patch by Shin-myoung-serp. +Added support for other image formats (PNG, PGM, and PPM) to the turtle module. Patch by Shin-myoung-serp. From 0644d711d0cbdc45b27d6ec54cf87cbeedd3d00d Mon Sep 17 00:00:00 2001 From: Shin-myoung-serp Date: Tue, 31 Dec 2024 15:35:56 +0900 Subject: [PATCH 7/7] Update Doc/library/turtle.rst Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Doc/library/turtle.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index d5521e0c2c2f758..512647f5f6e01f3 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -1824,7 +1824,7 @@ Window control .. function:: bgpic(picname=None) :param picname: a string, name of an image file (PNG, GIF, PGM, and PPM) - or ``"nopic"``, or ``None`` + 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