Photo gallery with pythonTkinter project that display all of your image files

This time I made a gallery with tkinter ,it was a matter of playing the Button functionality of tkinter ,it works well but thier is still room for improvement

the external library i installed is Pillow which is refered to as pil

pip install Pillow

first import the needed libraries

tkinter for GUI

PIL for image resizing

os to open files ,directories

create mainwindow give it a size

from tkinter import *
from PIL import Image,ImageTk
from tkinter import messagebox, filedialog
import os
mainwindow=Tk()
mainwindow.geometry("650x400")

initialize the varibles to use

allimages to store the file location of all images

count to keep track of number of images

and the maximg1,2,3,4 to store large images of images on the grid display

count=0
counter=0
allimages=[]
maximg1=''
maximg2=''
maximg3=''
maximg4=''

give the default directory from which images are displayed for mycase i set it to desktop images

directory='C:\\Users\\g\\Desktop\\'

define the function to load images

open the given directory

loop through all the files in the directory

and for each file check if its an image if yes increase image count by one and add the directory of the file to allimages

def loadimages():
    global count
    global allimages
    global directory
    dir_path=directory
    for root,dirs,files in os.walk(dir_path):
        for file in files:
            if file.endswith('.png') or file.endswith('.jpg')or file.endswith('.jpeg'):
                count=count+1
                image=os.path.join(root,file)
                image=image.replace('\\',"\\\\")
                allimages.append(image)

now add the function to display the images in grid and show next and previous images

declare local variables to store location of first four images.img1,2,3,4

use the global maximg1,2,3,4 variables to store location of the first four images

open the images ,resize them and store them in their respective variables

create four buttons to display the small images

call display_image() to give the large image to display

def nxtimages():
    global maximg1
    global maximg2
    global maximg3
    global maximg4
    global counter
    global allimages
    global img1
    global img2
    global img3
    global img4
    img1=allimages[counter+1]
    img2=allimages[counter+2]
    img3=allimages[counter+3]
    img4=allimages[counter+4]
     
    maximg1=allimages[counter+1]
    maximg2=allimages[counter+2]
    maximg3=allimages[counter+3]
    maximg4=allimages[counter+4]
     
    image=Image.open(img1)
    resizeds=image.resize((130,100))
    img1=ImageTk.PhotoImage(resizeds)
 
    image=Image.open(img2)
    resizeds=image.resize((130,100))
    img2=ImageTk.PhotoImage(resizeds)
     
    image=Image.open(img3)
    resizeds=image.resize((130,100))
    img3=ImageTk.PhotoImage(resizeds)
 
    #MAXIIMAGES
    image=Image.open(img4)
    resizeds=image.resize((130,100))
    img4=ImageTk.PhotoImage(resizeds)
    maiximg1=img1
     
    image=Image.open(maximg1)
    resizeds=image.resize((280,250))
    maximg1=ImageTk.PhotoImage(resizeds)
 
    image=Image.open(maximg2)
    resizeds=image.resize((280,250))
    maximg2=ImageTk.PhotoImage(resizeds)
     
    image=Image.open(maximg3)
    resizeds=image.resize((280,250))
    maximg3=ImageTk.PhotoImage(resizeds)
     
    image=Image.open(maximg4)
    resizeds=image.resize((280,250))
    maximg4=ImageTk.PhotoImage(resizeds)
     
    imagebut=Button(mainwindow,image=img1,command=lambda:display(1)).place(x=40,y=50)
    imagebut=Button(image=img2,command=lambda:display(2)).place(x=40,y=200)
    imagebut=Button(image=img3,command=lambda:display(3)).place(x=200,y=50)
    imagebut=Button(image=img4,command=lambda:display(4)).place(x=200,y=200)
    display_image(1)

now to the display_image() function

use the number that been given to the function to check for the respective large image number to display

def display_image(number):
    global maximg1
    global maximg2
    global maximg3
    global maximg4
    global img1
    global img2
    global img3
    global img4
    if number==1:   imagebut2=Button(image=maximg1).place(x=350,y=50)
    if number==2:
        imagebut2=Button(image=maximg2).place(x=350,y=50)
    if number==3:
        imagebut2=Button(image=maximg3).place(x=350,y=50)
    if number==4:
        imagebut2=Button(image=maximg4).place(x=350,y=50)

this other function does the same job of display_image ,it is called when a user clicks an image and it then calls the display_image() I dont know why i made it this way but it works

def display(number):
    display_image(number)

the next functions display the next and previous grids of image

def nextimageshow():
    global counter
    try:
        counter=counter+4
        nxtimages()
    except:
        pass
     
def previmageshow():
    global counter
    try:
        counter=counter-4
        nxtimages()
    except:
        pass

incase the user needs to display images in a particular folder this function takes the responsibility

first erase all the previous images and load the new ones in the folder

def selectfolder():
    global directory
    global maximg1
    global maximg2
    global maximg3
    global maximg4
    global counter
    global allimages
    global img1
    global img2
    global img3
    global img4
    allimages=[]
    allsmall=[]
    allmax=[]
    maximg1=''
    maximg2=''
    maximg3=''
    maximg4=''
 
    directory= filedialog.askdirectory(initialdir="%")
    loadimages()
    nxtimages()

now add this control buttons to display next,previous images and open folders

then call the loadimage() to load images in the default folder

nxtimages()to display loaded images

mainwindow.mainloop() to keep the app running

loadimages()
nxtimages()
 
nxt=Button(text='->',bg='red',fg='white',command=nextimageshow).place(x=200,y=350)
nxt=Button(text='<-',bg='red',fg='white',command=previmageshow).place(x=170,y=350)
folder=Button(text='select folder',bg='red',fg='white',command=selectfolder).place(x=400,y=350)
mainwindow.mainloop()

full application code for python tkinter photo gallery project

from tkinter import *
from PIL import Image,ImageTk
from tkinter import messagebox, filedialog
import os
  
mainwindow=Tk()
mainwindow.geometry("650x400")
  
def display_image(number):
    global maximg1
    global maximg2
    global maximg3
    global maximg4
    global img1
    global img2
    global img3
    global img4
 
    if number==1:
        imagebut2=Button(image=maximg1).place(x=350,y=50)
    if number==2:
        imagebut2=Button(image=maximg2).place(x=350,y=50)
    if number==3:
        imagebut2=Button(image=maximg3).place(x=350,y=50)
    if number==4:
        imagebut2=Button(image=maximg4).place(x=350,y=50)
      
def display(number):
    display_image(number)
          
  
    
  
count=0
counter=0
allimages=[]
maximg1=''
maximg2=''
maximg3=''
maximg4=''
directory='C:\\Users\\g\\Desktop\\'
 
def loadimages():
    global count
    global allimages
    global directory
    dir_path=directory
    for root,dirs,files in os.walk(dir_path):
        for file in files:
            if file.endswith('.png') or file.endswith('.jpg')or file.endswith('.jpeg'):
                count=count+1
                image=os.path.join(root,file)
                image=image.replace('\\',"\\\\")
                allimages.append(image)
                 
 
     
def nextimageshow():
    global counter
    try:
        counter=counter+4
        nxtimages()
    except:
        pass
      
def previmageshow():
    global counter
    try:
        counter=counter-4
        nxtimages()
    except:
        pass
def selectfolder():
    global directory
    global maximg1
    global maximg2
    global maximg3
    global maximg4
    global counter
    global allimages
    global img1
    global img2
    global img3
    global img4
    allimages=[]
    counter=0
    count=0
    maximg1=''
    maximg2=''
    maximg3=''
    maximg4=''
  
    directory= filedialog.askdirectory(initialdir="%")
    loadimages()
    nxtimages()
     
def nxtimages():
    global maximg1
    global maximg2
    global maximg3
    global maximg4
    global counter
    global allimages
    global img1
    global img2
    global img3
    global img4
    img1=allimages[counter+1]
    img2=allimages[counter+2]
    img3=allimages[counter+3]
    img4=allimages[counter+4]
      
    maximg1=allimages[counter+1]
    maximg2=allimages[counter+2]
    maximg3=allimages[counter+3]
    maximg4=allimages[counter+4]
      
    image=Image.open(img1)
    resizeds=image.resize((130,100))
    img1=ImageTk.PhotoImage(resizeds)
  
    image=Image.open(img2)
    resizeds=image.resize((130,100))
    img2=ImageTk.PhotoImage(resizeds)
      
    image=Image.open(img3)
    resizeds=image.resize((130,100))
    img3=ImageTk.PhotoImage(resizeds)
  
    #MAXIIMAGES
    image=Image.open(img4)
    resizeds=image.resize((130,100))
    img4=ImageTk.PhotoImage(resizeds)
    maiximg1=img1
      
    image=Image.open(maximg1)
    resizeds=image.resize((280,250))
    maximg1=ImageTk.PhotoImage(resizeds)
  
    image=Image.open(maximg2)
    resizeds=image.resize((280,250))
    maximg2=ImageTk.PhotoImage(resizeds)
      
    image=Image.open(maximg3)
    resizeds=image.resize((280,250))
    maximg3=ImageTk.PhotoImage(resizeds)
      
    image=Image.open(maximg4)
    resizeds=image.resize((280,250))
    maximg4=ImageTk.PhotoImage(resizeds)
      
    imagebut=Button(mainwindow,image=img1,command=lambda:display(1)).place(x=40,y=50)
    imagebut=Button(image=img2,command=lambda:display(2)).place(x=40,y=200)
    imagebut=Button(image=img3,command=lambda:display(3)).place(x=200,y=50)
    imagebut=Button(image=img4,command=lambda:display(4)).place(x=200,y=200)
    display_image(1)
loadimages()
nxtimages()
  
nxt=Button(text='>',bg='red',fg='white',command=nextimageshow).place(x=200,y=350)
nxt=Button(text='<',bg='red',fg='white',command=previmageshow).place(x=170,y=350)
folder=Button(text='selectfolder',bg='red',fg='white',command=selectfolder).place(x=400,y=350)
mainwindow.mainloop()

I value your comments ,say what want to say here

Leave a comment

Design a site like this with WordPress.com
Get started