import os
import glob
import random
from tkinter import Tk, Button, filedialog, Label
from PIL import Image

# Supported image extensions
IMAGE_EXTENSIONS = [‘.jpg’, ‘.jpeg’, ‘.png’, ‘.gif’, ‘.bmp’, ‘.webp’]

class ImageIndexerApp:
    def init(self, root):
        self.root = root
        self.root.title(“Image Indexer”)
        
        self.open_button = Button(self.root, text=“Open Folder”, command=self.on_open_folder)
        self.open_button.pack()
        
        self.next_button = Button(self.root, text=“Next Image”, command=self.next_image, state=‘disabled’)
        self.next_button.pack()
        
        self.random_button = Button(self.root, text=“Random Image”, command=self.random_image, state=‘disabled’)
        self.random_button.pack()

        self.total_images_label = Label(self.root, text=“Total images: 0”)
        self.total_images_label.pack()

        # Initialize index and file list
        self.current_image_index = 0
        self.image_files = []

    def is_image(self, filename):
        try:
            Image.open(filename)  # Attempt to open the image file using Pillow
            return True
        except (IOError, FileNotFoundError):
            return False

    def on_open_folder(self):
        directory = filedialog.askdirectory()
        if not directory:
            return
        self.image_files = self.list_images(directory)
        self.current_image_index = 0  # Reset index
        self.total_images_label.config(text=f"Total images: {len(self.image_files)}“)
        # Enable next and random buttons only if there are images
        if self.image_files:
            self.next_button.config(state=‘normal’)
            self.random_button.config(state=‘normal’)

    def next_image(self):
        if self.image_files:
            print(f”{self.current_image_index + 1}: {self.image_files[self.current_image_index]}“)
            self.current_image_index = (self.current_image_index + 1) % len(self.image_files)

    def random_image(self):
        if self.image_files:
            self.current_image_index = random.randint(0, len(self.image_files) - 1)
            print(f”{self.current_image_index + 1}: {self.image_files[self.current_image_index]}“)

    def list_images(self, directory):
        images = []
        for extension in IMAGE_EXTENSIONS:
            images.extend(glob.glob(os.path.join(directory, f”*{extension}")))

        images = [img for img in images if self.is_image(img)]
        return images

if name == ‘main’:
    root = Tk()
    app = ImageIndexerApp(root)
    root.mainloop()


add a previous button that print the image called previously, as opposed to the previous file in the index. change current_image_index to img_num. then make add a "random" checkbox that will make the next image button call random image in the index instead of going linearly. make it so that when i press previous while the "random" checkbox is on, it returns to the previously called image, and before that, and so on, instead of going to the previous file on the index, but only when the random checkbox is deactivated, it calls the previous image in the index list. make sure to add comments to explain each method and important parts