将图像导入tkin

2024-10-17 08:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我现在正在编写一个简单的图像查看器,我有足够的代码来查看这些图像,但是唯一的问题是查看您想要的图像。你必须把它们和脚本放在同一个目录中,然后重命名它们。我希望用户能够单击类似文件打开的东西,然后导入这些图像。我目前正在使用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()  

Tags: imageimportselfimgsizepilopenfloat
1条回答
网友
1楼 · 发布于 2024-10-17 08:32:55

给你-我这样做是为了一个形象,你为所有其他类似的形象

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)。 我已经在上面的代码中做到了。在

相关问题 更多 >