Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,26 @@
|
|
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(
|
9 |
-
|
|
|
|
|
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 |
-
|
16 |
-
|
17 |
-
|
18 |
-
sepia_imgs = [(
|
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:
|
@@ -29,21 +30,23 @@ def sepia_and_zip(input_img, num_copies):
|
|
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
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
from zipfile import ZipFile
|
6 |
|
7 |
+
def sepia_and_zip(input_img_path, num_copies):
|
8 |
+
input_img = Image.open(input_img_path)
|
9 |
+
input_img_np = np.array(input_img)
|
10 |
+
|
11 |
sepia_filter = np.array([
|
12 |
[0.393, 0.769, 0.189],
|
13 |
[0.349, 0.686, 0.168],
|
14 |
[0.272, 0.534, 0.131]
|
15 |
])
|
16 |
+
input_img_np = input_img_np / 255.0
|
17 |
+
sepia_img_np = np.dot(input_img_np[..., :3], sepia_filter.T)
|
18 |
+
sepia_img_np = np.clip(sepia_img_np, 0, 1) * 255
|
19 |
+
sepia_imgs = [(sepia_img_np).astype(np.uint8) for _ in range(num_copies)]
|
20 |
+
|
21 |
# Convert numpy images to PIL images for gallery
|
22 |
pil_images = [Image.fromarray(img) for img in sepia_imgs]
|
23 |
+
|
24 |
# Prepare ZIP file
|
25 |
zip_bytes_io = io.BytesIO()
|
26 |
with ZipFile(zip_bytes_io, 'w') as zip_file:
|
|
|
30 |
img_byte_arr = img_byte_arr.getvalue()
|
31 |
zip_file.writestr(f"sepia_image_{i}.png", img_byte_arr)
|
32 |
zip_bytes_io.seek(0)
|
33 |
+
|
34 |
+
# Return a gallery of images and a ZIP file for download
|
35 |
return pil_images, zip_bytes_io
|
36 |
|
37 |
+
title = "Sepia Tone Generator with Downloadable ZIP"
|
38 |
+
description = "Upload an image and select the number of sepia-toned copies you want. View the images in a gallery and download them all as a ZIP file."
|
39 |
+
examples = []
|
40 |
+
|
41 |
+
iface = gr.Interface(fn=sepia_and_zip,
|
42 |
+
inputs=[gr.Image(type="filepath", label="Input Image"),
|
43 |
+
gr.Number(label="Number of Copies")],
|
44 |
+
outputs=[gr.Gallery(label="Sepia Images"),
|
45 |
+
gr.File(label="Download ZIP")],
|
46 |
+
title=title,
|
47 |
+
description=description,
|
48 |
+
examples=examples,
|
49 |
+
allow_flagging="never",
|
50 |
+
cache_examples=False)
|
51 |
|
52 |
+
iface.launch(debug=True, enable_queue=False)
|
|