Python PIL and Vista/Windows 7 .. show() not working ...

Discussion in 'Python' started by Esmail, Nov 30, 2009.

  1. Esmail

    Esmail Guest

    Hello all.

    I am using the PIL 1.1.6 and Python 2.6.x under XP without any
    problems. However, I can't display any images under Vista
    or Windows 7. I could understand Windows 7 as it's relatively
    new, but Vista has been around for a bit.

    Sample code:

    import Image

    im ='c://mypic.jpg')

    this will work fine under XP, but under Windows 7 and Vista
    the default image viewer will come up with some error message
    that the image can't be found.

    I tried with an external image view program and tried to supply
    it via the command parameter to show - but that too didn't work.

    Definition:, title=None, command=None)

    Any suggestions/help/workarounds? If you can get this to work
    with Vista or Windows 7 I'd love to hear from you.

    Esmail, Nov 30, 2009
    1. Advertisements

  2. Esmail

    Esmail Guest

    sorry, slip of the finger, there's only one forward slash
    or you can use two backward slashes.

    The problem isn't with opening it (I know it opens fine
    since I can get its size attribute via im.size) - the show()
    is the problem.

    Esmail, Nov 30, 2009
    1. Advertisements

  3. Esmail

    Lie Ryan Guest

    What's your default image viewer? is intended to be for
    debugging purpose and may always guaranteed to work if your image viewer
    doesn't support receiving the file through <I don't know how PIL passes
    the image to the program>.
    Lie Ryan, Nov 30, 2009
  4. Esmail

    Esmail Guest

    It's whatever the default windows viewer is :) .. so if I double-
    click on
    the image in the filemanager it fires it up and shows it. This works
    in XP and Windows 7 and Vista (ie double clicking on the image and
    having it display).

    I dug around in the docs and found a named parameter that I can set
    when I
    call show.

    Definition:, title=None, command=None)

    I installed irfanview and specified it/its path in the parameter,
    but that didn't work either. It's really quite puzzling in the
    case of Vista since that's been around for quite a few years now.

    Esmail, Nov 30, 2009
  5. Esmail

    David Bolen Guest

    But I thought everyone was sticking their fingers in their ears and
    humming to try to forget Vista had been released, particularly now
    that Windows 7 is out :)

    Perhaps there's an issue with the temporary file location. I don't
    have a Vista system to test on, but the show() operation writes the
    image to a temporary file as returned by tempfile.mktemp(), and then
    passes the name on to the external viewer. The viewing command is
    handed to os.system() with the filename embedded without any special
    quoting. So if, for example, the temporary location has spaces or
    "interesting" characters, it probably won't get parsed properly.

    One easy debugging step is probably to add a print just before the
    os.system() call that views the image (bottom of _showxv function in in my copy of 1.1.6). That way at least you'll know the
    exact command being used.

    If that's the issue, there are various ways around it. You could
    patch PIL itself (same function) to quote the filename when it is
    constructing the command. Alternatively, the tempfile module has a
    tempdir global you could set to some other temporary directory before
    using the show() function (or any other code using tempfile).

    -- David
    David Bolen, Nov 30, 2009
  6. Esmail

    Esmail Guest

    Thanks for the pointers David, this will give me some things to
    As for me, I'm a long time and regular Linux user with some XP tossed
    I use the PIL under XP at times, but this problem is happening to
    I know who is using both Vista and Windows and can't get the basic
    to work so I am trying to help. (I have access to a Win 7 VM for
    purposes at least).

    If I find a work-around or fix or concrete cause I'll post. In the
    if anyone has any other ideas or fixes/suggestions, please don't be
    shy :)

    Esmail, Nov 30, 2009
  7. Esmail

    Terry Reedy Guest

    How about forget PIL's show and its automagic behavior.
    Use PIL's image save to write the image to a nice, known location, such
    as C:/temp/pic.jgp -- nothing but alphanumerics + ':/.'
    Next test how to run a known external viewer from a command window.
    Then use os.system or subprocess to run it with the same command line.
    Package the two lines as a myshow(params) function.
    Terry Reedy, Nov 30, 2009
  8. Esmail


    Mar 12, 2010
    Likes Received:
    What actually happens is that the Windows code relies on the fact that the default image viewer on Windows XP was able to work at blocking mode - this means that the command will wait until the image window will be closed. Because of that behaviour, they constructed a command line which deletes a file right after the image has been shown.

    Windows Vista doesn't work the same way, it immediately returns once the command has been executed, so what happens now is that the temporary file gets immediately deleted by the command line, and the image viewer doesn't have enough time to load the image before it is being deleted.

    Here is a quick workaround:

    Edit C:\Python26\lib\site-packages\PIL\, and around line 99, replace with the following line:

    return "start /wait %s && PING -n 5 > NUL && del /f %s" % (file, file)
    ronreiter, Mar 12, 2010
    jaimebayes likes this.
  9. Esmail


    Aug 19, 2010
    Likes Received:
    Hey, just wanted to say thanks. I found this via google, finally I can use .show() again.
    Would it be a good idea to post this as bug/fix to the PIL developer's board?
    thkoe002, Aug 19, 2010
  10. Esmail


    Mar 21, 2012
    Likes Received:
    Thanks! works perfectly
    db85, Mar 21, 2012
  11. Esmail


    Mar 26, 2012
    Likes Received:
    This is what I was looking for, from couple of days. Thank you ronreiter......
    joyadiar, Mar 26, 2012
  12. Esmail


    Jun 6, 2014
    Likes Received:
    It works perfectly. thanks a lot
    Arachchi, Jun 6, 2014
  13. Esmail


    Aug 16, 2014
    Likes Received:

    It works for me too, but I was wondering if I need to compile the file, o just save it with changes.

    thanks in advance, Jaime

    jaimebayes, Aug 16, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.