Python Forum

Full Version: image does not show with pillow
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi everone
? why the image does not show with pillow in python
although i have install the pillow moduel with
pip install pillow
I couldn't get it to work either. Here's the code:
from PIL import Image
my_image = Image.open("image1.PNG")
my_image.show()
Here's the error:
Output:
Traceback (most recent call last): File "tester.py", line 9, in <module> my_image.show() File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2146, in show _show(self, title=title, command=command) File "/usr/lib/python3/dist-packages/PIL/Image.py", line 3085, in _show _showxv(image, **options) File "/usr/lib/python3/dist-packages/PIL/Image.py", line 3091, in _showxv ImageShow.show(image, title, **options) File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 47, in show if viewer.show(image, title=title, **options): File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 67, in show return self.show_image(image, **options) File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 87, in show_image return self.show_file(self.save_image(image), **options) File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 168, in show_file subprocess.Popen(args) File "/usr/lib/python3.8/subprocess.py", line 858, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child self.pid = _posixsubprocess.fork_exec( TypeError: expected str, bytes or os.PathLike object, not NoneType
I'm on Linux Mint 20.3 using Python 3.8.10
Works fine for me using Idle as Python IDE and Ubuntu as OS

from PIL import Image

image = '/home/pedro/Pictures/icebergs_in_barbate.png'
# Opens an image in RGB mode 
im = Image.open(image)      
# Size of the image in pixels (size of original image) 
# Just for information
width, height = im.size
im.show()
Maybe you are not giving the correct path to your image??
Thank you @Pedroski55. I don't think that is the problem. New code:
from PIL import Image
image = './image1.PNG'
# Open an image in RGB mode 
im = Image.open(image)      
# print image attributes
print (im.format, im.size, im.mode)
# shows image was loaded
# now error trying to display image
im.show()
The image seems to be loading correctly but not displaying.
Output:
PNG (480, 360) RGB Traceback (most recent call last): File "tester.py", line 14, in <module> im.show() File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2146, in show _show(self, title=title, command=command) File "/usr/lib/python3/dist-packages/PIL/Image.py", line 3085, in _show _showxv(image, **options) File "/usr/lib/python3/dist-packages/PIL/Image.py", line 3091, in _showxv ImageShow.show(image, title, **options) File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 47, in show if viewer.show(image, title=title, **options): File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 67, in show return self.show_image(image, **options) File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 87, in show_image return self.show_file(self.save_image(image), **options) File "/usr/lib/python3/dist-packages/PIL/ImageShow.py", line 168, in show_file subprocess.Popen(args) File "/usr/lib/python3.8/subprocess.py", line 858, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child self.pid = _posixsubprocess.fork_exec( TypeError: expected str, bytes or os.PathLike object, not NoneType
It knows that the image is 480/360 RGB but will not display it.
(Apr-13-2025, 07:32 PM)BashBedlam Wrote: [ -> ]It knows that the image is 480/360 RGB but will not display it.
Error you see when calling im.show() indicates that the external image viewer that pillow trying to use isn’t set up properly.
In other words, one of the arguments being passed to the system’s subprocess call is coming out as None.

Your system might not have a default image viewer installed (or it isn’t available in your PATH).
For instance, Linux systems often rely on utilities like display (from ImageMagick) or xdg-open.

Can instal a viewer eg sudo apt-get install imagemagick
You can force Pillow to use a specific viewer by setting the environment variable before running your script:
import os
from PIL import Image

os.environ["PIL_IMAGE_SHOW"] = "/usr/bin/display"  # Adjust this path as needed
image = './image1.PNG'
im = Image.open(image)
print(im.format, im.size, im.mode)
im.show()