gaur3009 commited on
Commit
ba0a843
1 Parent(s): 28d9e4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -86
app.py CHANGED
@@ -1,87 +1,28 @@
1
  import gradio as gr
2
- from PIL import Image, ImageOps, ImageEnhance
3
- import torch
4
- from diffusers import DiffusionPipeline
5
-
6
- device = "cuda" if torch.cuda.is_available() else "cpu"
7
-
8
- if torch.cuda.is_available():
9
- torch.cuda.max_memory_allocated(device=device)
10
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
11
- pipe.enable_xformers_memory_efficient_attention()
12
- pipe = pipe.to(device)
13
- else:
14
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
15
- pipe = pipe.to(device)
16
-
17
- def edit_image(image, operation, *args):
18
- if operation == "rotate":
19
- angle = int(args[0])
20
- image = image.rotate(angle, expand=True)
21
- elif operation == "crop":
22
- left, top, right, bottom = map(int, args)
23
- image = image.crop((left, top, right, bottom))
24
- elif operation == "resize":
25
- width, height = map(int, args)
26
- image = image.resize((width, height))
27
- elif operation == "flip":
28
- if args[0] == "horizontal":
29
- image = ImageOps.mirror(image)
30
- else:
31
- image = ImageOps.flip(image)
32
- elif operation == "color":
33
- factor = float(args[0])
34
- image = ImageEnhance.Color(image).enhance(factor)
35
-
36
- return image
37
-
38
- def create_demo():
39
- with gr.Blocks() as demo:
40
- with gr.Row():
41
- gr.Markdown("# Image Editor")
42
-
43
- with gr.Row():
44
- with gr.Column():
45
- edit_operation = gr.Dropdown(choices=["rotate", "crop", "resize", "flip", "color"], label="Edit Operation")
46
- input_args = gr.State([])
47
-
48
- angle = gr.Slider(0, 360, step=1, label="Angle", visible=False)
49
- left = gr.Slider(0, 500, step=1, label="Left", visible=False)
50
- top = gr.Slider(0, 500, step=1, label="Top", visible=False)
51
- right = gr.Slider(0, 500, step=1, label="Right", visible=False)
52
- bottom = gr.Slider(0, 500, step=1, label="Bottom", visible=False)
53
- width = gr.Slider(50, 1000, step=1, label="Width", visible=False)
54
- height = gr.Slider(50, 1000, step=1, label="Height", visible=False)
55
- flip_direction = gr.Dropdown(choices=["horizontal", "vertical"], label="Direction", visible=False)
56
- color_factor = gr.Slider(0.1, 2.0, step=0.1, label="Color Factor", visible=False)
57
-
58
- edit_button = gr.Button("Edit Image")
59
- uploaded_image = gr.Image(label="Upload Image", type="pil")
60
- edited_image = gr.Image(label="Edited Image", type="pil", interactive=True)
61
-
62
- def update_inputs(operation):
63
- if operation == "rotate":
64
- return [gr.update(visible=True), angle]
65
- elif operation == "crop":
66
- return [gr.update(visible=True), left, top, right, bottom]
67
- elif operation == "resize":
68
- return [gr.update(visible=True), width, height]
69
- elif operation == "flip":
70
- return [gr.update(visible=True), flip_direction]
71
- elif operation == "color":
72
- return [gr.update(visible=True), color_factor]
73
- else:
74
- return []
75
-
76
- edit_operation.change(fn=update_inputs, inputs=[edit_operation], outputs=[angle, left, top, right, bottom, width, height, flip_direction, color_factor])
77
-
78
- edit_button.click(
79
- fn=lambda img_data, operation, *args: edit_image(img_data, operation, *args),
80
- inputs=[uploaded_image, edit_operation, angle, left, top, right, bottom, width, height, flip_direction, color_factor],
81
- outputs=[edited_image]
82
- )
83
-
84
- return demo
85
-
86
- demo = create_demo()
87
- demo.queue().launch()
 
1
  import gradio as gr
2
+ from PIL import Image, ImageOps
3
+
4
+ def edit_image(image, grayscale, flip, rotate):
5
+ img = Image.open(image)
6
+ if grayscale:
7
+ img = ImageOps.grayscale(img)
8
+ if flip:
9
+ img = ImageOps.flip(img)
10
+ if rotate:
11
+ img = img.rotate(rotate)
12
+ return img
13
+
14
+ interface = gr.Interface(
15
+ fn=edit_image,
16
+ inputs=[
17
+ gr.inputs.Image(type="file", label="Upload Image"),
18
+ gr.inputs.Checkbox(label="Grayscale"),
19
+ gr.inputs.Checkbox(label="Flip Vertically"),
20
+ gr.inputs.Slider(minimum=0, maximum=360, step=1, default=0, label="Rotate Angle")
21
+ ],
22
+ outputs="image",
23
+ live=True,
24
+ title="Image Editor",
25
+ description="Upload an image and apply transformations"
26
+ )
27
+
28
+ interface.launch()