Image.show() won't display the picture

Question:

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.

Asked By: user2131116

||

Answers:

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

sudo apt-get install imagemagick
Answered By: Lennart Regebro

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.

Answered By: Evo510

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.

Answered By: Louis Thibault

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.

Answered By: Clemens Sielaff

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)
Answered By: Cris C

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

Answered By: NoJoshua

What worked for me was:

brew install imagemagick
Answered By: TfromNYC

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.

Answered By: jdhao

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

Answered By: graial

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)
Answered By: Andy Smith

@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
Answered By: Shams E Shifat
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.