48

I'm running a python script but it won't show the picture.

I have an apple.jpg image in a directory with this program and it should show the picture, but it doesn't. Here is the code:

#!/usr/bin/env python

from PIL import Image

Apple = Image.open("apple.jpg")
Apple.show()

My OS is Ubuntu and this program just gets completed without showing any error.

Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
user2131116
  • 2,761
  • 6
  • 26
  • 33
  • 5
    Most likely this is because you lack a default image-viewer in your OS assuming you're running linux. PIL heavily depends on your OS to "render" your image. See if this is a better aproach if you just want to view an image and access the image data: http://stackoverflow.com/questions/11390596/how-to-display-image-in-pygame – Torxed Apr 29 '13 at 13:11

11 Answers11

57

It works for me on Ubuntu. It displays the image with Imagemagick. Try this:

sudo apt-get install imagemagick
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
13

I know, it's an old question but here is how I fixed it in Ubuntu, in case somebody has the same problem and does not want to install imagemagick (which does not fix the root cause of the problem anyway).

The default viewer on Ubuntu can be started using the command "eog" in the terminal. Pillow, by default, searches only for the commands "xv" and "display", the latter one being provided by imagemagick. Therefore, if you install imagemagick, calling "display" will indeed open up the image. But why not use the viewer that we already have?

The Python code to open the viewer can be found in lib/python3.4/site-packages/PIL/ImageShow.py (or the equivalent of your Python installation). Scroll down to below line# 155 and find the code block saying:

class DisplayViewer(UnixViewer):
    def get_command_ex(self, file, **options):
        command = executable = "display"
        return command, executable

if which("display"):
    register(DisplayViewer)

Copy that block and paste it right underneath, changing the "display" command to Ubuntu's "eog" command:

class DisplayViewer(UnixViewer):
    def get_command_ex(self, file, **options):
        command = executable = "eog"
        return command, executable

if which("eog"):
    register(DisplayViewer)

After saving ImageShow.py, Pillow should correctly show() images in the default viewer.

Clemens Sielaff
  • 708
  • 10
  • 15
  • 2
    This worked perfectly! So why was it set to "display" in the first place? Having made this change to the file, will something else possibly not work later, as it was expecting "display" and not "eog"? – Rafael_Espericueta Jun 19 '16 at 01:24
  • 1
    Excellent. Personally I think this is better than the accepted answer, I prefer `eog` also. I don't suppose you ever found a way that didn't involve changing the source-code? [PIL image.show](http://pillow.readthedocs.io/en/3.2.x/reference/Image.html#PIL.Image.Image.show) makes it sound like it should be an option (but I couldn't get it to work). Also, @Rafael_Espericueta - if I had to guess, the PIL developers probably preferred bringing up `ImageMagick`, since it's got editing capabilities, unlike `eog`. After all, _image.show()_ was "mainly intended for debugging purposes." – John C Jun 22 '16 at 18:34
4

This is an old question, however, it has bugged me for a while. This is my solution:

I run Python 2.7 on Linux Mint and when calling Image.show() nothing happens. On my system, the default viewer is "Image Viewer", which is called "eog" in bash. My Python thinks that I use something else maybe "xv". So, open a terminal and run

sudo gedit /usr/lib/python2.7/dist-packages/PIL/ImageShow.py

Search for "xv" and replace it with "eog".

Save the file and Image.show() works fine (for debugging purposes).

NoJoshua
  • 374
  • 3
  • 11
4

I was in a similar situation and Ipython.display() worked well, especially in Google Colab. "img" is a PIL image.

import IPython.display as display
display.display(img)
Andy
  • 91
  • 3
3

I hope what I found here can be helpful to someone, it worked for me. It explains that when the default viewer does not accept command line image files, you can use the webbrowser module to show the image:

import Image

# Apple = Image.open("apple.jpg")    
# Apple.show()
# instead of using .open and .show(), 
# save the image to show it with the webbrowser module

filename = "apple.jpg"
import webbrowser
webbrowser.open(filename)
Cris C
  • 39
  • 3
  • +1 because this uses the filename as the title for the viewer window unlike imagemagick that uses some tmp file gibberish name. Seeing the filename was something I kind of needed.. So even that imagemagick worked for me this solved another issue. – Gil-Mor Apr 21 '17 at 15:22
2

What worked for me was:

brew install imagemagick
TfromNYC
  • 41
  • 3
0

It may be that you don't have a default image viewer set. Try opening the image file outside of the program and see if it asks you to select a program or just opens in another program.

Evo510
  • 173
  • 1
  • 1
  • 15
  • A good guess, but PIL doesn't seem to use the default image viewer. It *should* but does not. At least not for a useful definition of "Default". :) – Lennart Regebro Apr 29 '13 at 13:41
  • I do this before , the picture can be open and see , but if I perform this program , it still not working . – user2131116 Apr 29 '13 at 13:43
0

In my case, I am using Ubuntu on Windows and can not display image when using Pillow.

There are two things we need to do:

  1. Install a X server on your Windows, for example, xming
  2. Install an image viewer in Ubuntu on Windows (not on Windows, this is important!), for example, imagemagick, as suggested by @Lennart Regebro.

Then add the following setting to your .bashrc and source it:

export DISPLAY=:0

Now, you should be able to see the image displayed successfully.

jdhao
  • 24,001
  • 18
  • 134
  • 273
0

The default viewer for Mint19.3 is xviewer (I don't know if that's true for Ubunutu) You can confirm for your system by typing: xviewer /path/to/test_image.jpg the viewer should launch

while I'm using xviewer as an example, this approach should work for any viewer if you know the command to launch it from the command line

by typing which xviewer, I know that xviewer is located at: /usr/bin/xviewer

  • if you are on this page, I presume display is currently unallocated, you can confirm that by checking that which diplay returns nothing. If it returns a path, then you may want to check that to ensure another installed program is not using that command for a different purpose.

To assign a new to python (without changing source code) you can then use a symbolic link to assign display to xviewer:

sudo ln -T /usr/bin/xviewer /usr/bin/display

graial
  • 79
  • 8
0

@Lennart Regebro has already answered the question. I am just adding abit more a Linux Mint 20.3 User:

  1. Install Imagemagick.
sudo apt install imagemagick
  1. Add the following line at the end of your ~/.bashrc file and save it. (Because, In my case it did't get added to path automatically.)
export PATH="$MAGICK_HOME/bin:$PATH"

3. source ~/.bashrc file or simply restart your terminal.

Check: Go to any folder that contains an image (say: your_image.png) run:

display your_image.png
-2

PIL is pretty much abandonware at this point. You should consider using it's successor pillow, which can be downloaded via easy_install or pip.

On ubuntu, I'd suggest the following:

  1. If you don't yet have pip, install it through apt-get: sudo apt-get install python-pip
  2. Install pillow: pip install pillow --user

From there, you should be able to install pillow and use it's Image class in the same manner.

Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
  • Pillow doesn't behave differently in this case. – Lennart Regebro Apr 29 '13 at 14:08
  • @LennartRegebro, Are you speaking for OP? I'm afraid I don't follow. Image display problems of various sorts are often resolved by switching to pillow. – Louis Thibault Apr 29 '13 at 14:12
  • It's not an opinion, so I don't see how "speaking for" applies here. This will not be resolved by switching to Pillow. – Lennart Regebro Apr 29 '13 at 14:39
  • @LennartRegebro, I beg to differ. I had a similar problem to OP's (if not an identical one) that *was* resolved by installing pillow. That's why your response confused me. – Louis Thibault Apr 29 '13 at 14:45
  • 3
    Well first of all, because I'm using Pillow, and can confirm the behavior, and secondly because my suggestion worked. It's not a tricky issue. Pillow doesn't behave differently from how the OP described the issue. For all you know he might be using Pillow already. – Lennart Regebro Apr 29 '13 at 19:53
  • Switching to pillow does not work. Pillow also tries to the system default image viewer. – jdhao Feb 16 '19 at 06:09