danandu
Posts: 2
Joined: Wed Jun 23, 2021 5:18 am

Display jpg image using Image Viewer python 2.7

Wed Jun 23, 2021 5:29 am

Hi ,
i am unable to open jpg image using python 2.7 ImageViewer

Code: Select all

from PIL import Image
img = Image.open('butterfly1.jpg')
img.show()
please let me know what to do. i am running this code in raspbian raspberry pi 3 B+

gordon77
Posts: 5767
Joined: Sun Aug 05, 2012 3:12 pm

Re: Display jpg image using Image Viewer python 2.7

Wed Jun 23, 2021 5:07 pm

Do you get any error messages ?

klricks
Posts: 7849
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Display jpg image using Image Viewer python 2.7

Wed Jun 23, 2021 6:23 pm

Where is the image file stored?
Try using the full path.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

User avatar
scruss
Posts: 4179
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Display jpg image using Image Viewer python 2.7

Wed Jun 23, 2021 8:53 pm

danandu wrote:
Wed Jun 23, 2021 5:29 am
please let me know what to do. i am running this code in raspbian raspberry pi 3 B+
Hi danandu - welcome to the forum.

You've done nothing wrong here. There's been a package missing from the Raspberry Pi OS desktop forever that stops this from working. You will need a working network connection to fix this Please do this from a terminal:

Code: Select all

sudo apt install -y imagemagick
and try running the code again. It should run this time!

Explanation for nerdy folks: This fails silently on the Desktop because PIL or Pillow has a hidden hard-coded dependency on an image viewer. It looks through your PATH for one of three viewers:
  1. display, from ImageMagick;
  2. eog, from Gnome;
  3. xv, from the primordial mists of time. I'm old enough to remember when the (one) web browser couldn't load JPEGs and shelled out to xv to display them …
Since quite a bit of ImageMagick is already installed, it makes sense to install the binaries. eog would require most of an Gnome installation to run, and xv — though tiny — had a nonfree licence and is probably still showing UNREGISTERED to anyone who'd dare to build it. I bought a licence, once, in 1996, but I'm not sure what happened to it.

How you might diagnose silent problems like this: strace. Calling a program via strace prints every system call and its results to stderr. Yes, that's a lot of output. Here's what you get running strace without ImageMagick installed, edited a lot for length:

Code: Select all

strace python ./show.py
 …
stat64("/usr/local/sbin/display", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
stat64("/usr/local/bin/display", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
stat64("/usr/sbin/display", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
stat64("/usr/bin/display", 0xbeb9a9d0)  = -1 ENOENT (No such file or directory)
stat64("/sbin/display", 0xbeb9a9d0)     = -1 ENOENT (No such file or directory)
stat64("/bin/display", 0xbeb9a9d0)      = -1 ENOENT (No such file or directory)
stat64("/usr/local/games/display", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
stat64("/usr/games/display", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
stat64("/usr/local/sbin/eog", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
 …
stat64("/usr/local/sbin/xv", 0xbeb9a9d0) = -1 ENOENT (No such file or directory)
 …
I edited the PATH walk down a lot, but it looks for display, then eog, then finally xv. Here it finds none of them.

After installing ImageMagick, there's one line that's critically different:

Code: Select all

stat64("/usr/bin/display", {st_mode=S_IFREG|0755, st_size=5596, ...}) = 0
That signals that the binary has been found and all is well.

Update: logged an issue - RPiOS Desktop is missing ImageMagick's display, so PIL.Image.show() Python examples won't work #249
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Return to “Python”