2

All I'm trying to do is display an image using Pillow on Raspberry Pi 3

First tried using this code:

from PIL import Image 
from PIL import ImageShow

imageA = Image.open('Moth.png')
ImageShow.show(imageA)
print("Done")

It didn't work, no error code, no nothing, it just skipped the code entirely

I digged a bit around and found out that maybe there could be an issue with the image displayer, so I tried adding it in like this:

from PIL import Image 
from PIL import ImageShow

imageA = Image.open('Moth.png')
ImageShow.show(imageA,title=None,command='GPicView')
print("Done")

It didn't work either, I also tried installing fim and using

ImageShow.show(imageA,title=None,command='fim')

like one of the answers on this site suggested, but that didn't work either

I made sure that fim has been installed correctly, so there must be something wrong with the code, but I can't understand what, maybe I didn't import ImageShow the right way?

I also tried using

imageA.show(command='fim')

But it yields the same results

I'm new to coding with Python (and in general), so maybe I'm just doing something stupid without realizing it

David Medinets
  • 5,160
  • 3
  • 29
  • 42
Dubious Shrub
  • 43
  • 1
  • 5

1 Answers1

1

Updated Answer

I think newer versions of PIL/Pillow use the xdg-open command to display images. Internally, PIL/Pillow saves your in-memory image as a PNG file on disk and calls the OS's viewer to view that on-disk PNG. So, I presume there must be a way to set the default viewer for MIME-type "image/png" to be the viewer of your choice, but as a Mac user, I am unsure how you would do that - I guess it is possible with the xdg-mime command.

Original Answer

I think PIL/Pillow works something like this when displaying on Unix/Linux systems:

  • it expects and hopes to find display which is part of ImageMagick
  • it will use eog "Eye of Gnome" if it finds it
  • it will fall back to xv

So, there are a number of possibilities depending on your skill-set, patience, disk-space, desire to use a specific viewer. I don't know those parameters, so here are some possibilities:

Option: Install ImageMagick with:

sudo apt install imagemagick

Option: Install eog with:

sudo apt install eog

Option: Install xv - I don't have the exact command to hand

Option: Install feh or some other viewer and symlink it to display so PIL/Pillow thinks it is using ImageMagick display

sudo apt install feh
sudo ln -s /usr/bin/feh /usr/bin/display

Another option might be to create a custom viewer as a derived class from PIL's UnixViewer that places itself at the top of the list of viewers so it is used first.

So, create a file called "CustomViewer.py" that looks like this - mine uses the feh viewer but you can use any application you like:

#!/usr/bin/env python3

import shutil
import sys

from PIL import Image, ImageShow

class CustomViewer(ImageShow.UnixViewer):
   format = "PNG"
   options = {"compress_level": 1}

   def get_command_ex(self, file, **options):
      command = executable = "feh"
      return command, executable

if shutil.which("feh"):
   print(f'Registering custom viewer for PIL')
   ImageShow.register(CustomViewer, order=-1) # Insert as primary viewer

Then, in your regular Python code where you want to use your custom viewer, just add:

import CustomViewer

and it will output a message saying it is loaded and any subsequent calls to show() will use your custom viewer.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hi! Both the gnome viewer and using your code to trick pillow worked (I used the default image viewer GPicView instead of feh) Would you happen to know a way to change the name on the display window too? Right now it shows the name of the temp image Thank you very much regardless of that ^-^ – Dubious Shrub Feb 22 '22 at 13:41
  • I don't use `GPicView` so I am not the best person to ask. I looked at the manual page and help for `GPicView` and there doesn't appear to be any way to set the title in the display. I notice though that if I have some random image, e.g. `abc123.png` and I create a symbolic link to it with `ln -s abc123.png FreddyFrog.png` I can then use `GPicView FreddyFrog.png` and it will actually show `abc123.png` but with the title `FreddyFrog.png` so you should be able to adapt that concept if you are that keen on `GPicView`. – Mark Setchell Feb 22 '22 at 13:54
  • Thanks! I've not been able to make it work, but I found out that if I open normally the image file I need to be named it will stay named, and it won't be closed when using `os.system("pkill display")` (which is the command I use to close all the images opened by Pillow) because it is opened with the normal GPicView, which works for my project! – Dubious Shrub Feb 25 '22 at 07:29