Spaces:
Sleeping
Sleeping
import gradio as gr | |
from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw | |
import numpy as np | |
def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop, resize, blur, sharpness, draw_text, text_position, text_color, text_size): | |
img = Image.open(image) | |
if grayscale: | |
img = ImageOps.grayscale(img) | |
if flip: | |
img = ImageOps.flip(img) | |
if rotate: | |
img = img.rotate(rotate) | |
# Apply brightness | |
enhancer = ImageEnhance.Brightness(img) | |
img = enhancer.enhance(brightness) | |
# Apply contrast | |
enhancer = ImageEnhance.Contrast(img) | |
img = enhancer.enhance(contrast) | |
# Apply color | |
enhancer = ImageEnhance.Color(img) | |
img = enhancer.enhance(color) | |
# Apply crop | |
if crop: | |
img = img.crop(crop) | |
# Apply resize | |
if resize: | |
img = img.resize(resize) | |
# Apply blur | |
if blur > 0: | |
img = img.filter(ImageFilter.GaussianBlur(blur)) | |
# Apply sharpness | |
enhancer = ImageEnhance.Sharpness(img) | |
img = enhancer.enhance(sharpness) | |
# Draw text | |
if draw_text: | |
draw = ImageDraw.Draw(img) | |
draw.text(text_position, draw_text, fill=text_color, font=None, anchor=None, spacing=4, align="left") | |
return img | |
def get_crop_coordinates(crop_start, crop_end): | |
return (crop_start[0], crop_start[1], crop_end[0], crop_end[1]) | |
interface = gr.Interface( | |
fn=edit_image, | |
inputs=[ | |
gr.Image(type="filepath", label="Upload Image"), | |
gr.Checkbox(label="Grayscale"), | |
gr.Checkbox(label="Flip Vertically"), | |
gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle"), | |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"), | |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"), | |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"), | |
gr.Textbox(label="Crop (left, upper, right, lower)", placeholder="e.g., 100,100,400,400"), | |
gr.Textbox(label="Resize (width, height)", placeholder="e.g., 800,600"), | |
gr.Slider(minimum=0, maximum=10, step=0.1, value=0, label="Blur"), | |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Sharpness"), | |
gr.Textbox(label="Draw Text", placeholder="e.g., Hello World"), | |
gr.Textbox(label="Text Position (x, y)", placeholder="e.g., 100,100"), | |
gr.ColorPicker(label="Text Color"), | |
gr.Slider(minimum=10, maximum=100, step=1, value=30, label="Text Size") | |
], | |
outputs=gr.Image(), | |
live=True, | |
title="Advanced Image Editor", | |
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, cropping, resizing, blurring, and adding text." | |
) | |
interface.launch() | |