0

This code didn't show my picture. The picture really exists :) Does anybody know why this doesnt work? Thanks in advance!

from PIL import Image
im = Image.open("D:\\Python26\\PYTHON-PROGRAMME\\bild.jpg")
im.show()
4
  • What was the error message? Do you have a stacktrace? Commented Dec 30, 2009 at 17:30
  • there isn't any message in IDLE. The picture-viewer shows the message: picture is deleted or moved. But I think this is wrong. What is stacktrace? Commented Dec 30, 2009 at 17:33
  • Is this the stacktrace? [('<string>', 1, '<module>', None), ('D:\\Python26\\lib\\idlelib\\run.py', 93, 'main', 'ret = method(*args, **kwargs)'), ('D:\\Python26\\lib\\idlelib\\run.py', 293, 'runcode', 'exec code in self.locals'), ('D:\\Python26\\PYTHON-PROGRAMME\\00000000000000000', 11, '<module>', 'x()'), ('D:\\Python26\\PYTHON-PROGRAMME\\00000000000000000', 5, 'x', 'print traceback.extract_stack()')] Commented Dec 30, 2009 at 17:41
  • 1
    Which version of Windows are you using? This works for me in XP, but looking at the implementation in Image.py, I feel that it's method of showing the image is brittle and may have broken in Vista/7 Commented Dec 30, 2009 at 17:56

1 Answer 1

1

You probably need to call the load() method to force the open() method to do its work. open is lazy.

Try:

from PIL import Image
im = Image.open("D:\\Python26\\PYTHON-PROGRAMME\\bild.jpg")
im.load()
im.show()

Idea #2: Patch PIL's file Image.py to have a potentially more robust approach to using the Windows shell to display your image. In the method _showxv, replace the following lines:

if os.name == "nt":
    command = "start /wait %s && del /f %s" % (file, file)

with

if os.name == "nt":
    command = "%s" % file

I suspect that the problem with the existing implementation is that the del command after the && is running immediately after the start command rather than after the result of the start command finishes. Thus, the file has already been deleted by the time that the image viewer is ready to load and display it.

Do back up your copy of the code before patching it.

Sign up to request clarification or add additional context in comments.

3 Comments

The picture-viewer shows the message: loading... :( But there isn't any picture.
I wouldn't fret too much about how well the show() method works. It is intended for debugging purposes, and based on the documentation's explanation of how it works (convert to a bitmap, save the bitmap to a temporary file, use default system viewer to show the temporary file) there are too many places where it could fail to be able to fix this. Instead, consider performing some transformations on the Image and then saving it to a new location. Open the new file manually. After all, you are using PIL to change an image in some way, right? You're not primarily using it simply to display images
i didn`t want to change os.name now, because actually i didn't what it will change. for now I change the image, and save it under an other name. after this, I watch it with some picture-viewer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.