Update tools/edit.py

#28
Files changed (1) hide show
  1. tools/edit.py +115 -26
tools/edit.py CHANGED
@@ -1,38 +1,127 @@
1
- import torch
2
- from PIL import Image
3
- from diffusers import AutoPipelineForImage2Image
4
 
5
- pipe = None
6
 
 
 
 
7
 
8
- def load_model():
9
- global pipe
 
 
10
 
11
- if pipe is None:
12
- pipe = AutoPipelineForImage2Image.from_pretrained(
13
- "stabilityai/sd-turbo",
14
- torch_dtype=torch.float32
15
- )
16
- pipe.to("cpu")
17
 
18
- return pipe
 
 
 
19
 
 
 
20
 
21
- def edit_image(image_path: str, prompt: str):
 
22
 
23
- if image_path is None:
24
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- model = load_model()
27
 
28
- image = Image.open(image_path).convert("RGB")
29
 
30
- result = model(
31
- prompt=prompt,
32
- image=image,
33
- strength=0.75,
34
- guidance_scale=3.0,
35
- num_inference_steps=20
36
- )
37
 
38
- return result.images[0]
 
1
+ import os
2
+ from PIL import Image, ImageEnhance, ImageFilter
 
3
 
 
4
 
5
+ def edit_image(image_path: str, prompt: str):
6
+ """
7
+ Edit an existing image based on the user's prompt.
8
 
9
+ Parameters
10
+ ----------
11
+ image_path : str
12
+ Path to uploaded/current image
13
 
14
+ prompt : str
15
+ User request
 
 
 
 
16
 
17
+ Returns
18
+ -------
19
+ PIL.Image
20
+ """
21
 
22
+ if image_path is None:
23
+ raise ValueError("No image selected.")
24
 
25
+ if not os.path.exists(image_path):
26
+ raise FileNotFoundError(image_path)
27
 
28
+ img = Image.open(image_path).convert("RGB")
29
+
30
+ prompt = prompt.lower()
31
+
32
+ # ---------------------------------
33
+ # Brightness
34
+ # ---------------------------------
35
+
36
+ if "bright" in prompt:
37
+
38
+ enhancer = ImageEnhance.Brightness(img)
39
+
40
+ img = enhancer.enhance(1.4)
41
+
42
+ # ---------------------------------
43
+ # Contrast
44
+ # ---------------------------------
45
+
46
+ elif "contrast" in prompt:
47
+
48
+ enhancer = ImageEnhance.Contrast(img)
49
+
50
+ img = enhancer.enhance(1.6)
51
+
52
+ # ---------------------------------
53
+ # Blur
54
+ # ---------------------------------
55
+
56
+ elif "blur" in prompt:
57
+
58
+ img = img.filter(ImageFilter.GaussianBlur(4))
59
+
60
+ # ---------------------------------
61
+ # Sharpen
62
+ # ---------------------------------
63
+
64
+ elif "sharpen" in prompt:
65
+
66
+ img = img.filter(ImageFilter.SHARPEN)
67
+
68
+ # ---------------------------------
69
+ # Black & White
70
+ # ---------------------------------
71
+
72
+ elif "black" in prompt or "white" in prompt:
73
+
74
+ img = img.convert("L").convert("RGB")
75
+
76
+ # ---------------------------------
77
+ # Grayscale
78
+ # ---------------------------------
79
+
80
+ elif "grayscale" in prompt:
81
+
82
+ img = img.convert("L").convert("RGB")
83
+
84
+ # ---------------------------------
85
+ # Flip
86
+ # ---------------------------------
87
+
88
+ elif "flip" in prompt:
89
+
90
+ img = img.transpose(Image.FLIP_LEFT_RIGHT)
91
+
92
+ # ---------------------------------
93
+ # Rotate
94
+ # ---------------------------------
95
+
96
+ elif "rotate" in prompt:
97
+
98
+ img = img.rotate(90, expand=True)
99
+
100
+ # ---------------------------------
101
+ # Enhance
102
+ # ---------------------------------
103
+
104
+ elif "enhance" in prompt:
105
+
106
+ img = img.filter(ImageFilter.DETAIL)
107
+
108
+ # ---------------------------------
109
+ # Anime / Pixar / Ghibli
110
+ # ---------------------------------
111
+
112
+ elif (
113
+ "anime" in prompt
114
+ or "pixar" in prompt
115
+ or "ghibli" in prompt
116
+ or "cartoon" in prompt
117
+ ):
118
+
119
+ # Placeholder
120
 
121
+ print("Connect Image Editing API here")
122
 
123
+ else:
124
 
125
+ print("No matching edit command.")
 
 
 
 
 
 
126
 
127
+ return img