import os
from tkinter import *
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk

# Create main window

root= Tk()
root.title("Image Viewer")
root.geometry("800x600")
root.config(bg="#f0f0f0")

image_list = []
image_index = 0

def load_image():
    global image_list, image_index
    folder_path = filedialog.askdirectory(title="Select Image Folder")
    
    if not folder_path:
        return
    
    supported_ext = ('.png', '.jpg', '.jpeg', '.gif', '.bmp')
    image_files =[f for f in os.listdir(folder_path) if f.lower().endswith(supported_ext)]
    
    if not image_files:
        messagebox.showerror("No images", "No image files found in the selected folder!")
        return
    
    image_list = [os.path.join(folder_path, f) for f in image_files]
    image_index = 0
    show_image(image_list[image_index])
    
def show_image(image_path):
    global img_label, img_display
    img = Image.open(image_path)
    img = img.resize((700, 500), Image.LANCZOS)
    img_display = ImageTk.PhotoImage(img)
    
    img_label.config(image=img_display)
    img_label.image = img_display
    status_label.config(text=f"{os.path.basename(image_path)} ({image_index+1}/{len(image_list)})")
    
def prev_image():
    global image_index
    if image_list:
        image_index = (image_index - 1) % len(image_list)
        show_image(image_list[image_index])
def next_image():
    global image_index
    if image_list:
        image_index = (image_index + 1) % len(image_list)
        show_image(image_list[image_index])
# UI Layout
frame = Frame(root, bg="#f0f0f0")
frame.pack(pady=10)

btn_load = Button(frame, text="📁 Load Folder",command=load_image,bg="#4CAF50",fg="white", font=("Arial", 12, "bold"))
btn_load.grid(row=0, column=0, padx=10)

btn_prev = Button(frame, text="⬅ Previous",command=prev_image,bg="#4CAF50",fg="white", font=("Arial", 12,))
btn_prev.grid(row=0, column=1, padx=10)

btn_next = Button(frame, text="Next ➡",command=next_image,bg="#4CAF50",fg="white", font=("Arial", 12,))
btn_next.grid(row=0, column=2, padx=10)

img_label = Label(root, bg="white", width=700, height=500, bd=2, relief=SUNKEN)
img_label.pack(pady=10)

status_label = Label(root, text="No Image Loaded", bg="#f0f0f0", font=("Arial", 10, "italic"))
status_label.pack(pady=5)




root.mainloop()