+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

使用tkinter在画布中一个接一个地加载多个图像

发布于2020-08-04 18:33     阅读(942)     评论(0)     点赞(25)     收藏(4)


我想从一个文件夹中加载多个图像,并对每个图像进行某些操作(裁剪),然后关闭后,我要加载下一个图像以进行裁剪等……但是目前我的代码仅加载一个图像并经过处理后,它会按预期关闭,但下一张图像不会加载到画布上。我认为问题出在'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()

解决方案


代码中的主要错误是您没有更改变量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/从文件名中删除因此,如果脚本无法在您的计算机上找到文件,则可能需要将这些文件重新添加到代码中。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/471385/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

25 0
收藏该文
已收藏

评论内容:(最多支持255个字符)