File size: 2,861 Bytes
408242e
483cf17
 
ba0a843
483cf17
 
 
 
 
 
 
012c6d8
483cf17
 
 
 
 
012c6d8
483cf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
012c6d8
 
483cf17
 
 
 
 
 
01ce57c
483cf17
 
 
 
 
 
012c6d8
7430fac
 
 
 
483cf17
 
 
 
 
8bfb11f
01ce57c
7430fac
012c6d8
483cf17
ba0a843
483cf17
 
019c117
f737883
483cf17
f737883
012c6d8
483cf17
012c6d8
 
f737883
ba0a843
483cf17
 
ba0a843
 
012c6d8
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
import gradio as gr
from PIL import Image, ImageOps, ImageDraw, ImageFont
import numpy as np

def apply_color(image, color):
    color = Image.new('RGB', image.size, color)
    colored_image = Image.blend(image.convert('RGB'), color, alpha=0.5)
    return colored_image

def add_design(image, design, position):
    if design:
        design = Image.open(design).convert("RGBA")
        image.paste(design, position, design)
    return image

def resize_image(image, size):
    if size:
        return image.resize((size, size))
    return image

def rotate_image(image, angle):
    if angle:
        return image.rotate(angle, expand=True)
    return image

def crop_image(image, crop_coords):
    if crop_coords:
        return image.crop(crop_coords)
    return image

def add_text(image, text, position, font_size, font_color):
    if text:
        draw = ImageDraw.Draw(image)
        font = ImageFont.load_default()
        draw.text(position, text, fill=font_color, font=font)
    return image

def process_image(image, color, design, design_position, text, text_position, font_size, font_color, resize, rotate, crop_sketch):
    crop_coords = get_crop_coords(crop_sketch)
    if color:
        image = apply_color(image, color)
    if design:
        image = add_design(image, design, design_position)
    if text:
        image = add_text(image, text, text_position, font_size, font_color)
    if resize:
        image = resize_image(image, resize)
    if rotate:
        image = rotate_image(image, rotate)
    if crop_coords:
        image = crop_image(image, crop_coords)
    return image

def get_crop_coords(crop_sketch):
    if crop_sketch:
        x_coords = [point[0] for point in crop_sketch]
        y_coords = [point[1] for point in crop_sketch]
        return (min(x_coords), min(y_coords), max(x_coords), max(y_coords))
    return None

design_position = (100, 100)  # Default position for design
text_position = (50, 50)      # Default position for text

interface = gr.Interface(
    fn=lambda image, color, design, design_position, text, text_position, font_size, font_color, resize, rotate, crop_sketch: process_image(
        image, color, design, design_position, text, text_position, font_size, font_color, resize, rotate, crop_sketch
    ),
    inputs=[
        gr.Image(type="pil"),
        gr.ColorPicker(label="Color"),
        gr.File(label="Design"),
        gr.Sketchpad(label="Design Position"),
        gr.Textbox(label="Text"),
        gr.Sketchpad(label="Text Position"),
        gr.Slider(10, 100, step=1, value=20, label="Font Size"),
        gr.ColorPicker(label="Font Color"),
        gr.Slider(50, 1000, step=1, value=100, label="Resize"),
        gr.Slider(0, 360, step=1, value=0, label="Rotate"),
        gr.Sketchpad(label="Crop Area")
    ],
    outputs=gr.Image(type="pil"),
    live=True
)

interface.launch()