diff --git a/pygmt/figure.py b/pygmt/figure.py index f23d9fe4011..1f44e0b4ad8 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. @@ -304,8 +304,13 @@ def savefig( "kml": "g", } - prefix, ext = os.path.splitext(fname) - ext = ext[1:] # Remove the . + fname = Path(fname) + prefix, suffix = fname.with_suffix("").as_posix(), fname.suffix + ext = suffix[1:] # Remove the . + # alias jpeg to jpg + if ext == "jpeg": + ext = "jpg" + if ext not in fmts: if ext == "ps": raise GMTInvalidInput( @@ -327,6 +332,11 @@ def savefig( kwargs["W"] = "+k" self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs) + + # 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) 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)