Rahatara commited on
Commit
0ce663a
·
verified ·
1 Parent(s): c7183fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -65
app.py CHANGED
@@ -1,36 +1,6 @@
1
  import gradio as gr
2
  import numpy as np
3
- from PIL import Image
4
- import random
5
- import tempfile
6
- import os
7
- from zipfile import ZipFile
8
-
9
- def apply_random_transformations(image):
10
- # Randomly apply transformations: rotate, zoom, or add noise
11
- transformations = [rotate_image, zoom_image, add_noise]
12
- random.shuffle(transformations) # Shuffle to apply them in random order
13
-
14
- for transform in transformations:
15
- if random.choice([True, False]): # Randomly decide whether to apply this transformation
16
- image = transform(image)
17
- return image
18
-
19
- def rotate_image(image):
20
- angle = random.randint(-30, 30) # Random angle
21
- return image.rotate(angle)
22
-
23
- def zoom_image(image):
24
- zoom_factor = random.uniform(0.9, 1.1) # Random zoom factor
25
- width, height = image.size
26
- new_width = int(width * zoom_factor)
27
- new_height = int(height * zoom_factor)
28
- return image.resize((new_width, new_height), Image.LANCZOS)
29
-
30
- def add_noise(image_np):
31
- noise = np.random.randn(*image_np.shape) * 25 # Adjust intensity as needed
32
- noisy_image = np.clip(image_np + noise, 0, 255)
33
- return Image.fromarray(noisy_image.astype(np.uint8))
34
 
35
  def sepia(input_img, num_copies):
36
  sepia_filter = np.array([
@@ -38,41 +8,21 @@ def sepia(input_img, num_copies):
38
  [0.349, 0.686, 0.168],
39
  [0.272, 0.534, 0.131]
40
  ])
41
- input_img = np.array(input_img) / 255.0
42
- sepia_imgs = []
43
- for _ in range(num_copies):
44
- # Apply sepia filter
45
- sepia_img = np.dot(input_img[..., :3], sepia_filter.T)
46
- sepia_img = np.clip(sepia_img, 0, 1) * 255
47
- img_pil = Image.fromarray(sepia_img.astype(np.uint8))
48
-
49
- # Apply random transformations
50
- img_transformed = apply_random_transformations(img_pil)
51
- sepia_imgs.append(img_transformed)
52
 
53
- return sepia_imgs
54
-
55
- def zip_sepia_images(sepia_imgs):
56
- temp_dir = tempfile.mkdtemp()
57
- zip_path = os.path.join(temp_dir, "sepia_images.zip")
58
- with ZipFile(zip_path, 'w') as zipf:
59
- for i, img in enumerate(sepia_imgs):
60
- img_path = os.path.join(temp_dir, f"sepia_image_{i}.png")
61
- img.save(img_path)
62
- zipf.write(img_path, os.path.basename(img_path))
63
- shutil.rmtree(temp_dir) # Clean up after creating the ZIP
64
- return zip_path
65
 
66
- with gr.Blocks() as demo:
67
- with gr.Row():
68
- input_img = gr.Image()
69
- num_copies = gr.Number(value=1)
70
- gallery = gr.Gallery()
71
- download_btn = gr.File()
72
-
73
- generate_btn = gr.Button("Generate Sepia Images")
74
- generate_btn.click(fn=sepia, inputs=[input_img, num_copies], outputs=gallery)
75
- generate_btn.click(fn=zip_sepia_images, inputs=gallery, outputs=download_btn)
76
 
77
  if __name__ == "__main__":
78
- demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def sepia(input_img, num_copies):
6
  sepia_filter = np.array([
 
8
  [0.349, 0.686, 0.168],
9
  [0.272, 0.534, 0.131]
10
  ])
11
+ # Normalize input image to 0-1 range
12
+ input_img = input_img / 255.0
13
+ sepia_img = np.dot(input_img[...,:3], sepia_filter.T)
14
+ sepia_img = np.clip(sepia_img, 0, 1)
 
 
 
 
 
 
 
15
 
16
+ # Iterate to yield multiple sepia images
17
+ for _ in range(num_copies):
18
+ # Simulating a delay for demonstration, you might not need this
19
+ time.sleep(1)
20
+ yield (sepia_img * 255).astype(np.uint8)
 
 
 
 
 
 
 
21
 
22
+ demo = gr.Interface(fn=sepia,
23
+ inputs=[gr.Image(), gr.Number()],
24
+ outputs="image",
25
+ title="Sepia Tone Generator")
 
 
 
 
 
 
26
 
27
  if __name__ == "__main__":
28
+ demo.launch()