How to show PIL images on the screen?

Question:

I am doing some image editing with the PIL libary. The point is, that I don’t want to save the image each time on my HDD to view it in Explorer. Is there a small module that simply enables me to set up a window and display the image?

Answers:

Maybe you can use matplotlib for this, you can also plot normal images with it. If you call show() the image pops up in a window. Take a look at this:

http://matplotlib.org/users/image_tutorial.html

Answered By: Puckl

From near the beginning of the PIL Tutorial:

Once you have an instance of the Image class, you can use the methods
defined by this class to process and manipulate the image. For
example, let’s display the image we just loaded:

     >>> im.show()

Update:

Nowadays the Image.show() method is formally documented in the Pillow fork of PIL along with an explanation of how it’s implemented on different OSs.

Answered By: martineau

If you find that PIL has problems on some platforms, using a native image viewer may help.

img.save("tmp.png") #Save the image to a PNG file called tmp.png.

For MacOS:

import os
os.system("open tmp.png") #Will open in Preview.

For most GNU/Linux systems with X.Org and a desktop environment:

import os
os.system("xdg-open tmp.png")

For Windows:

import os
os.system("powershell -c tmp.png")
Answered By: Feather Feet

You can display an image in your own window using Tkinter, w/o depending on image viewers installed in your system:

import Tkinter as tk
from PIL import Image, ImageTk  # Place this at the end (to avoid any conflicts/errors)

window = tk.Tk()
#window.geometry("500x500") # (optional)    
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()

For Python 3, replace import Tkinter as tk with import tkinter as tk.

Answered By: Apostolos

I tested this and it works fine for me:

from PIL import Image
im = Image.open('image.jpg')
im.show()
Answered By: Hrvoje

You can use pyplot to show images:

from PIL import Image
import matplotlib.pyplot as plt
im = Image.open('image.jpg')
plt.imshow(im)
Answered By: Valery Noname

Yes, PIL.Image.Image.show() easy and convenient.

But if you want to put the image together, and do some comparing, then I will suggest you use the matplotlib. Below is an example,

import PIL
import PIL.IcoImagePlugin
import PIL.Image
import matplotlib.pyplot as plt

with PIL.Image.open("favicon.ico") as pil_img:
    pil_img: PIL.IcoImagePlugin.IcoImageFile  # You can omit. It helps IDE know what the object is, and then it will hint at the method very correctly.
    out_img = pil_img.resize((48, 48), PIL.Image.ANTIALIAS)

    plt.figure(figsize=(2, 1))  # 2 row and 1 column.
    plt.subplots_adjust(hspace=1)  # or you can try: plt.tight_layout()
    plt.rc(('xtick', 'ytick'), color=(1, 1, 1, 0))  # set xtick, ytick to transparent
    plt.subplot(2, 1, 1), plt.imshow(pil_img)
    plt.subplot(2, 1, 2), plt.imshow(out_img)
    plt.show()

enter image description here

Answered By: Carson

This is what worked for me:

roses = list(data_dir.glob('roses/*'))
abc = PIL.Image.open(str(roses[0]))
PIL.Image._show(abc)
Answered By: pkumar90