diff --git a/Tests/test_image.py b/Tests/test_image.py index 4d1b66dff..ae3829bf1 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -12,6 +12,7 @@ from .helper import ( assert_image_similar, assert_not_all_same, hopper, + is_mingw, is_win32, ) @@ -609,6 +610,24 @@ class TestImage: assert not fp.closed + def test_show_command(self, tmp_path): + # Arrange + temp_file = str(tmp_path / "opened") + with open(temp_file, "a") as f: + f.close() + assert os.path.exists(temp_file) + im = hopper() + + # Act + im.show( + command=("del" if (is_win32() and not is_mingw()) else "rm") + + " " + + temp_file + ) + + # Assert + assert not os.path.exists(temp_file) + @pytest.mark.parametrize( "test_module", [PIL, Image], ) diff --git a/src/PIL/ImageShow.py b/src/PIL/ImageShow.py index fc5089423..365959599 100644 --- a/src/PIL/ImageShow.py +++ b/src/PIL/ImageShow.py @@ -44,9 +44,19 @@ def show(image, title=None, **options): :param \**options: Additional viewer options. :returns: True if a suitable viewer was found, false otherwise. """ - for viewer in _viewers: - if viewer.show(image, title=title, **options): - return 1 + command = options.get("command") + if command: + + class CommandViewer(Viewer): + def get_command(self, file): + return command + " " + file + + CommandViewer().show(image) + return 1 + else: + for viewer in _viewers: + if viewer.show(image, title=title, **options): + return 1 return 0