我想从文件夹中加载多个图像并对每个图像执行某些操作(裁剪),关闭后我希望下一个图像加载以进行裁剪等等......但是目前我的代码只加载一个图像,并在其上工作后,它应该关闭,但下一个图像不会加载到画布上。 我认为问题出现在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()
答案 0 :(得分:0)
您的代码中的主要错误是您没有更改变量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/
。因此,如果脚本无法在您的计算机上找到这些文件,您可能需要将这些文件添加回代码。