# Importing required libraries import gradio as gr from PIL import Image import random # Define pixel sizes for different levels pixel_sizes = [128, 96, 64, 32, 24, 16, 12, 8, 4, 2] # Function to pixelate an image def pixelate(image, pixel_size): # Reduce the image size small = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.Resampling.NEAREST) # Scale back to original size return small.resize(image.size, Image.Resampling.NEAREST) # List of celebrities and folder paths celeb_list = ["Tom Cruise", "Jake Gyllenhal", "Natalie Portman", "Aubrey Plaza", "Oscar Isaac", "Kate Winslet", "Ellen DeGeneres"] celeb_folder = { "Tom Cruise": "./Celebs/TomCruise.jpeg", "Jake Gyllenhal": "./Celebs/JakeGyllenhal.jpg", "Natalie Portman": "./Celebs/NataliePortman.png", "Aubrey Plaza": "./Celebs/AubreyPlaza.jpg", "Oscar Isaac": "./Celebs/OscarIsaac.jpg", "Kate Winslet": "./Celebs/KateWinslet.jpg", "Ellen DeGeneres": "./Celebs/EllenDeGeneres.jpg" } # Initialize global variables current_index = 0 current_pixel_size = 256 def clear_and_start(prev_size=256): """ Resets the current image and returns the first level of pixelation. """ global current_index, current_pixel_size current_pixel_size = prev_size celebrity = celeb_list[current_index] image_path = celeb_folder[celebrity] # Open and pixelate the image img = Image.open(image_path) result_img = pixelate(img, current_pixel_size) return result_img, celebrity, current_pixel_size def next_image(pixel_size): """ Moves to the next celebrity and pixelates the image. """ global current_index, current_pixel_size current_index = (current_index + 1) % len(celeb_list) # Loop through the list current_pixel_size = pixel_size celebrity = celeb_list[current_index] image_path = celeb_folder[celebrity] # Open and pixelate the image img = Image.open(image_path) result_img = pixelate(img, current_pixel_size) return result_img, celebrity, current_pixel_size def progressive_clear(pixel_size): """ Progressively clears the pixelation of the current image. """ global current_pixel_size current_pixel_size = max(pixel_size - 32, 2) # Decrease pixel size for better clarity celebrity = celeb_list[current_index] image_path = celeb_folder[celebrity] # Open and pixelate the image img = Image.open(image_path) result_img = pixelate(img, current_pixel_size) return result_img, celebrity, current_pixel_size # Gradio App Layout MARKDOWN = "## Guess the Celebrity before the Image Clears Up!" with gr.Blocks(theme=gr.themes.Ocean()) as demo: gr.Markdown(MARKDOWN) with gr.Row(): with gr.Column(scale=1): pixelated_image = gr.Image(type='pil', label='Pixelated Image') with gr.Accordion("Reveal Answer", open=False): answer = gr.Textbox(label="Current Celebrity") with gr.Column(scale=1): Start_button = gr.Button(value='Start', variant='primary') Clear_button = gr.Button(value='Clear More', variant='secondary') Next_button = gr.Button(value='Next Image', variant='success') # Define button actions Start_button.click(fn=clear_and_start, inputs=[], outputs=[pixelated_image, answer, gr.State(current_pixel_size)]) Clear_button.click(fn=progressive_clear, inputs=[gr.State(current_pixel_size)], outputs=[pixelated_image, answer, gr.State(current_pixel_size)]) Next_button.click(fn=next_image, inputs=[gr.State(current_pixel_size)], outputs=[pixelated_image, answer, gr.State(current_pixel_size)]) demo.launch(debug=True, show_error=True)