|
| 1 | +from tkinter import * |
| 2 | +from PIL import Image, ImageTk |
| 3 | +import os |
| 4 | + |
| 5 | +root = Tk() |
| 6 | +root.geometry("600x600") |
| 7 | + |
| 8 | +def resize_image(root, copy_image, label1): |
| 9 | + new_height = 600 |
| 10 | + new_width = 600 |
| 11 | + image=copy_image.resize((new_width,new_height)) |
| 12 | + photo=ImageTk.PhotoImage(image) |
| 13 | + label1.configure(image=photo) |
| 14 | + label1.image=photo |
| 15 | + |
| 16 | +def next(): |
| 17 | + global n |
| 18 | + global itemslist |
| 19 | + n=(n+1)%len(itemslist) |
| 20 | + img1 = itemslist[n] |
| 21 | + |
| 22 | + image = Image.open(path+"/"+img1) |
| 23 | + copy_image=image.copy() |
| 24 | + photo=ImageTk.PhotoImage(image) |
| 25 | + |
| 26 | + label=Label(root,image=photo) |
| 27 | + label.bind('<configure>',resize_image(root, copy_image, label1)) |
| 28 | + label.pack() |
| 29 | + |
| 30 | +def previous(): |
| 31 | + global n |
| 32 | + global itemslist |
| 33 | + n=(n-1)%len(itemslist) |
| 34 | + img1 = itemslist[n] |
| 35 | + |
| 36 | + image = Image.open(path+"/"+img1) |
| 37 | + copy_image=image.copy() |
| 38 | + photo=ImageTk.PhotoImage(image) |
| 39 | + |
| 40 | + label2=Label(root,image=photo) |
| 41 | + label2.bind('<configure>',resize_image(root, copy_image, label1)) |
| 42 | + label2.pack() |
| 43 | + |
| 44 | + |
| 45 | +n=0 |
| 46 | +path = input(r"Enter path for the image folder: ") |
| 47 | +itemslist = os.listdir(path) |
| 48 | +img1 = itemslist[n] |
| 49 | + |
| 50 | +image = Image.open(path+"/"+img1) |
| 51 | +copy_image=image.copy() |
| 52 | +photo=ImageTk.PhotoImage(image) |
| 53 | + |
| 54 | +label1=Label(root,image=photo) |
| 55 | +label1.bind('<configure>',resize_image(root, copy_image, label1)) |
| 56 | +label1.pack() |
| 57 | + |
| 58 | +btn1 = Button(root,text="next",width=5,height=10,command=next) |
| 59 | +btn1.place(x=570,y=150) |
| 60 | + |
| 61 | +btn1 = Button(root,text="prev",width=5,height=10,command=previous) |
| 62 | +btn1.place(x=0,y=150) |
| 63 | + |
| 64 | +root.mainloop() |
| 65 | + |
0 commit comments