[英]Import Image into tkinter
Ok so I'm currently writing a simple image viewer and I have enough code to be able to view those images, but the only problem is to view the images you want. 好的,所以我目前正在编写一个简单的图像查看器,并且我有足够的代码来查看那些图像,但是唯一的问题是查看所需的图像。 You have to put them in the same directory as the script and rename them.
您必须将它们与脚本放在同一目录中,然后重命名它们。 I want the user to be able to click something like file-open and then import those images.
我希望用户能够单击诸如打开文件之类的内容,然后导入那些图像。 I'm currently using Tkinter as my Gui and PIL for displaying the images.
我目前正在使用Tkinter作为Gui和PIL来显示图像。 here is my latest code:
这是我的最新代码:
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style
import os
import PIL
import Tkinter
filename = "test.jpg"
filename2 = "test1.jpg"
filename3 = "test2.jpg"
filename4 = "test3.jpg"
basewidth = 300
img = Image.open(filename)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize.jpg')
basewidth = 300
img = Image.open(filename2)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize2.jpg')
basewidth = 300
img = Image.open(filename4)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize4.jpg')
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Picture")
self.pack(fill=BOTH, expand=1)
Style().configure("TFrame", background="")
image1 = Image.open("resize.jpg")
bardejov = ImageTk.PhotoImage(image1)
label1 = Label(self, image=bardejov)
label1.image = bardejov
label1.place(x=5, y=5)
image2 = Image.open("resize2.jpg")
bardejov = ImageTk.PhotoImage(image2)
label1 = Label(self, image=bardejov)
label1.image = bardejov
label1.place(x=5, y=250)
image3 = Image.open("resize3.jpg")
bardejov = ImageTk.PhotoImage(image3)
label1 = Label(self, image=bardejov)
label1.image = bardejov
label1.place(x= 350, y=5)
image3 = Image.open("resize4.jpg")
bardejov = ImageTk.PhotoImage(image3)
label1 = Label(self, image=bardejov)
label1.image = bardejov
label1.place(x= 350, y=250)
def main():
root = Tk()
root.geometry("660x488")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Here you go - I do it for one image, you do it for all others similarly 在这里-我对一张图片进行处理,对所有其他图片进行处理
import tkFileDialog
from Tkinter import *
from PIL import Image
import os
root= Tk()
def resizeIt():
filename = tkFileDialog.askopenfilename()
basewidth = 300
img = Image.open(filename)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
img.save('resize.jpg')
os.remove(filename) # deletes the original image after you have got the resized image
Button(text='add image', command=resizeIt).pack()
root.mainloop()
Edit after your question in comment 在评论您的问题后进行编辑
Yes you can delete a file using the os
module. 是的,您可以使用
os
模块删除文件。 First you import os
in the current namespace and then after you have saved the resized image, you add a line os.remove(filename)
. 首先,在当前名称空间中
import os
,然后在保存调整大小后的图像之后,添加一行os.remove(filename)
。 I have done that in the above code. 我已经在上面的代码中做到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.