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?
-
Display the image how? In a browser?– user554546Commented Sep 24, 2012 at 18:38
-
in a python made window. Something like a window from pygame. I could do it in pygame, but i would like a better solution.– Bartlomiej LewandowskiCommented Sep 24, 2012 at 18:40
-
1why dont you want to save it?– Joran BeasleyCommented Sep 24, 2012 at 18:41
-
1lets say i have 100 small pictures to look through. It would be better if i could have all of them printed on my screen, without having to open them in some other program. I am not really familiar with wxpython. Is it simple?– Bartlomiej LewandowskiCommented Sep 24, 2012 at 18:44
-
1You're going to need some windowing library—whether pygame, wxPython, TkInter, PyQt, PyGtk, PyWin32, PyObjC, etc.—to open a window. Once you've got a window open, most of them make it trivial to display an image in that window.– abarnertCommented Sep 24, 2012 at 18:55
8 Answers
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.
-
it is actually what i was looking for, but i get a windows live photo gallery error. 0x800706ba Commented Sep 24, 2012 at 19:25
-
3That's because Windows live photo gallery is associated with the type of temporary file PIL created when you called the method -- which probably defaults to .bmp on Windows which doesn't support many of the capabilities of modern image file formats. Try setting the image format attribute explicitly to .png (i.e.
im.format = "PNG"
) before theim.show()
call. Commented Sep 24, 2012 at 19:38 -
still the problem persists. I might actually try a window libary. Commented Sep 24, 2012 at 19:42
-
3PIL's built-in
show()
method is the simplest possible version of the "script some other program to show it". (On earlier Windows systems you'd end up with a whole bunch of MSPaint windows.) It was worth suggesting and trying even if it didn't work. You could actually do something similar, and almost as simple, by, say, explicitly launching MSPaint.exe on each image…– abarnertCommented Sep 24, 2012 at 21:19 -
3@abarnert: Actually the problem sounds like a bug in Windows Live Photo Gallery 2011 and there's a newer version that can be download that may fix the PIL
show()
problem. Commented Sep 24, 2012 at 21:33
I tested this and it works fine for me:
from PIL import Image
im = Image.open('image.jpg')
im.show()
-
2While this may answer the question (untested) it is best practice to provide an explanation with your code on SO. It is actually discouraged to give code only answers. Commented Mar 14, 2019 at 20:11
-
2
-
3@kawingkelvin Question was "How to show PIL images on the screen?". You should seek answer for your specific situation elsewhere...perhaps here: stackoverflow.com/questions/55288657/…– HrvojeCommented Mar 19, 2020 at 5:36
-
1
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
-
1Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.– Tyler2PCommented May 1, 2021 at 15:11
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")
import os
os.system("powershell -c tmp.png")
-
Feather, on MacOS, it won't necessarily open in Preview, but in the default for the file type. I was working with some large jpg files, which Preview converts to .png, taking about 20 seconds. Changing the jpg default to Chrome worked wonders... Commented Jul 27, 2020 at 5:42
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:
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
.
-
4Where did you see that the code involves saving the image to disk??? The question asks for a way to edit an image w/o having to save it on disk each time. But you must open the image from disk at least once in the beginning. Then you can process and view the image loaded in memory as 'img' as many times you like w/o involving any disk operation. Commented Oct 26, 2018 at 8:36
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()
This is what worked for me:
roses = list(data_dir.glob('roses/*'))
abc = PIL.Image.open(str(roses[0]))
PIL.Image._show(abc)