Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import tempfile
|
4 |
-
import shutil
|
5 |
import os
|
|
|
|
|
6 |
from zipfile import ZipFile
|
7 |
|
8 |
-
def
|
|
|
9 |
sepia_filter = np.array([
|
10 |
-
[0.393, 0.769, 0.189],
|
11 |
-
[0.349, 0.686, 0.168],
|
12 |
[0.272, 0.534, 0.131]
|
13 |
])
|
14 |
input_img = input_img / 255.0
|
15 |
sepia_img = np.dot(input_img[..., :3], sepia_filter.T)
|
16 |
sepia_img = np.clip(sepia_img, 0, 1) * 255
|
17 |
sepia_imgs = [(sepia_img).astype(np.uint8) for _ in range(num_copies)]
|
18 |
-
return sepia_imgs
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
with gr.Blocks() as demo:
|
31 |
with gr.Row():
|
32 |
input_img = gr.Image()
|
33 |
num_copies = gr.Number(label="Number of Copies", value=1)
|
|
|
34 |
gallery = gr.Gallery(label="Sepia Images")
|
35 |
-
download_btn = gr.File(label="Download ZIP"
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
generate_btn.click(fn=sepia, inputs=[input_img, num_copies], outputs=gallery)
|
42 |
-
generate_btn.click(fn=zip_sepia_images, inputs=gallery, outputs=download_btn)
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
|
|
3 |
import os
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
from zipfile import ZipFile
|
7 |
|
8 |
+
def sepia_and_zip(input_img, num_copies):
|
9 |
+
# Apply the sepia filter to the image
|
10 |
sepia_filter = np.array([
|
11 |
+
[0.393, 0.769, 0.189],
|
12 |
+
[0.349, 0.686, 0.168],
|
13 |
[0.272, 0.534, 0.131]
|
14 |
])
|
15 |
input_img = input_img / 255.0
|
16 |
sepia_img = np.dot(input_img[..., :3], sepia_filter.T)
|
17 |
sepia_img = np.clip(sepia_img, 0, 1) * 255
|
18 |
sepia_imgs = [(sepia_img).astype(np.uint8) for _ in range(num_copies)]
|
|
|
19 |
|
20 |
+
# Convert numpy images to PIL images for gallery
|
21 |
+
pil_images = [Image.fromarray(img) for img in sepia_imgs]
|
22 |
+
|
23 |
+
# Prepare ZIP file
|
24 |
+
zip_bytes_io = io.BytesIO()
|
25 |
+
with ZipFile(zip_bytes_io, 'w') as zip_file:
|
26 |
+
for i, img in enumerate(pil_images):
|
27 |
+
img_byte_arr = io.BytesIO()
|
28 |
+
img.save(img_byte_arr, format='PNG')
|
29 |
+
img_byte_arr = img_byte_arr.getvalue()
|
30 |
+
zip_file.writestr(f"sepia_image_{i}.png", img_byte_arr)
|
31 |
+
zip_bytes_io.seek(0)
|
32 |
+
|
33 |
+
return pil_images, zip_bytes_io
|
34 |
|
35 |
with gr.Blocks() as demo:
|
36 |
with gr.Row():
|
37 |
input_img = gr.Image()
|
38 |
num_copies = gr.Number(label="Number of Copies", value=1)
|
39 |
+
with gr.Row():
|
40 |
gallery = gr.Gallery(label="Sepia Images")
|
41 |
+
download_btn = gr.File(label="Download ZIP")
|
42 |
|
43 |
+
btn = gr.Button("Generate Sepia Images")
|
44 |
+
btn.click(fn=sepia_and_zip,
|
45 |
+
inputs=[input_img, num_copies],
|
46 |
+
outputs=[gallery, download_btn])
|
|
|
|
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
demo.launch()
|