Update app.py
Browse files
app.py
CHANGED
|
@@ -21,18 +21,25 @@ def sepia(input_img, num_copies):
|
|
| 21 |
sepia_imgs.append(Image.fromarray(sepia_img.astype(np.uint8)))
|
| 22 |
return sepia_imgs
|
| 23 |
|
|
|
|
| 24 |
def zip_sepia_images(sepia_imgs):
|
| 25 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
with gr.Row():
|
|
@@ -42,10 +49,10 @@ with gr.Blocks() as demo:
|
|
| 42 |
gallery = gr.Gallery(label="Sepia Images")
|
| 43 |
download_btn = gr.File(label="Download ZIP")
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
generate_btn = gr.Button("Generate Sepia Images")
|
| 51 |
generate_btn.click(fn=update_output,
|
|
|
|
| 21 |
sepia_imgs.append(Image.fromarray(sepia_img.astype(np.uint8)))
|
| 22 |
return sepia_imgs
|
| 23 |
|
| 24 |
+
|
| 25 |
def zip_sepia_images(sepia_imgs):
|
| 26 |
+
# Initialize a BytesIO object to hold the ZIP file in memory
|
| 27 |
zip_bytes = io.BytesIO()
|
| 28 |
with ZipFile(zip_bytes, 'w') as zipf:
|
| 29 |
for i, img in enumerate(sepia_imgs):
|
| 30 |
+
# Convert the PIL Image to a bytes object
|
| 31 |
img_byte_arr = io.BytesIO()
|
| 32 |
img.save(img_byte_arr, format='PNG')
|
| 33 |
img_byte_arr.seek(0)
|
| 34 |
+
# Write the image bytes to the ZIP file
|
| 35 |
zipf.writestr(f"sepia_image_{i}.png", img_byte_arr.getvalue())
|
| 36 |
+
# IMPORTANT: Seek back to the start of the BytesIO object
|
| 37 |
zip_bytes.seek(0)
|
| 38 |
+
|
| 39 |
+
# Return the ZIP file as a tuple: (filename, bytes)
|
| 40 |
+
# Note: Ensure the filename has the correct file extension (.zip)
|
| 41 |
+
return "sepia_images.zip", zip_bytes.getvalue()
|
| 42 |
+
|
| 43 |
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
with gr.Row():
|
|
|
|
| 49 |
gallery = gr.Gallery(label="Sepia Images")
|
| 50 |
download_btn = gr.File(label="Download ZIP")
|
| 51 |
|
| 52 |
+
def update_output(input_img, num_copies):
|
| 53 |
+
sepia_imgs = sepia(input_img, num_copies)
|
| 54 |
+
zip_name, zip_bytes = zip_sepia_images(sepia_imgs)
|
| 55 |
+
return sepia_imgs, (zip_name, zip_bytes) # This tuple matches Gradio's expectation
|
| 56 |
|
| 57 |
generate_btn = gr.Button("Generate Sepia Images")
|
| 58 |
generate_btn.click(fn=update_output,
|