tags:

views:

41

answers:

1

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()
+1  A: 

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.

Adam Crossland
The picture-viewer shows the message: loading... :( But there isn't any picture.
kame
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
Adam Crossland
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!
kame