-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_functions.py
320 lines (266 loc) · 10.9 KB
/
edit_functions.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from PIL import Image
import cv2
import numpy as np
class AllEditFunctions:
@staticmethod
def _apply_horizontal_flip_image(img_properties=None, img=None):
"""
Applies a horizontal flip to the image if the image_properties.is_flipped_horz is True
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be flipped
Returns:
numpy.ndarray: The flipped image
"""
image_properties = img_properties
image = img
image = Image.fromarray(image)
flipped_image = image.transpose(
method=Image.FLIP_LEFT_RIGHT) if image_properties.is_flipped_horz else image
numpy_image = np.array(flipped_image)
return numpy_image
@staticmethod
def _apply_vertical_flip_image(img_properties=None, img=None):
"""
Applies a vertical flip to the image if the image_properties.is_flipped_vert is True
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be flipped
Returns:
numpy.ndarray: The flipped image
"""
image_properties = img_properties
image = img
image = Image.fromarray(image)
flipped_image = image.transpose(
method=Image.FLIP_TOP_BOTTOM) if image_properties.is_flipped_vert else image
numpy_image = np.array(flipped_image)
return numpy_image
@staticmethod
def _apply_grayscale_to_image(img_properties=None, img=None):
"""
Applies a grayscale to the image if the image_properties.is_grayscaled is True
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be grayscaled
Returns:
numpy.ndarray: The grayscaled image
"""
image_properties = img_properties
image = img
grayscale_image = cv2.cvtColor(
image, cv2.COLOR_BGR2GRAY) if image_properties.is_grayscaled else image
return grayscale_image
@staticmethod
def _apply_sepia_to_image(img_properties=None, img=None):
"""
Applies a sepia filter to the image if the image_properties.is_sepia is True
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be sepia'd
Returns:
numpy.ndarray: The sepia'd image
"""
image_properties = img_properties
image = img
sepia_image = image
if image_properties.is_sepia:
array_image = np.array(image, dtype=np.float64)
sepia_filter = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
sepia_image = np.dot(array_image, sepia_filter.T).clip(
0, 255).astype(np.uint8)
sepia_image = np.array(sepia_image, dtype=np.uint8)
return sepia_image
@staticmethod
def _apply_rotation_to_image(img_properties=None, img=None):
"""
Applies a rotation to the image if the image_properties.rotation is not 0
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be rotated
Returns:
numpy.ndarray: The rotated image
"""
image_properties = img_properties
image = img
angle = image_properties.rotation
image = Image.fromarray(image)
rotated_image = image.rotate(
angle=angle, resample=Image.NEAREST, expand=True)
numpy_image = np.array(rotated_image)
resize_height = image_properties.resize_image_height
resize_width = image_properties.resize_image_width
if angle == 90 or angle == 270:
image_properties.altered_image_height = resize_width
image_properties.altered_image_width = resize_height
elif angle in [0, 180, 360]:
image_properties.altered_image_height = resize_height
image_properties.altered_image_width = resize_width
return numpy_image
@staticmethod
def _apply_resize_to_image(img_properties=None, img=None):
"""
Applies a resize to the image if the image_properties.is_resized is True
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be resized
Returns:
numpy.ndarray: The resized image
"""
image_properties = img_properties
image = img
resized_image = cv2.resize(image, (image_properties.altered_image_width,
image_properties.altered_image_height))
return resized_image
@staticmethod
def _apply_all_basic_edits(img_properties=None, img=None):
"""
Applies all basic edits to the image
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be edited
Returns:
numpy.ndarray: The edited image
"""
image_properties = img_properties
resize = image_properties.is_resized
image = img
image = AllEditFunctions._apply_rotation_to_image(
image_properties, image)
if resize is True:
image = AllEditFunctions._apply_resize_to_image(
image_properties, image)
image = AllEditFunctions._apply_grayscale_to_image(
image_properties, image)
image = AllEditFunctions._apply_horizontal_flip_image(
image_properties, image)
image = AllEditFunctions._apply_vertical_flip_image(
image_properties, image)
image = AllEditFunctions._apply_sepia_to_image(image_properties, image)
return image
@staticmethod
def _convert_brightness(num):
"""
Converts the brightness value to a value that can be used by OpenCV
"""
return num * 2.54 - 127
@staticmethod
def _convert_contrast(num):
"""
Converts the contrast value to a value that can be used by OpenCV
"""
return num * 0.02
@staticmethod
def _convert_hue(num):
"""
Converts the hue value to a value that can be used by OpenCV
"""
return int(num * 1.79)
@staticmethod
def _apply_blur_to_image(img_properties=None, img=None):
"""
Applies a blur to the image if the image_properties.blur is not 0
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be blurred
Returns:
numpy.ndarray: The blurred image
"""
image_properties = img_properties
image = img
blur_value = image_properties.blur
# this is how distorted each pixel will become
kernel_size = (blur_value, blur_value)
# the kernel size had to be a positive, ODD number
kernel_size = tuple(size + 1 if size %
2 == 0 else size for size in kernel_size)
# applies the actual blur
image = cv2.blur(img, kernel_size)
return image
@staticmethod
def _apply_hue_to_image(img_properties=None, img=None):
"""
Applies a hue to the image if the image_properties.hue is not 0
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be hue'd
Returns:
numpy.ndarray: The hue'd image
"""
image_properties = img_properties
image = img
hue_value = AllEditFunctions._convert_hue(image_properties.hue)
# Convert image to HSV
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Change the hue channel
# Hue values range from 0 to 179
hsv_image[:, :, 0] = (hsv_image[:, :, 0] + hue_value) % 180
# Convert back to BGR
image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
return image
@staticmethod
def _apply_saturation_to_image(img_properties=None, img=None):
"""
Applies a saturation to the image if the image_properties.saturation is not 0
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be saturation'd
Returns:
numpy.ndarray: The saturation'd image
"""
image_properties = img_properties
image = img
saturation_value = image_properties.saturation
saturation_factor = 1 + saturation_value
# Convert image to HSV
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Saturation values range 0 - 255
hsv_image[:, :, 1] = np.clip(
hsv_image[:, :, 1] * saturation_factor, 0, 255).astype(np.uint8)
# Convert back to BGR
image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
return image
@staticmethod
def _apply_brightness_and_contrast_to_image(img_properties=None, img=None):
"""
Applies a brightness and contrast to the image if the image_properties.brightness or image_properties.contrast is not 0
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be brightness'd and contrast'd
Returns:
numpy.ndarray: The brightness'd and contrast'd image
"""
image_properties = img_properties
image = img
brightness_value = image_properties.brightness
contrast_value = image_properties.contrast
brightness_factor = AllEditFunctions._convert_brightness(
brightness_value)
contrast_factor = AllEditFunctions._convert_contrast(contrast_value)
# applies the actual brightness change
image = cv2.convertScaleAbs(
image, alpha=contrast_factor, beta=brightness_factor)
return image
@staticmethod
def _apply_all_advanced_edits(img_properties=None, img=None):
"""
Applies all advanced edits to the image
Parameters:
img_properties (ImageProperties): The image properties object
img (numpy.ndarray): The image to be edited
Returns:
numpy.ndarray: The edited image
"""
image_properties = img_properties
image = img
image = AllEditFunctions._apply_blur_to_image(image_properties, image)
image = AllEditFunctions._apply_brightness_and_contrast_to_image(
image_properties, image)
if image_properties.is_grayscaled == False:
image = AllEditFunctions._apply_hue_to_image(
image_properties, image)
image = AllEditFunctions._apply_saturation_to_image(
image_properties, image)
return image