Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Showing an image opens a console

Open Vermylion opened this issue 1 year ago • 3 comments

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

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.

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).

Vermylion avatar Feb 09 '24 09:02 Vermylion

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

radarhere avatar Feb 09 '24 09:02 radarhere

I've created PR https://github.com/python-pillow/Pillow/pull/7791 with your solution, except with subprocess.Popen() instead of subprocess.call(), to prevent blocking.

radarhere avatar Feb 10 '24 09:02 radarhere

Hi, thanks for making the PR!

Vermylion avatar Feb 11 '24 17:02 Vermylion