【问题标题】:Load multiple images one after another in a canvas with tkinter使用 tkinter 在画布中依次加载多个图像
【发布时间】:2018-11-13 11:13:16
【问题描述】:

我想从一个文件夹中加载多个图像并对每个图像执行某些操作(裁剪),关闭后我希望加载下一个图像以进行裁剪等等......但目前我的代码只加载一个图像并在处理后按预期关闭,但下一个图像不会加载到画布上。 我认为问题出在“load_image”函数或对该函数的调用中。

from Tkinter import *
from PIL import Image, ImageTk
import os

array = []
tuplee = ()
count = 0
d = 0
f = 1
FileDir = ""

#Load all files from directory
File = os.listdir('frames/')
#print(File)

if __name__ == "__main__":
    root = Tk()

#setting up a tkinter frame and canvas
frame = Frame(root, bd=2, relief = SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

canvas = Canvas(frame, bd=1, width=950, height=600)
canvas.grid(row=0, column=0, sticky=N+S+E+W)

frame.pack(fill=BOTH,expand=1)

FileDir = 'C:/Users/cvcrb/Desktop/NumberPlate/frames/' + File[0]
img = ImageTk.PhotoImage(Image.open(FileDir))
canvas.create_image(0,0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL))

def load_image():
    FileDir = 'C:/Users/cvcrb/Desktop/NumberPlate/frames/' + File[f]
    img = ImageTk.PhotoImage(Image.open(FileDir))
    canvas.create_image(0,0,image=img,anchor="nw")
    canvas.config(scrollregion=canvas.bbox(ALL))

    #root.update_idletasks()
    #root.after(100,load_image)

#function to be called when mouse is clicked
def printcoords(event):
    global count
    global d
    global FileDir
    global f
    for i in range(1):
        array.append(event.x)
        array.append(event.y)
        tuplee = tuple(array)
        print (tuplee)
        count += 1
        if count == 2:                  
            print ("Cropping...")
            crop(FileDir, tuplee, 'cropped%d.jpg' % d)
            d += 1
            print ("Cropped")
            count = 0
            canvas.destroy()            #canvas closes after 2 clicks
        load_image()                    #should load the next image 

#mouseclick event
canvas.bind("<Button 1>",printcoords)



#function to crop a certain area using coordinates
def crop(image_path, coords, saved_location):
    image_obj = Image.open(image_path)
    cropped_image = image_obj.crop(coords)
    cropped_image.save(saved_location)
    image_obj.close()


root.mainloop()

【问题讨论】:

    标签: python user-interface canvas tkinter python-imaging-library


    【解决方案1】:

    您的代码中的主要错误是您没有更改变量f,因此您的load_image() 只是一遍又一遍地加载相同的图像。

    第二个主要问题是你不需要做canvas.destroy()。在创建新图像之前执行canvas.delete("all") 就足够了。

    这是您修改后的代码,对我来说效果很好:

    from Tkinter import *
    from PIL import Image, ImageTk
    import os
    
    array = []
    tuplee = ()
    f = 1
    FileDir = ""
    
    #Load all files from directory
    File = os.listdir('frames/')
    #print(File)
    
    if __name__ == "__main__":
        root = Tk()
    
    #setting up a tkinter frame and canvas
    frame = Frame(root, bd=2, relief = SUNKEN)
    frame.grid_rowconfigure(0, weight=1)
    frame.grid_columnconfigure(0, weight=1)
    
    canvas = Canvas(frame, bd=1, width=950, height=600)
    canvas.grid(row=0, column=0, sticky=N+S+E+W)
    
    frame.pack(fill=BOTH,expand=1)
    
    FileDir = 'frames/' + File[0]
    img = ImageTk.PhotoImage(Image.open(FileDir))
    cimg = canvas.create_image(0,0,image=img,anchor="nw")
    canvas.config(scrollregion=canvas.bbox(ALL))
    
    def load_image():
        global canvas, img, FileDir
        FileDir = 'frames/' + File[f]
        canvas.delete("all")
        #del img
        #del canvas
        #canvas = Canvas(frame, bd=1, width=950, height=600)
        #canvas.grid(row=0, column=0, sticky=N+S+E+W)
        img = ImageTk.PhotoImage(Image.open(FileDir))
        canvas.create_image(0,0,image=img,anchor="nw")
        canvas.config(scrollregion=canvas.bbox(ALL))
    
        #root.update_idletasks()
        #root.after(100,load_image)
    
    #function to be called when mouse is clicked
    def printcoords(event):
        global FileDir
        global f
        global array
        array.append(event.x)
        array.append(event.y)
        tuplee = tuple(array)
        print (tuplee)
        if len(tuplee)==4:
            print ("Cropping...")
            crop(FileDir, tuplee, 'cropped%d.jpg' % f)
            print ("Cropped")
            array = []
            f += 1
            load_image()                    #should load the next image
    
    #mouseclick event
    canvas.bind("<Button 1>",printcoords)
    
    
    
    #function to crop a certain area using coordinates
    def crop(image_path, coords, saved_location):
        image_obj = Image.open(image_path)
        cropped_image = image_obj.crop(coords)
        cropped_image.save(saved_location)
        image_obj.close()
    
    
    root.mainloop()
    

    注意:我从文件名中删除了C:/Users/cvcrb/Desktop/NumberPlate/。因此,如果脚本在您的机器上找不到文件,您可能需要将这些文件添加回代码中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多