使用Python中的Tkinter画布循环显示图像

时间:2017-05-03 19:55:52

标签: python tkinter tkinter-canvas

我正在尝试编写一个脚本,允许用户选择带有图像的文件夹,然后保存用户点击每个图像的坐标。为此,我想在Tkinter画布上显示每个图像文件,保存点击位置,然后当画布关闭时,打开下一个图像。

我可以使用下面的代码(改编自this question)使单个图像工作。我希望for File in imgs循环可以继续打开下一张图像,但事实并非如此。我怀疑我需要一个on_closing函数来告诉Tkinter打开另一个图像。

关闭图像后让Tkinter打开下一张图像的正确方法是什么?

from Tkinter import *
from tkFileDialog import askopenfilenames, askopenfilename, askdirectory
from PIL import Image, ImageTk
import cv2
import numpy as np
import os

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

    #setting up a tkinter canvas with scrollbars
    frame = Frame(width=1920, height=1080, bd=2, relief=SUNKEN)
    frame.grid_rowconfigure(0, weight=1)
    frame.grid_columnconfigure(0, weight=1)
    xscroll = Scrollbar(frame, orient=HORIZONTAL)
    xscroll.grid(row=1, column=0, sticky=E+W)
    yscroll = Scrollbar(frame)
    yscroll.grid(row=0, column=1, sticky=N+S)
    canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
    canvas.config(width=1920, height=1080)
    canvas.grid(row=0, column=0, sticky=N+S+E+W)
    xscroll.config(command=canvas.xview)
    yscroll.config(command=canvas.yview)
    frame.pack(fill=BOTH,expand=1)

    # Function to be called when mouse is clicked
    def save_coords(event):
        coords.append([event.x, event.y])

    # def on_closing():
        # Open the next image file


    # Create empty list for coordinate arrays to be appended to
    coords = []

    # Choose multiple images
    img_dir = askdirectory(parent=root, initialdir="D:/Temp/", title='Choose folder')
    os.chdir(img_dir)
    imgs = os.listdir(img_dir)
    #imgs = askopenfilenames(parent=root, initialdir="D:/Temp/cvCal/", title='Choose images')
    for File in imgs:
        img = ImageTk.PhotoImage(Image.open(File).resize((1280,720), Image.ANTIALIAS))
        canvas.create_image(0,0,image=img,anchor="nw")
        canvas.config(scrollregion=canvas.bbox(ALL))
        canvas.bind("<Button 1>",save_coords)
        # on_closing()...

    root.mainloop()

1 个答案:

答案 0 :(得分:1)

如果你可以使用Label,那将会容易得多:

另外,请注意没有理由使用ImageTk,因为从文件加载图像会作为Tkinter.PhotoImage内置到tkinter中。

另外,我将您的通配符导入转换为正常导入;通配符导入很乱并且违反了PEP8。

最后,在关闭时我不知道你的意思,所以我添加了一个按钮以前进到下一张图片。这将在您需要处理的最后一个图像上抛出StopIteration错误。

import Tkinter as tk
from tkFileDialog import askdirectory
import os

# Create empty list for coordinate arrays to be appended to
coords = []

# Function to be called when mouse is clicked
def save_coords(event):
    click_loc = [event.x, event.y]
    print "you clicked on", click_loc
    coords.append(click_loc)

# Function to load the next image into the Label
def next_img():
    img_label.img = tk.PhotoImage(file=next(imgs))
    img_label.config(image=img_label.img)

root = tk.Tk()

# Choose multiple images
img_dir = askdirectory(parent=root, initialdir="D:/Temp/", title='Choose folder')
os.chdir(img_dir)
imgs = iter(os.listdir(img_dir))

img_label = tk.Label(root)
img_label.pack()
img_label.bind("<Button-1>",save_coords)

btn = tk.Button(root, text='Next image', command=next_img)
btn.pack()

next_img() # load first image

root.mainloop()

print coords