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 = Image.open('c://mypic.jpg')
    im.show()


    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: im.show(self, 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.

    Thanks!
    Esmail
    Esmail, Nov 30, 2009
    #1
    1. Advertising

  2. Esmail

    Esmail Guest

    >
    >   im = Image.open('c://mypic.jpg')


    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
    Esmail, Nov 30, 2009
    #2
    1. Advertising

  3. Esmail

    Lie Ryan Guest

    On 12/1/2009 5:04 AM, Esmail wrote:
    >>
    >> im = Image.open('c://mypic.jpg')

    >
    > 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.



    What's your default image viewer? im.show 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
    #3
  4. Esmail

    Esmail Guest

    On Nov 30, 3:08 pm, Lie Ryan <> wrote:
    >
    > What's your default image viewer? im.show 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>.



    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: im.show(self, 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
    Esmail, Nov 30, 2009
    #4
  5. Esmail

    David Bolen Guest

    Esmail <> writes:

    > I dug around in the docs and found a named parameter that I can set
    > when I
    > call show.
    >
    > Definition: im.show(self, 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.


    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
    Image.py 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
    #5
  6. Esmail

    Esmail Guest

    On Nov 30, 4:37 pm, David Bolen <> wrote:
    > Esmail <> writes:
    > > I dug around in the docs and found a named parameter that I can set
    > > when I
    > > call show.

    >
    > > Definition:     im.show(self, 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.

    >
    > 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
    > Image.py 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


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

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

    Thanks,
    Esmail
    Esmail, Nov 30, 2009
    #6
  7. Esmail

    Terry Reedy Guest

    Esmail wrote:
    > On Nov 30, 4:37 pm, David Bolen <> wrote:


    >> 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

    >
    > Thanks for the pointers David, this will give me some things to
    > investigate.
    > As for me, I'm a long time and regular Linux user with some XP tossed
    > in.
    > I use the PIL under XP at times, but this problem is happening to
    > someone
    > I know who is using both Vista and Windows and can't get the basic
    > thing
    > to work so I am trying to help. (I have access to a Win 7 VM for
    > testing
    > purposes at least).
    >
    > If I find a work-around or fix or concrete cause I'll post. In the
    > meantime
    > if anyone has any other ideas or fixes/suggestions, please don't be
    > shy :)


    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
    #7
  8. Esmail

    ronreiter

    Joined:
    Mar 12, 2010
    Messages:
    1
    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\ImageShow.py, and around line 99, replace with the following line:

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

    thkoe002

    Joined:
    Aug 19, 2010
    Messages:
    1
    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
    #9
  10. Esmail

    db85

    Joined:
    Mar 21, 2012
    Messages:
    1
    Thanks! works perfectly
    db85, Mar 21, 2012
    #10
  11. Esmail

    joyadiar

    Joined:
    Mar 26, 2012
    Messages:
    1
    Location:
    USA
    This is what I was looking for, from couple of days. Thank you ronreiter......
    joyadiar, Mar 26, 2012
    #11
  12. Esmail

    Arachchi

    Joined:
    Jun 6, 2014
    Messages:
    1
    It works perfectly. thanks a lot
    Arachchi, Jun 6, 2014
    #12
  13. Esmail

    jaimebayes

    Joined:
    Saturday
    Messages:
    1
    Hello

    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 at 9:32 PM
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    5
    Views:
    897
    Bjoern Schliessmann
    Nov 3, 2007
  2. Méta-MCI \(MVP\)

    PIL + show() + Vista

    Méta-MCI \(MVP\), Nov 29, 2007, in forum: Python
    Replies:
    10
    Views:
    2,330
    elizas
    May 7, 2010
  3. David Robinow
    Replies:
    0
    Views:
    636
    David Robinow
    Dec 1, 2009
  4. Sverre
    Replies:
    2
    Views:
    3,540
    Sverre
    Dec 17, 2009
  5. suresh.amritapuri

    PIL show() not working for 2nd pic

    suresh.amritapuri, Jan 8, 2010, in forum: Python
    Replies:
    3
    Views:
    410
    Cousin Stanley
    Jan 9, 2010
Loading...

Share This Page