-1

Suppose I have 'n' image files in a directory filedir which I want to view via tkinter console. Now, I want to skip a few files whenever necessary, using a button click event which invokes a function nextFile().

E.g.

import os


def nextFile():
    global img
    img_2 = process(img)
    return img_2


window = tk.Tk()
window.title("File Viewer")
files = os.listdir(filedir)
button1 = tk.Button(window, text="Browse Files...", fg="black", command=askopenfilename)
button2 = tk.Button(window, text="SELECT", width=50, command=nextFile)
canvas = tk.Canvas(window, width=width, height=height)
button1.pack(side=tk.LEFT)
button2.pack(side=tk.BOTTOM)
canvas.pack()

for f in files:
    img = cv2.cvtColor(cv2.imread(filedir + '/' + f), cv2.COLOR_BGR2RGB)
    photo = ImageTk.PhotoImage(image=Image.fromarray((img))
    canvas.create_image(0, 0, image=photo, anchor=tk.CENTER)

window.mainloop() 

Any help is appreciated. Thanks!

Dr. Strange
  • 87
  • 11
  • You do not need `return` in your function. Also this function creates a new instance of `tk` every time it is called. I would make a few changes. That or your indention is messed up. You name your root window `my_window` then tell your widgets to be placed on `window`. This will not work and should error. – Mike - SMT Feb 17 '20 at 20:18
  • Sorry about the indentation. I edited it now. – Dr. Strange Feb 17 '20 at 20:22
  • `command=askopenfilename` wont do you any good. A button cannot do anything with the value that is returned from any function/method. – Mike - SMT Feb 17 '20 at 20:25
  • I need to load images recursively from the folder and do the skipping whenever necessary. – Dr. Strange Feb 17 '20 at 20:27
  • What you could do is load all the file names from that directory into a list. Then build a function that loads the image on click based on the list index. – Mike - SMT Feb 17 '20 at 20:28
  • The "files" variable has the list of all filenames in the "filedir" directory. – Dr. Strange Feb 17 '20 at 20:30
  • You haven't actually asked a question. Rather, it sounds like you're asking us to implement some functionality for you. Is there a specific part of the problem that you're struggling with? – Bryan Oakley Feb 17 '20 at 20:45

1 Answers1

1

Here is a simple example using PIL to load the inital image and then use a button and function to load each next image.

import tkinter as tk
from PIL import ImageTk, Image
import os

path = 'C:/your/file/path/here'

def nextFile():
    # the global is needed to keep track of current index and also keep
    # a reference of the image being displayed so its is not garbage collected.
    global current_ndex, image
    current_ndex += 1
    if current_ndex < len(files):
        img = Image.open('{}/{}'.format(path, files[current_ndex]))
        image = ImageTk.PhotoImage(img)
        # clear the canvas before adding the new image.
        canvas.delete("all")
        canvas.create_image(0, 0, image=image, anchor=tk.CENTER)


my_window = tk.Tk()
my_window.title("File Viewer")
files = os.listdir(path)

current_ndex = 0

button2 = tk.Button(my_window, text="Next", width=50, command=nextFile)
canvas = tk.Canvas(my_window, width=100, height=100)
button2.pack(side=tk.BOTTOM)
canvas.pack()

first_image = files[0]

img = Image.open('{}/{}'.format(path, first_image))
image = ImageTk.PhotoImage(img)
canvas.create_image(0, 0, image=image, anchor=tk.CENTER)
my_window.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Thank you for the answer. When I run for a single image it loads an image on canvas, but when I try with recursively loading files from a directory, it throws the following error:TclError: image "pyimage77" doesn't exist – Dr. Strange Feb 18 '20 at 20:32
  • 1
    Maybe this post will help. https://stackoverflow.com/questions/24274072/tkinter-pyimage-doesnt-exist. I do not have the error you mentioned. My end works fine with multiple images. – Mike - SMT Feb 18 '20 at 20:36
  • Yes, it was the problem. Thank you! – Dr. Strange Apr 13 '20 at 13:06