Spaces:
Sleeping
Sleeping
File size: 3,912 Bytes
408242e a8602f1 4437e45 408242e 28d9e4e e12ff47 e910e8f e12ff47 e910e8f e12ff47 e910e8f e12ff47 a8602f1 e12ff47 4437e45 13863ad 4437e45 a8602f1 4437e45 408242e 4437e45 e12ff47 4437e45 a8602f1 4437e45 408242e 4437e45 e7ff32b |
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 86 87 88 |
import gradio as gr
from PIL import Image, ImageOps, ImageEnhance
import torch
from diffusers import DiffusionPipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
if torch.cuda.is_available():
torch.cuda.max_memory_allocated(device=device)
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
pipe.enable_xformers_memory_efficient_attention()
pipe = pipe.to(device)
else:
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
pipe = pipe.to(device)
def edit_image(image, operation, *args):
if operation == "rotate":
angle = int(args[0])
image = image.rotate(angle, expand=True)
elif operation == "crop":
left, top, right, bottom = map(int, args)
image = image.crop((left, top, right, bottom))
elif operation == "resize":
width, height = map(int, args)
image = image.resize((width, height))
elif operation == "flip":
if args[0] == "horizontal":
image = ImageOps.mirror(image)
else:
image = ImageOps.flip(image)
elif operation == "color":
factor = float(args[0])
image = ImageEnhance.Color(image).enhance(factor)
return image
def create_demo():
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Image Editor")
with gr.Row():
with gr.Column():
edit_operation = gr.Dropdown(choices=["rotate", "crop", "resize", "flip", "color"], label="Edit Operation")
input_args = gr.State([])
angle = gr.Slider(0, 360, step=1, label="Angle", visible=False)
left = gr.Slider(0, 500, step=1, label="Left", visible=False)
top = gr.Slider(0, 500, step=1, label="Top", visible=False)
right = gr.Slider(0, 500, step=1, label="Right", visible=False)
bottom = gr.Slider(0, 500, step=1, label="Bottom", visible=False)
width = gr.Slider(50, 1000, step=1, label="Width", visible=False)
height = gr.Slider(50, 1000, step=1, label="Height", visible=False)
flip_direction = gr.Dropdown(choices=["horizontal", "vertical"], label="Direction", visible=False)
color_factor = gr.Slider(0.1, 2.0, step=0.1, label="Color Factor", visible=False)
edit_button = gr.Button("Edit Image")
uploaded_image = gr.Image(label="Upload Image", type="pil")
edited_image = gr.Image(label="Edited Image", type="pil", interactive=True)
def update_inputs(operation):
if operation == "rotate":
return [gr.update(visible=True), angle]
elif operation == "crop":
return [gr.update(visible=True), left, top, right, bottom]
elif operation == "resize":
return [gr.update(visible=True), width, height]
elif operation == "flip":
return [gr.update(visible=True), flip_direction]
elif operation == "color":
return [gr.update(visible=True), color_factor]
else:
return []
edit_operation.change(fn=update_inputs, inputs=[edit_operation], outputs=[angle, left, top, right, bottom, width, height, flip_direction, color_factor])
edit_button.click(
fn=lambda img_data, operation, *args: edit_image(img_data, operation, *args),
inputs=[uploaded_image, edit_operation, angle, left, top, right, bottom, width, height, flip_direction, color_factor],
outputs=[edited_image]
)
return demo
demo = create_demo()
demo.queue().launch()
|