gokaygokay commited on
Commit
c1e6a45
·
verified ·
1 Parent(s): 63347fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -0
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ import torchvision.transforms as T
5
+ import numpy as np
6
+ from pixeloe.pixelize import pixelize
7
+ from PIL import Image
8
+
9
+ def pixelize_image(
10
+ image,
11
+ downscale_mode="contrast",
12
+ target_size=128,
13
+ patch_size=16,
14
+ thickness=2,
15
+ color_matching=True,
16
+ upscale=True
17
+ ):
18
+ """
19
+ Apply pixelization effect to an image or batch of images.
20
+ """
21
+ # Convert PIL Image to numpy array
22
+ if isinstance(image, Image.Image):
23
+ image = np.array(image)
24
+
25
+ # Process the image
26
+ processed = pixelize(
27
+ image,
28
+ mode=downscale_mode,
29
+ target_size=target_size,
30
+ patch_size=patch_size,
31
+ thickness=thickness,
32
+ contrast=1.0,
33
+ saturation=1.0,
34
+ color_matching=color_matching,
35
+ no_upscale=not upscale
36
+ )
37
+
38
+ # Convert back to PIL Image
39
+ return Image.fromarray(processed)
40
+
41
+ # Gradio interface function
42
+ @spaces.GPU
43
+ def process_image(image, downscale_mode, target_size, patch_size, thickness, color_matching, upscale):
44
+ if image is None:
45
+ return None
46
+
47
+ result = pixelize_image(
48
+ image,
49
+ downscale_mode=downscale_mode,
50
+ target_size=target_size,
51
+ patch_size=patch_size,
52
+ thickness=thickness,
53
+ color_matching=color_matching,
54
+ upscale=upscale
55
+ )
56
+
57
+ return result
58
+
59
+ # Create Gradio interface
60
+ with gr.Blocks(title="Image Pixelizer") as demo:
61
+ gr.Markdown("# Image Pixelizer")
62
+ gr.Markdown("Upload an image and adjust the parameters to create pixel art effects.")
63
+
64
+ with gr.Row():
65
+ with gr.Column():
66
+ input_image = gr.Image(label="Input Image", type="pil")
67
+
68
+ downscale_mode = gr.Dropdown(
69
+ choices=["contrast", "bicubic", "nearest", "center", "k-centroid"],
70
+ value="contrast",
71
+ label="Downscale Mode"
72
+ )
73
+
74
+ target_size = gr.Slider(
75
+ minimum=8,
76
+ maximum=1024,
77
+ value=128,
78
+ step=8,
79
+ label="Target Size"
80
+ )
81
+
82
+ patch_size = gr.Slider(
83
+ minimum=4,
84
+ maximum=32,
85
+ value=16,
86
+ step=2,
87
+ label="Patch Size"
88
+ )
89
+
90
+ thickness = gr.Slider(
91
+ minimum=1,
92
+ maximum=16,
93
+ value=2,
94
+ step=1,
95
+ label="Thickness"
96
+ )
97
+
98
+ color_matching = gr.Checkbox(
99
+ value=True,
100
+ label="Color Matching"
101
+ )
102
+
103
+ upscale = gr.Checkbox(
104
+ value=True,
105
+ label="Upscale"
106
+ )
107
+
108
+ process_btn = gr.Button("Process Image")
109
+
110
+ with gr.Column():
111
+ output_image = gr.Image(label="Processed Image")
112
+
113
+ # Set up processing event
114
+ process_btn.click(
115
+ fn=process_image,
116
+ inputs=[
117
+ input_image,
118
+ downscale_mode,
119
+ target_size,
120
+ patch_size,
121
+ thickness,
122
+ color_matching,
123
+ upscale
124
+ ],
125
+ outputs=output_image
126
+ )
127
+
128
+ # Add example images
129
+ gr.Examples(
130
+ examples=[
131
+ ["example1.jpg", "contrast", 128, 16, 2, True, True],
132
+ ["example2.jpg", "bicubic", 256, 8, 1, True, True],
133
+ ],
134
+ inputs=[
135
+ input_image,
136
+ downscale_mode,
137
+ target_size,
138
+ patch_size,
139
+ thickness,
140
+ color_matching,
141
+ upscale
142
+ ],
143
+ outputs=output_image,
144
+ cache_examples=True,
145
+ )
146
+
147
+ # Launch the interface
148
+ if __name__ == "__main__":
149
+ demo.launch(share=True)