gpt4 book ai didi

python - 将图像导入 tkinter

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:51 25 4
gpt4 key购买 nike

好吧,我目前正在编写一个简单的图像查看器,并且我有足够的代码来查看这些图像,但唯一的问题是查看您想要的图像。您必须将它们放在与脚本相同的目录中并重命名。我希望用户能够单击“文件打开”之类的按钮,然后导入这些图像。我目前使用 Tkinter 作为我的 Gui 和 PIL 来显示图像。这是我最新的代码:

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

最佳答案

给你 - 我对一张图像执行此操作,您对所有其他图像执行类似的操作

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

在评论中提出问题后进行编辑

是的,您可以使用os模块删除文件。首先,在当前命名空间中import os,然后在保存调整大小的图像后,添加一行os.remove(filename)。我已经在上面的代码中做到了这一点。

关于python - 将图像导入 tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com