Unable to show an image using python PIL Image.show

10,685

The documentation says:

On Windows, it [show()] saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.

Problem is that your program exits immediately somehow, the temporary file is deleted upon exit and Windows etc. cannot find it. As a temporary solution, try adding:

import time
# Your code as above
time.sleep(30)

This will make the program wait 30 seconds before exiting. If you prefer, you could make it wait the user to press a key.

EDIT: it seems like you are experiencing problems with temporary files. As a workaround, save the image somewhere on the disk using, say, img.save("C:\Users\User\Pictures\test.jpg") and open it with your favorite image viewer. Whenever you want to show the processed image, call save again and reload the picture in the image viewer.

Share:
10,685
user2250337
Author by

user2250337

Updated on June 05, 2022

Comments

  • user2250337
    user2250337 almost 2 years

    I'm using the Python Imaging Library and I am unable to open an image successfully in Windows Live Photo Gallery. There is a message that shows up saying "There are no photos or videos selected" instead of the image.

    This is what I've tried:

    import Image
    
    img = Image.open(r"C:\Users\User\Pictures\image.jpg")
    img.show()
    

    This is pretty much the same as in the PIL handbook tutorial, so I'm not sure where I'm going wrong.

  • user2250337
    user2250337 almost 11 years
    I'm getting a different message now: "Photo Gallery can't open this photo or video because it might have been deleted or is in a location that is not available".
  • Stefano Sanfilippo
    Stefano Sanfilippo almost 11 years
    Open IDLE, enter python statements there and let me know if it works.
  • user2250337
    user2250337 almost 11 years
    Heya, I've just tried it in IDLE and the same thing is happening.
  • Stefano Sanfilippo
    Stefano Sanfilippo almost 11 years
    Try replacing img.show() with img.save("C:\Users\User\Pictures\image.testcopy.jpg") and see if the other image is created correctly...
  • Stefano Sanfilippo
    Stefano Sanfilippo almost 11 years
    @user2250337 Ok, probably it's not worth loosing time to understand why temporary files do not work as expected. Added workaround in the edit.
  • user2250337
    user2250337 almost 11 years
    Thanks for all the help, very much appreciated :)
  • PM 2Ring
    PM 2Ring over 9 years
    Thanks heaps, @StefanoSanfilippo ! Adding a delay (or waiting for user input) lets me use my zbar-based QR code display script (which uses PIL for display & image conversion) in a KDE desktop script. Without a delay the image.show() window opens but then immediately closes, even though KDE keeps the script's terminal open.