1

I'm trying to display an image using python. I used the following code :

from PIL import Image

img = Image.open("pic.jpg")
img.show()
del img

I checked that everything was installed, and tried different options inside the Image.open part, yet nothing works. I don't even have an error (so other topics were irrelevant in my case).

I am new to python, so I have no idea how to debug this. My code just executes but nothing happens.

If you have an idea, I'll try. Thank you.

Eltom
  • 11
  • 1
  • @Suvo: What you say is untrue. Try it. The script works fine for me so the problem must be something else. The [documenation](https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.show) says that the `show()` method creates a temporary file and invokes an OS-dependent utility to display it, so deleting the in-memory image data doesn't affect that. – martineau Feb 04 '21 at 21:19
  • Eltom: Have you tried different images & image types? – martineau Feb 04 '21 at 21:21
  • 1) Please state your OS and how you ran the script 2) please add `print(img)` after you open it so we can see its dimensions and type. Thank you. – Mark Setchell Feb 04 '21 at 22:12
  • Thank you for your answer. The del function is just to make it cleaner. I tried with both an img in png and jpg, and it didn't open the img file. I am on raspbian (maybe I should have stated that because I thought it was OS-dependent) but when I installed PIL it seemed to work perfectly. print(img) doesn't write anything in the teminal. The script is ran using python3 in the term. – Eltom Feb 11 '21 at 19:50
  • So you ran `python3 YOURSCRIPT.py` from the Terminal in a directory where you can see your image listed if you run `ls -l pic.jpg` ? – Mark Setchell Feb 11 '21 at 20:13
  • 0 It may be that you don't have a default image viewer set. Try opening the image file outside of the program and see if it asks you to select a program or just opens in another program. – pippo1980 Feb 24 '21 at 15:50
  • see here: https://stackoverflow.com/questions/12570859/how-to-show-pil-images-on-the-screen/12571366 – pippo1980 Feb 24 '21 at 15:51
  • Yes that is how I open my script. I don't know I have no trouble opening an image on its own so that is not the viewer imo. Thanks for the link but I did look for answers elsewhere and the only way I found around it is to open the image in a webbrowser (through python) – Eltom Mar 09 '21 at 04:55
  • Try adding `print(img)` on the line after opening it and reporting back the result. – Mark Setchell Mar 09 '21 at 07:25

1 Answers1

0

You can get some information to debug your Python by changing your code to the following:

from PIL import Image
import subprocess
import sys

# Print the full path of the Python interpreter you are using
print(sys.executable)

# Print the working directory that your program is running in
subprocess.run('pwd')

# Print directory listing
# WINDOWS VERSION: subprocess.run('DIR')
subprocess.run(['ls', '-l'])

# Open the image
img = Image.open("pic.jpg")

# Check image loaded and its size and type
print(img)

# Display image
img.show()

# Check program ran to completion
print('Done')
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432