File size: 3,223 Bytes
fd05616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6681bbb
fd05616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6681bbb
fd05616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import spaces
import numpy as np
from pixeloe.pixelize import pixelize
from PIL import Image
import gradio as gr

def pixelize_image(
    image,
    downscale_mode="contrast",
    target_size=128,
    patch_size=16,
    thickness=2,
    color_matching=True,
    upscale=True
):
    """
    Apply pixelization effect to an image or batch of images.
    """
    if isinstance(image, Image.Image):
        image = np.array(image)
    
    processed = pixelize(
        image,
        mode=downscale_mode,
        target_size=target_size,
        patch_size=patch_size,
        thickness=thickness,
        contrast=1.0,
        saturation=1.0,
        color_matching=color_matching,
        no_upscale=not upscale
    )
    
    return Image.fromarray(processed)

@spaces.GPU
def process_image(image, downscale_mode, target_size, patch_size, thickness, color_matching, upscale):
    if image is None:
        return None
    
    result = pixelize_image(
        image,
        downscale_mode=downscale_mode,
        target_size=target_size,
        patch_size=patch_size,
        thickness=thickness,
        color_matching=color_matching,
        upscale=upscale
    )
    
    return result

def create_pixelize_tab():
    with gr.Tab("Pixelizer"):
        
        with gr.Row():
            with gr.Column():
                input_image = gr.Image(label="Input Image", type="pil", height=256)
                
                downscale_mode = gr.Dropdown(
                    choices=["contrast", "bicubic", "nearest", "center", "k-centroid"],
                    value="contrast",
                    label="Downscale Mode"
                )
                
                target_size = gr.Slider(
                    minimum=8,
                    maximum=1024,
                    value=128,
                    step=8,
                    label="Target Size"
                )
                
                patch_size = gr.Slider(
                    minimum=4,
                    maximum=32,
                    value=16,
                    step=2,
                    label="Patch Size"
                )
                
                thickness = gr.Slider(
                    minimum=1,
                    maximum=16,
                    value=2,
                    step=1,
                    label="Thickness"
                )
                
                color_matching = gr.Checkbox(
                    value=True,
                    label="Color Matching"
                )
                
                upscale = gr.Checkbox(
                    value=True,
                    label="Upscale"
                )
                
                process_btn = gr.Button("Process Image")
            
            with gr.Column():
                output_image = gr.Image(label="Processed Image", height=256)
        
        # Set up processing event
        process_btn.click(
            fn=process_image,
            inputs=[
                input_image,
                downscale_mode,
                target_size,
                patch_size,
                thickness,
                color_matching,
                upscale
            ],
            outputs=output_image
        )