Rahatara commited on
Commit
1d4619e
1 Parent(s): 6367602

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -36
app.py CHANGED
@@ -1,11 +1,9 @@
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([
@@ -13,51 +11,35 @@ def sepia(input_img, num_copies):
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
-
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():
46
  input_img = gr.Image()
47
  num_copies = gr.Number(label="Number of Copies", value=1)
48
- 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,
59
- inputs=[input_img, num_copies],
60
- outputs=[gallery, download_btn])
61
 
62
  if __name__ == "__main__":
63
  demo.launch()
 
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 sepia(input_img, num_copies):
9
  sepia_filter = np.array([
 
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
  def zip_sepia_images(sepia_imgs):
21
+ temp_dir = tempfile.mkdtemp()
22
+ zip_path = os.path.join(temp_dir, "sepia_images.zip")
23
+ with ZipFile(zip_path, 'w') as zipf:
24
  for i, img in enumerate(sepia_imgs):
25
+ img_path = os.path.join(temp_dir, f"sepia_image_{i}.png")
26
+ # gr.utils.save_image(img_path, img)
27
+ zipf.write(img_path, os.path.basename(img_path))
28
+ return zip_path
 
 
 
 
 
 
 
 
 
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", visible=True)
36
+
37
+ input_img.change(fn=lambda x: x, inputs=input_img, outputs=gallery)
38
+ num_copies.change(fn=lambda x: x, inputs=num_copies, outputs=gallery)
39
+
 
 
40
  generate_btn = gr.Button("Generate Sepia Images")
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()