Update process_img.py
Browse files- process_img.py +47 -47
process_img.py
CHANGED
@@ -1,48 +1,48 @@
|
|
1 |
-
import numpy as np
|
2 |
-
from PIL import Image, ImageOps
|
3 |
-
import logging
|
4 |
-
|
5 |
-
class Image_Processor:
|
6 |
-
def __init__(self):
|
7 |
-
pass
|
8 |
-
def is_image_white_by_percentage(self,image_path, white_threshold):
|
9 |
-
image = image_path.convert('RGB')
|
10 |
-
image_np = np.array(image)
|
11 |
-
white_pixel = np.array([255, 255, 255])
|
12 |
-
white_pixels_count = np.sum(np.all(image_np == white_pixel, axis=-1))
|
13 |
-
total_pixels = image_np.shape[0] * image_np.shape[1]
|
14 |
-
white_pixel_percentage = (white_pixels_count / total_pixels) * 100
|
15 |
-
return white_pixel_percentage > white_threshold
|
16 |
-
|
17 |
-
def padding_white(self,image, output_size=(224, 224)):
|
18 |
-
# Ensure image is in RGB mode before padding
|
19 |
-
if image.mode != 'RGB':
|
20 |
-
image = image.convert('RGB')
|
21 |
-
new_image = ImageOps.pad(image, output_size, method=Image.Resampling.LANCZOS, color=(255, 255, 255))
|
22 |
-
return new_image
|
23 |
-
|
24 |
-
def resize_image_with_aspect_ratio(self,img):
|
25 |
-
target_size=
|
26 |
-
width, height = img.size
|
27 |
-
original_aspect_ratio = width / height
|
28 |
-
if width > height:
|
29 |
-
new_width = target_size
|
30 |
-
new_height = int(target_size / original_aspect_ratio)
|
31 |
-
else:
|
32 |
-
new_height = target_size
|
33 |
-
new_width = int(target_size * original_aspect_ratio)
|
34 |
-
resized_img = img.resize((new_width, new_height))
|
35 |
-
return resized_img
|
36 |
-
|
37 |
-
def get_processed_img(self,image):
|
38 |
-
white_thresh = self.is_image_white_by_percentage(image,50)
|
39 |
-
if white_thresh == True:
|
40 |
-
resized_image = self.resize_image_with_aspect_ratio(image)
|
41 |
-
final_image = self.padding_white(resized_image)
|
42 |
-
logging.info('Resized and Padded Image')
|
43 |
-
else:
|
44 |
-
final_image = self.resize_image_with_aspect_ratio(image)
|
45 |
-
logging.info('Resized Image')
|
46 |
-
|
47 |
-
final_image = final_image.convert('L') if final_image.mode != 'L' else final_image
|
48 |
return final_image
|
|
|
1 |
+
import numpy as np
|
2 |
+
from PIL import Image, ImageOps
|
3 |
+
import logging
|
4 |
+
|
5 |
+
class Image_Processor:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
def is_image_white_by_percentage(self,image_path, white_threshold):
|
9 |
+
image = image_path.convert('RGB')
|
10 |
+
image_np = np.array(image)
|
11 |
+
white_pixel = np.array([255, 255, 255])
|
12 |
+
white_pixels_count = np.sum(np.all(image_np == white_pixel, axis=-1))
|
13 |
+
total_pixels = image_np.shape[0] * image_np.shape[1]
|
14 |
+
white_pixel_percentage = (white_pixels_count / total_pixels) * 100
|
15 |
+
return white_pixel_percentage > white_threshold
|
16 |
+
|
17 |
+
def padding_white(self,image, output_size=(224, 224)):
|
18 |
+
# Ensure image is in RGB mode before padding
|
19 |
+
if image.mode != 'RGB':
|
20 |
+
image = image.convert('RGB')
|
21 |
+
new_image = ImageOps.pad(image, output_size, method=Image.Resampling.LANCZOS, color=(255, 255, 255))
|
22 |
+
return new_image
|
23 |
+
|
24 |
+
def resize_image_with_aspect_ratio(self,img):
|
25 |
+
target_size=336
|
26 |
+
width, height = img.size
|
27 |
+
original_aspect_ratio = width / height
|
28 |
+
if width > height:
|
29 |
+
new_width = target_size
|
30 |
+
new_height = int(target_size / original_aspect_ratio)
|
31 |
+
else:
|
32 |
+
new_height = target_size
|
33 |
+
new_width = int(target_size * original_aspect_ratio)
|
34 |
+
resized_img = img.resize((new_width, new_height))
|
35 |
+
return resized_img
|
36 |
+
|
37 |
+
def get_processed_img(self,image):
|
38 |
+
white_thresh = self.is_image_white_by_percentage(image,50)
|
39 |
+
if white_thresh == True:
|
40 |
+
resized_image = self.resize_image_with_aspect_ratio(image)
|
41 |
+
final_image = self.padding_white(resized_image)
|
42 |
+
logging.info('Resized and Padded Image')
|
43 |
+
else:
|
44 |
+
final_image = self.resize_image_with_aspect_ratio(image)
|
45 |
+
logging.info('Resized Image')
|
46 |
+
|
47 |
+
final_image = final_image.convert('L') if final_image.mode != 'L' else final_image
|
48 |
return final_image
|