Bar*_*ski 59 python image python-imaging-library
我正在使用PIL库进行一些图像编辑.关键是,我不想每次都将图像保存在硬盘上以便在资源管理器中查看.是否有一个小模块可以让我设置一个窗口并显示图像?
mar*_*eau 79
从PIL教程:
获得Image类的实例后,可以使用此类定义的方法来处理和操作图像.例如,让我们显示刚刚加载的图像:
>>> im.show()
小智 15
您可以使用 pyplot 来显示图像:
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
Run Code Online (Sandbox Code Playgroud)
我测试了一下,对我来说很好用:
from PIL import Image
im = Image.open('image.jpg')
im.show()
Run Code Online (Sandbox Code Playgroud)
也许你可以使用matplotlib,你也可以用它绘制普通图像.如果你调用show(),图像会弹出一个窗口.看看这个:
http://matplotlib.org/users/image_tutorial.html
小智 5
如果您发现PIL在某些平台上有问题,则使用本机图像查看器可能会有所帮助。
img.save("tmp.png") #Save the image to a PNG file called tmp.png.
Run Code Online (Sandbox Code Playgroud)
对于MacOS:
import os
os.system("open tmp.png") #Will open in Preview.
Run Code Online (Sandbox Code Playgroud)
对于大多数具有X.Org和桌面环境的GNU / Linux系统:
import os
os.system("xdg-open tmp.png")
Run Code Online (Sandbox Code Playgroud)
import os
os.system("powershell -c tmp.png")
Run Code Online (Sandbox Code Playgroud)
您可以使用 Tkinter 在您自己的窗口中显示图像,无需依赖于系统中安装的图像查看器:
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()
Run Code Online (Sandbox Code Playgroud)
对于 Python 3,替换import Tkinter as tk
为import tkinter as tk
.
归档时间: |
|
查看次数: |
139242 次 |
最近记录: |