Posts: 2
Threads: 1
Joined: Apr 2025
Apr-13-2025, 06:29 AM
(This post was last modified: Apr-13-2025, 05:47 PM by buran.)
hi everone
? why the image does not show with pillow in python
buran write Apr-13-2025, 05:47 PM:Please, use descriptive title
Larz60+ write Apr-13-2025, 09:31 AM:Rather than attaching code, please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Attached Files
Thumbnail(s)
Posts: 2
Threads: 1
Joined: Apr 2025
although i have install the pillow moduel with
pip install pillow
Posts: 379
Threads: 2
Joined: Jan 2021
Apr-13-2025, 12:22 PM
(This post was last modified: Apr-13-2025, 12:28 PM by BashBedlam.)
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
Attached Files
Thumbnail(s)
Posts: 1,093
Threads: 143
Joined: Jul 2017
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??
Posts: 379
Threads: 2
Joined: Jan 2021
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.
Posts: 7,315
Threads: 123
Joined: Sep 2016
(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()
Gribouillis likes this post
|