mobenta's picture
Upload 2 files
0d77952 verified
import gradio as gr
from PIL import Image, ImageEnhance, ImageFilter
import numpy as np
def edit_image(image, crop, resize_width, resize_height, rotate, flip, brightness, contrast, blur):
img = Image.fromarray(image)
# Cropping (crop box should be in left, top, right, bottom format)
if crop != (0, 0, img.width, img.height):
crop_values = [int(x) for x in crop.split(",")]
img = img.crop(crop_values)
# Resizing
if resize_width and resize_height:
img = img.resize((resize_width, resize_height))
# Rotation
if rotate:
img = img.rotate(rotate)
# Flipping
if flip == "Horizontal":
img = img.transpose(Image.FLIP_LEFT_RIGHT)
elif flip == "Vertical":
img = img.transpose(Image.FLIP_TOP_BOTTOM)
# Brightness Adjustment
if brightness != 1:
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(brightness)
# Contrast Adjustment
if contrast != 1:
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(contrast)
# Blurring
if blur > 0:
img = img.filter(ImageFilter.GaussianBlur(blur))
return np.array(img)
# Define the interface
iface = gr.Interface(
fn=edit_image,
inputs=[
gr.Image(type="numpy"), # Input image without the tool argument
gr.Textbox(label="Crop (left, top, right, bottom)", value="0, 0, 500, 500"),
gr.Slider(minimum=10, maximum=1000, label="Resize Width", value=500),
gr.Slider(minimum=10, maximum=1000, label="Resize Height", value=500),
gr.Slider(minimum=-360, maximum=360, label="Rotate (degrees)", value=0),
gr.Radio(choices=["None", "Horizontal", "Vertical"], label="Flip", value="None"),
gr.Slider(minimum=0.1, maximum=2.0, step=0.1, label="Brightness", value=1),
gr.Slider(minimum=0.1, maximum=2.0, step=0.1, label="Contrast", value=1),
gr.Slider(minimum=0, maximum=10, step=1, label="Blur", value=0)
],
outputs="image",
title="Image Editor with Cropping, Resizing, Rotation, Brightness, and More",
description="Upload an image and apply various editing features such as cropping, resizing, rotation, flipping, brightness/contrast adjustments, and blurring."
)
iface.launch()