14

I would like to show an image using python on windows and other platforms. When I do:

from PIL import Image
im = Image.open('image.png')
im.show()

my default viewer opens up and tells me that Windows Photo Viewer can't open this picture because either this file was deleted , etc.

The file is probably deleted because PIL calls the os with the following command: "start /wait %s && del /f %s" % (file, file)

I found a workaround here. They recommend changing PIL's code to "start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s" % (file, file). However, I want others to be able to use my code.

Is there a simple solution? Should I look for an alternative to PIL that would work crossplatform?

cyborg
  • 9,989
  • 4
  • 38
  • 56
  • 1
    Ok, found a solution [here](http://www.daniweb.com/software-development/python/threads/308081): `import webbrowser` `webbrowser.open('image.png')` It opens the default viewer, not the browser, on my machine. – cyborg Oct 10 '11 at 16:07
  • please post that as an answer (and accept it), so others that come here this question can find it easily. – Petr Viktorin Oct 10 '11 at 16:21
  • @Petr Viktorin. This requires 100 reputation, or I have to wait 8 hours... – cyborg Oct 10 '11 at 16:22
  • Well, you do have 100 reputation ;) – Petr Viktorin Oct 10 '11 at 16:28
  • Microsoft broke the "start" command. It always WANTED you to specify a window title first. `start /wait "Title" %s ....` is the correct Windows syntax. THe latest GitHub Pillow code contains this fix. – Warren P Jan 15 '14 at 21:53

3 Answers3

12

Ok, found a solution here:

import webbrowser
webbrowser.open('image.png')

It opens the default viewer, not the browser, on my machine.

Also, there is os.startfile.

cyborg
  • 9,989
  • 4
  • 38
  • 56
  • 2
    how does that help display the (possibly modified) `im` object/image as was previously possible with `im.show()`? What if I want to display `im`. I guess you can save it to some temporary filename and then use that .. – Levon Jun 16 '12 at 16:18
2

If you want it to be opened by MS paint only, you can use:

start /wait mspaint %s && del /f %s" % (file, file)

The /wait is not having any effect when the file name is specified directly.

Pierre GM
  • 19,809
  • 3
  • 56
  • 67
  • 1
    If you want to make the above change, then edit: C:\Python26\lib\site-packages\PIL\ImageShow.py around line 99, replace 'return "start /wait %s && del /f %s" % (file, file)' with 'return "start /wait mspaint %s && del /f %s" % (file, file)' thanks @NikhilGeorge for the mspaint suggestion! – GnomeDePlume Apr 10 '13 at 07:54
  • Or just fix the real problem. Add "WindowTitle" to "start" command as the first parameter. Be sure to INCLUDE the quotes. – Warren P Jan 15 '14 at 21:54
0

When Photo Viewer does appear, Go to menu on top ->Open->Choice Program-> Select Paint
On Spanish -> Abrir-> Elegir Programa -> seleccionar Paint

image reference: image

Pregunton
  • 77
  • 7