Reputation: 2861
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.
Upvotes: 51
Views: 82383
Reputation: 1
I use Linux too, and I found a quick solution. I just keep the image viewer opened and then I ran the code and it worked.
Upvotes: 0
Reputation: 56
@Lennart Regebro has already answered the question. I am just adding abit more a Linux Mint 20.3 User:
sudo apt install imagemagick
~/.bashrc
file and save it. (Because, In my case it did't get added to path automatically.)export PATH="$MAGICK_HOME/bin:$PATH"
Check:
Go to any folder that contains an image (say: your_image.png)
run:
display your_image.png
Upvotes: 0
Reputation: 101
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)
Upvotes: 4
Reputation: 79
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
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
Upvotes: 0
Reputation: 28329
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:
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.
Upvotes: 0
Reputation: 384
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).
Upvotes: 4
Reputation: 39
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)
Upvotes: 3
Reputation: 748
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.
Upvotes: 13
Reputation: 21390
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:
sudo apt-get install python-pip
pip install pillow --user
From there, you should be able to install pillow and use it's Image
class in the same manner.
Upvotes: -2
Reputation: 173
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.
Upvotes: 0
Reputation: 172209
It works for me on Ubuntu. It displays the image with Imagemagick. Try this:
sudo apt-get install imagemagick
Upvotes: 59