0

The idea is to select all images from a directory (folder), it would display all these thumbnail images in a Window via tkinter.

I was able to find all the image directories with while, as in the code below, but I can't make them all display automatically.

Would there be any library or native function to create this "gallery" of images?

from tkinter import *
from typing import Sized
from PIL import *
import os
import cv2

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)

        load = Image.open("temporario/page0.png")
        load.thumbnail((350,350))
        render = ImageTk.PhotoImage(load)

        img = Label(self, image=render)
        img.image = render
        img.place(x=50, y=50)


        files = []
        pasta = 'temporario'
        for (diretorio, subpastas, arquivos) in os.walk(pasta):
            files.extend(arquivos)
        #print(pasta + '/' + files[2])


        i = 0
        while i <= 2:

            dirImg = pasta + '/' + files[i]
            loading = Image.open(dirImg)
            loading.thumbnail((350,350))
            renderiza = ImageTk.PhotoImage(loading)

            i = i + 1
            print(dirImg)
            imagemA = Label(self, image=renderiza)
            imagemA.image = renderiza
            imagemA.place(x=350, y=50)


root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("800x500")
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You are placing each thumbnail image at exactly the same coordinates in your window. The `.grid()` geometry manager would make a lot more sense here. – jasonharper Aug 17 '21 at 21:51
  • There is nothing built-in — sometimes you have to write things yourself or find a similar question. You may find [How to make a image viewer with left right functionality in tkinter?](https://stackoverflow.com/questions/68698441/how-to-make-a-image-viewer-with-left-right-functionality-in-tkinter) of interest. – martineau Aug 17 '21 at 22:07
  • you can't really use loops in the same thread/process as `tkinter`, either use `threading` (or `multiprocessing` but `threading` is easier) or use `.after()` "loops", and yes it is possible to create a gallery, and adding to @martineau comment you can find tutorials on youtube too like [this one about creating an image viewer](https://youtu.be/zg4c92pNFeo?list=PLCC34OHNcOtoC6GglhF3ncJ5rLwQrLGnV) (btw there might be a tutorial for gridding images too or another tutorial that covers a "gallery" type thing) – Matiiss Aug 17 '21 at 22:10

1 Answers1

0

thanks for the tips, it worked!

The code looked like this:

from tkinter import *
from typing import Sized
from PIL import *
import os
import cv2


from PIL import Image, ImageTk

class Window(Frame):
    

    def __init__(self, master=None):


        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)
                
        render2 = ImageTk.PhotoImage(load2)
        img2 = Label(self, image=render2, height=250, width=250)
        img2.image = render2
        img2.place(x=350, y=50)'''

        files = []
        path = 'temporario'

        for file in os.listdir(path):
            if file.endswith('.jpg') or file.endswith('.png'):
                files.append(file)
                print(file)
        
        
        i = 0
        while i <= (len(files))-1:

            dirImg = path + '/' + files[i]
            loading = Image.open(dirImg)
            loading.thumbnail((350,350))
            renderiza = ImageTk.PhotoImage(loading)            
            
            
            i = i + 1
            imagemA = Label(self, image=renderiza)
            imagemA.image = renderiza
            imagemA.grid(column=i, row=0, padx=5, pady=5)
        
         
            
            
        
root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("800x500")
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301