NikhilJoson commited on
Commit
c2058a3
·
verified ·
1 Parent(s): 7a3b1ed

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from PIL import Image
3
+ import matplotlib.pyplot as plt
4
+
5
+
6
+ # Define pixel sizes for different levels
7
+ pixel_sizes = [128, 96, 64, 32, 24, 16, 12, 8, 4, 2]
8
+ # Function to pixelate an image
9
+ def pixelate(image, pixel_size):
10
+ # Reduce the image size
11
+ small = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.Resampling.NEAREST)
12
+ # Scale back to original size
13
+ return small.resize(image.size, Image.Resampling.NEAREST)
14
+
15
+ # dictionary of photo name and url
16
+ celeb_list = ["Tom Cruise", "Jake Gyllenhal", "Natalie Portman", "Aubrey Plaza", "Oscar Isaac", "Kate Winslet", "Ellen DeGeneres"]
17
+ celeb_folder = {
18
+ "Tom Cruise" : "./Celebs/TomCruise.jpeg",
19
+ "Jake Gyllenhal" : "./Celebs/JakeGyllenhal.jpg",
20
+ "Natalie Portman" : "./Celebs/NataliePortman.png",
21
+ "Kajol" : "./Celebs/Kajol.png",
22
+ "Oscar Isaac" : "./Celebs/OscarIsaac.jpg",
23
+ "Nayanthara" : "./Celebs/Nayanthara.jpg",
24
+ "Dhanush" : "./Celebs/Dhanush.jpg",
25
+ }
26
+
27
+
28
+ def Clear(prev_size, photo=celeb_list[1], folder=celeb_folder, next=False):
29
+ if next:
30
+ index = celeb_list.index(photo) + 1
31
+ photo = celeb_list[index]
32
+
33
+ print(f"Processing {photo}")
34
+ image_path = folder[photo]
35
+ img = Image.open(BytesIO(image_path))
36
+
37
+ for curr_size in pixel_sizes:
38
+ if curr_size<prev_size:
39
+ return pixelate(img, i), photo, curr_size
40
+
41
+
42
+ # # Display the pixelated images progressively
43
+ # for idx, img in enumerate(images):
44
+ # plt.figure(figsize=(6, 6))
45
+ # plt.imshow(img)
46
+ # plt.axis("off")
47
+ # plt.title(f"Guess the Portrait - Level {idx + 1}")
48
+ # plt.show()
49
+
50
+ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
51
+ gr.Markdown(MARKDOWN)
52
+ with gr.Row():
53
+ with gr.Column(scale=1):
54
+ pixelated_image = gr.Image(type='pil', image_mode='RGB', label='Pixelated Image')
55
+ with gr.Accordion("Answer", open=False):
56
+ answer = gr.Textbox()
57
+
58
+ Clear_button = gr.Button(value='Clear', variant='primary')
59
+ Next_button = gr.Button(value='Next', variant='secondary')
60
+
61
+ Clear_button.click(fn=Clear, inputs=[256], outputs=[pixelated_image, answer, prev_size])
62
+ Next_button.click(fn=Clear, inputs=[256, photo==answer, next==True], outputs=[pixelated_image, answer, prev_size])
63
+
64
+ demo.launch(debug=False, show_error=True)