File size: 2,712 Bytes
16b7d61
e50d873
16b7d61
 
8579fd1
 
e50d873
 
16b7d61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8579fd1
e50d873
8579fd1
 
e50d873
 
16b7d61
 
 
 
 
 
 
 
 
 
 
 
8579fd1
6b7517b
8579fd1
 
 
 
 
 
16b7d61
8579fd1
16b7d61
8579fd1
e50d873
8579fd1
 
c7183fb
 
 
 
8579fd1
 
 
 
e50d873
8579fd1
16b7d61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import numpy as np
from PIL import Image
import random
import tempfile
import os
from zipfile import ZipFile

def apply_random_transformations(image):
    # Randomly apply transformations: rotate, zoom, or add noise
    transformations = [rotate_image, zoom_image, add_noise]
    random.shuffle(transformations)  # Shuffle to apply them in random order
    
    for transform in transformations:
        if random.choice([True, False]):  # Randomly decide whether to apply this transformation
            image = transform(image)
    return image

def rotate_image(image):
    angle = random.randint(-30, 30)  # Random angle
    return image.rotate(angle)

def zoom_image(image):
    zoom_factor = random.uniform(0.9, 1.1)  # Random zoom factor
    width, height = image.size
    new_width = int(width * zoom_factor)
    new_height = int(height * zoom_factor)
    return image.resize((new_width, new_height), Image.LANCZOS)

def add_noise(image_np):
    noise = np.random.randn(*image_np.shape) * 25  # Adjust intensity as needed
    noisy_image = np.clip(image_np + noise, 0, 255)
    return Image.fromarray(noisy_image.astype(np.uint8))

def sepia(input_img, num_copies):
    sepia_filter = np.array([
        [0.393, 0.769, 0.189], 
        [0.349, 0.686, 0.168], 
        [0.272, 0.534, 0.131]
    ])
    input_img = np.array(input_img) / 255.0
    sepia_imgs = []
    for _ in range(num_copies):
        # Apply sepia filter
        sepia_img = np.dot(input_img[..., :3], sepia_filter.T)
        sepia_img = np.clip(sepia_img, 0, 1) * 255
        img_pil = Image.fromarray(sepia_img.astype(np.uint8))

        # Apply random transformations
        img_transformed = apply_random_transformations(img_pil)
        sepia_imgs.append(img_transformed)
    
    return sepia_imgs

def zip_sepia_images(sepia_imgs):
    temp_dir = tempfile.mkdtemp()
    zip_path = os.path.join(temp_dir, "sepia_images.zip")
    with ZipFile(zip_path, 'w') as zipf:
        for i, img in enumerate(sepia_imgs):
            img_path = os.path.join(temp_dir, f"sepia_image_{i}.png")
            img.save(img_path)
            zipf.write(img_path, os.path.basename(img_path))
    shutil.rmtree(temp_dir)  # Clean up after creating the ZIP
    return zip_path

with gr.Blocks() as demo:
    with gr.Row():
        input_img = gr.Image()
        num_copies = gr.Number(value=1)
        gallery = gr.Gallery()
        download_btn = gr.File()
        
    generate_btn = gr.Button("Generate Sepia Images")
    generate_btn.click(fn=sepia, inputs=[input_img, num_copies], outputs=gallery)
    generate_btn.click(fn=zip_sepia_images, inputs=gallery, outputs=download_btn)

if __name__ == "__main__":
    demo.launch()