Python图像显示 [英] Python images display
问题描述
如何在mac上的目录中创建运行图像(1.jpeg-n.jpeg)的python脚本,并在浏览器中显示或通过其他python程序显示?
How can I create a python script that runs through the images (1.jpeg-n.jpeg) in a directory on a mac and displays them in a browser OR via another python program?
我是否将文件导入python而不是在浏览器中显示?
我是否提取文件名1,2,3,4,5并将其添加到列表中,我将其提供给另一个调用浏览器并显示的函数?
Do I import a file to python and than display in browser? Do I extract the file names 1,2,3,4,5 and add that to a list, which I give to another function that calls a browser and displays?
任何帮助都会很棒。
谢谢!
推荐答案
为此目的使用Tkinter和PIL非常简单。将muskies示例添加到此主题的信息中,其中包含此示例:
Using Tkinter and PIL for this purpose is pretty trivial. Add muskies example to the information from this thread that contains this example:
# use a Tkinter label as a panel/frame with a background image
# note that Tkinter only reads gif and ppm images
# use the Python Image Library (PIL) for other image formats
# free from [url]http://www.pythonware.com/products/pil/index.htm[/url]
# give Tkinter a namespace to avoid conflicts with PIL
# (they both have a class named Image)
import Tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
root.title('background image')
# pick an image file you have .bmp .jpg .gif. .png
# load the file and covert it to a Tkinter image object
imageFile = "Flowers.jpg"
image1 = ImageTk.PhotoImage(Image.open(imageFile))
# get the image size
w = image1.width()
h = image1.height()
# position coordinates of root 'upper left corner'
x = 0
y = 0
# make the root window the size of the image
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
# put a button on the image panel to test it
button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')
# save the panel's image from 'garbage collection'
panel1.image = image1
# start the event loop
root.mainloop()
当然,如果你对另一个GUI比较熟悉,那就去吧这个例子,它不应该花费太多。
Of course if you're more familiar with another GUI, go ahead and adapt the example, it shouldn't take much.
这篇关于Python图像显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!