Spaces:
Sleeping
Sleeping
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() |