Spaces:
Sleeping
Sleeping
import random | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
# 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) | |
# dictionary of photo name and url | |
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", | |
"Kajol" : "./Celebs/Kajol.png", | |
"Oscar Isaac" : "./Celebs/OscarIsaac.jpg", | |
"Nayanthara" : "./Celebs/Nayanthara.jpg", | |
"Dhanush" : "./Celebs/Dhanush.jpg", | |
} | |
def Clear(prev_size, photo=celeb_list[1], folder=celeb_folder, next=False): | |
if next: | |
index = celeb_list.index(photo) + 1 | |
photo = celeb_list[index] | |
print(f"Processing {photo}") | |
image_path = folder[photo] | |
img = Image.open(BytesIO(image_path)) | |
for curr_size in pixel_sizes: | |
if curr_size<prev_size: | |
return pixelate(img, i), photo, curr_size | |
# # Display the pixelated images progressively | |
# for idx, img in enumerate(images): | |
# plt.figure(figsize=(6, 6)) | |
# plt.imshow(img) | |
# plt.axis("off") | |
# plt.title(f"Guess the Portrait - Level {idx + 1}") | |
# plt.show() | |
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', image_mode='RGB', label='Pixelated Image') | |
with gr.Accordion("Answer", open=False): | |
answer = gr.Textbox() | |
Clear_button = gr.Button(value='Clear', variant='primary') | |
Next_button = gr.Button(value='Next', variant='secondary') | |
Clear_button.click(fn=Clear, inputs=[256], outputs=[pixelated_image, answer, prev_size]) | |
Next_button.click(fn=Clear, inputs=[256, photo==answer, next==True], outputs=[pixelated_image, answer, prev_size]) | |
demo.launch(debug=False, show_error=True) |