Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11180

How to show PIL images on the screen?

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?

Upvotes: 125

Views: 371534

Answers (8)

Valery Noname
Valery Noname

Reputation: 381

You can use pyplot to show images:

from PIL import Image
import matplotlib.pyplot as plt
im = Image.open('image.jpg')
plt.imshow(im)
plt.show() # image will not be displayed without this

Upvotes: 18

martineau
martineau

Reputation: 123463

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.

Upvotes: 172

pkumar90
pkumar90

Reputation: 71

This is what worked for me:

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

Upvotes: 1

Carson
Carson

Reputation: 7988

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

Upvotes: 1

Hrvoje
Hrvoje

Reputation: 15162

I tested this and it works fine for me:

from PIL import Image
im = Image.open('image.jpg')
im.show()

Upvotes: 35

Apostolos
Apostolos

Reputation: 3445

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.

Upvotes: 5

Feather Feet
Feather Feet

Reputation: 131

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

Upvotes: 11

Puckl
Puckl

Reputation: 741

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

Upvotes: 7

Related Questions