File size: 3,569 Bytes
e9f8b4f
680e8a6
c2058a3
e9f8b4f
c2058a3
 
 
e9f8b4f
c2058a3
 
 
 
 
 
 
e9f8b4f
c2058a3
 
e9f8b4f
 
 
 
 
 
 
c2058a3
 
e9f8b4f
 
 
c2058a3
e9f8b4f
 
 
 
 
 
 
 
 
 
 
 
 
c2058a3
e9f8b4f
 
 
 
 
 
 
 
 
 
 
 
 
 
c2058a3
e9f8b4f
 
 
 
 
 
 
 
 
 
 
 
 
2aa8325
e9f8b4f
 
 
c2058a3
e9f8b4f
c2058a3
 
e9f8b4f
 
 
c2058a3
e9f8b4f
 
 
 
8836ad1
e9f8b4f
 
 
 
c2058a3
e9f8b4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

def next_image(prev_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 = 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

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

# Gradio App Layout
MARKDOWN = "## Guess the Celebrity before the Image Clears Up!"
with gr.Blocks() 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])
    Clear_button.click(fn=progressive_clear, inputs=[gr.Number(value=current_pixel_size)], outputs=[pixelated_image, answer])
    Next_button.click(fn=next_image, inputs=[gr.Number(value=current_pixel_size)], outputs=[pixelated_image, answer])

demo.launch(debug=True, show_error=True)