From e8f05bd9bb165583c3e6e093b4160df79fd42e85 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 15 Sep 2023 13:45:00 +0800 Subject: [PATCH 1/5] Support .jpeg as JPEG image extension --- pygmt/figure.py | 16 +++++++++++----- pygmt/tests/test_figure.py | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index f23d9fe4011..ddb4daf402b 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -260,9 +260,9 @@ def savefig( This method implements a matplotlib-like interface for :meth:`pygmt.Figure.psconvert`. - Supported formats: PNG (``.png``), JPEG (``.jpg``), PDF (``.pdf``), - BMP (``.bmp``), TIFF (``.tif``), EPS (``.eps``), and KML (``.kml``). - The KML output generates a companion PNG file. + Supported formats: PNG (``.png``), JPEG (``.jpg`` or ``.jpeg), + PDF (``.pdf``), BMP (``.bmp``), TIFF (``.tif``), EPS (``.eps``), and + KML (``.kml``). The KML output generates a companion PNG file. You can pass in any keyword arguments that :meth:`pygmt.Figure.psconvert` accepts. @@ -298,14 +298,15 @@ def savefig( "png": "g", "pdf": "f", "jpg": "j", + "jpeg": "j", "bmp": "b", "eps": "e", "tif": "t", "kml": "g", } - prefix, ext = os.path.splitext(fname) - ext = ext[1:] # Remove the . + prefix = Path(fname).with_suffix("") + ext = Path(fname).suffix[1:] # suffix without . if ext not in fmts: if ext == "ps": raise GMTInvalidInput( @@ -327,6 +328,11 @@ def savefig( kwargs["W"] = "+k" self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs) + + # rename .jpg to .jpeg + if ext == "jpeg": + Path(fname).with_suffix(".jpg").rename(fname) + if show: launch_external_viewer(fname) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 4cfe4ab9345..d895c08785d 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -83,7 +83,7 @@ def test_figure_savefig_exists(): fig = Figure() fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af") prefix = "test_figure_savefig_exists" - for fmt in "png pdf jpg bmp eps tif".split(): + for fmt in "png pdf jpg jpeg bmp eps tif".split(): fname = ".".join([prefix, fmt]) fig.savefig(fname) assert os.path.exists(fname) From 1176a65bd5bd043be2bbc067fd01c84e07f7e3bd Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 15 Sep 2023 16:59:35 +0800 Subject: [PATCH 2/5] Pass a string as prefix --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index ddb4daf402b..c5aa28111ab 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -305,7 +305,7 @@ def savefig( "kml": "g", } - prefix = Path(fname).with_suffix("") + prefix = Path(fname).with_suffix("").as_posix() ext = Path(fname).suffix[1:] # suffix without . if ext not in fmts: if ext == "ps": From 8f1d1e382ce53adffece1dfedea0092e72fbc354 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 17 Sep 2023 20:00:22 +0800 Subject: [PATCH 3/5] Update pygmt/figure.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index c5aa28111ab..fa753fe7bf9 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -260,7 +260,7 @@ def savefig( This method implements a matplotlib-like interface for :meth:`pygmt.Figure.psconvert`. - Supported formats: PNG (``.png``), JPEG (``.jpg`` or ``.jpeg), + Supported formats: PNG (``.png``), JPEG (``.jpg`` or ``.jpeg``), PDF (``.pdf``), BMP (``.bmp``), TIFF (``.tif``), EPS (``.eps``), and KML (``.kml``). The KML output generates a companion PNG file. From bda7a8fee59c34c4633d7848d61dcbba1e2f6890 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 18 Sep 2023 12:12:36 +0800 Subject: [PATCH 4/5] Improve the codes so that it can be extened to support upper-case extensions in the future --- pygmt/figure.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index fa753fe7bf9..6e9f5d31673 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -298,15 +298,19 @@ def savefig( "png": "g", "pdf": "f", "jpg": "j", - "jpeg": "j", "bmp": "b", "eps": "e", "tif": "t", "kml": "g", } - prefix = Path(fname).with_suffix("").as_posix() - ext = Path(fname).suffix[1:] # suffix without . + fname = Path(fname) + prefix, suffix = fname.with_suffix("").as_posix(), fname.suffix + ext = suffix[1:] + # alias jpeg to jpg + if ext == "jpeg": + ext = "jpg" + if ext not in fmts: if ext == "ps": raise GMTInvalidInput( @@ -329,9 +333,9 @@ def savefig( self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs) - # rename .jpg to .jpeg - if ext == "jpeg": - Path(fname).with_suffix(".jpg").rename(fname) + # Rename if file extension doesn't match the input file suffix + if ext != suffix[1:]: + fname.with_suffix("." + ext).rename(fname) if show: launch_external_viewer(fname) From a9c4c1b98eeda3b660e692562229b6e588b5a6e7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 19 Sep 2023 10:41:10 +0800 Subject: [PATCH 5/5] Update pygmt/figure.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 6e9f5d31673..1f44e0b4ad8 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -306,7 +306,7 @@ def savefig( fname = Path(fname) prefix, suffix = fname.with_suffix("").as_posix(), fname.suffix - ext = suffix[1:] + ext = suffix[1:] # Remove the . # alias jpeg to jpg if ext == "jpeg": ext = "jpg"