Spaces:
Sleeping
Sleeping
import gradio as gr | |
from PIL import Image, ImageOps, ImageEnhance, ImageDraw | |
def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, color_picker, coordinates): | |
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) | |
# Change color of selected area | |
if coordinates: | |
draw = ImageDraw.Draw(img) | |
for coord in coordinates: | |
draw.rectangle(coord, fill=color_picker, outline=None) | |
return img | |
def parse_coordinates(coordinate_str): | |
coordinates = [] | |
if coordinate_str: | |
for coord in coordinate_str.split(';'): | |
try: | |
x1, y1, x2, y2 = map(int, coord.split(',')) | |
coordinates.append((x1, y1, x2, y2)) | |
except ValueError: | |
continue | |
return coordinates | |
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.ColorPicker(label="Select Color"), | |
gr.Textbox(label="Coordinates (x1,y1,x2,y2;...)", placeholder="Enter coordinates separated by semicolons") | |
], | |
outputs=gr.Image(), | |
live=True, | |
title="Advanced Image Editor", | |
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, and change the color of specific areas." | |
) | |
interface.launch() |