Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,56 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
from zipfile import ZipFile
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
+
def sepia(input_img, num_copies):
|
| 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.array(input_img) / 255.0
|
| 17 |
+
sepia_imgs = []
|
| 18 |
+
for _ in range(num_copies):
|
| 19 |
+
sepia_img = np.dot(input_img[..., :3], sepia_filter.T)
|
| 20 |
+
sepia_img = np.clip(sepia_img, 0, 1) * 255
|
| 21 |
+
sepia_imgs.append(Image.fromarray(sepia_img.astype(np.uint8)))
|
| 22 |
+
return sepia_imgs
|
| 23 |
|
| 24 |
+
def zip_sepia_images(sepia_imgs):
|
| 25 |
+
# Use BytesIO to avoid actual file system operations for ZIP file creation
|
| 26 |
+
zip_bytes = io.BytesIO()
|
| 27 |
+
with ZipFile(zip_bytes, 'w') as zipf:
|
| 28 |
+
for i, img in enumerate(sepia_imgs):
|
| 29 |
+
# Convert PIL Image to bytes
|
| 30 |
+
img_byte_arr = io.BytesIO()
|
| 31 |
+
img.save(img_byte_arr, format='PNG')
|
| 32 |
+
img_byte_arr.seek(0)
|
| 33 |
+
zipf.writestr(f"sepia_image_{i}.png", img_byte_arr.getvalue())
|
| 34 |
+
zip_bytes.seek(0)
|
| 35 |
+
return zip_bytes, "sepia_images.zip"
|
| 36 |
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
with gr.Row():
|
| 39 |
+
input_img = gr.Image()
|
| 40 |
+
num_copies = gr.Number(label="Number of Copies", value=1)
|
| 41 |
+
with gr.Row():
|
| 42 |
+
gallery = gr.Gallery(label="Sepia Images")
|
| 43 |
+
download_btn = gr.File(label="Download ZIP")
|
| 44 |
+
|
| 45 |
+
def update_output(input_img, num_copies):
|
| 46 |
+
sepia_imgs = sepia(input_img, num_copies)
|
| 47 |
+
zip_file, zip_name = zip_sepia_images(sepia_imgs)
|
| 48 |
+
return sepia_imgs, (zip_name, zip_file)
|
| 49 |
+
|
| 50 |
+
generate_btn = gr.Button("Generate Sepia Images")
|
| 51 |
+
generate_btn.click(fn=update_output,
|
| 52 |
+
inputs=[input_img, num_copies],
|
| 53 |
+
outputs=[gallery, download_btn])
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|