Repository: python-pillow/Pillow

Issue:
Showing an image opens a console
<!--
Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

1.  General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    * https://stackoverflow.com/questions/tagged/python-imaging-library

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

2.  Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    * https://stackoverflow.com/help/mcve

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

3.  Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->

### What did you do?
Tried opening an image using ``image.show()`` with a ``.pyw`` file.

### What did you expect to happen?
Logically, no cmd console should pop up, as the file is ``no console``.

### What actually happened?
PIL opens up a console when launching image viewer, presumably Windows Viewer (default viewer).

### What are your OS, Python and Pillow versions?

* OS: Windows 10/11
* Python: 3.12
* Pillow: 10.2.0

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->

### To Replicate the Issue

Have a file with no console extension: ``main.pyw``
Launch python file outside of an environment, IDE, or a console (cmd). Launch it, for example, from the Windows File Explorer.

![](https://github.com/Vermylion/assets/raw/main/main.pyw_explorer_bug_report.png)

```python
from PIL import Image

img_path = 'your_image_path.png' # Replace with your image path

img = Image.open(img_path)

img.show()
```

### Potential Solution
From what I've seen and understood, the issue is caused in ``ImageShow.py``, ``show_file()`` line 116 with ``os.system(self.get_command(path, **options))``. This line of code appears to be opening a new console if the file is ran with no console. However, it can be easily fixed by replacing ``os`` by ``subprocess``: ``subprocess.call(self.get_command(path, **options), shell=True, creationflags=subprocess.CREATE_NO_WINDOW)`` (has been tested and works).




Discussion:
Hi. Would you like to create a PR with your solution?

Resolving patch:
diff --git a/src/PIL/ImageShow.py b/src/PIL/ImageShow.py
--- a/src/PIL/ImageShow.py
+++ b/src/PIL/ImageShow.py
@@ -138,6 +138,17 @@ def get_command(self, file: str, **options: Any) -> str:
             f'&& del /f "{file}"'
         )
 
+    def show_file(self, path: str, **options: Any) -> int:
+        """
+        Display given file.
+        """
+        subprocess.Popen(
+            self.get_command(path, **options),
+            shell=True,
+            creationflags=getattr(subprocess, "CREATE_NO_WINDOW"),
+        )  # nosec
+        return 1
+
 
 if sys.platform == "win32":
     register(WindowsViewer)


Test patch: