Skip to content

Commit 3fa8be4

Browse files
authored
add padding_to_fix_sized_and_save_imgs
add padding_to_fix_sized_and_save_imgs
1 parent 57e6457 commit 3fa8be4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

img_process.py

+26
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,32 @@ def __dataset_padding(src_mat, ori_size, out_size=[[128, 128]], dtype=np.float32
554554
return out_mat
555555

556556

557+
def padding_to_fix_sized_and_save_imgs(source_path, save_path, padding_height, padding_width,
558+
source_extension='png'):
559+
"""
560+
padding zeros to images data to get fix sized images and save them. But if the source images's size is larger than
561+
the required size, do croping on them
562+
:param source_path: source images path: the struct should be source_path/images files
563+
:param save_path: the processed images' save path
564+
:param padding_height: output image's height
565+
:param padding_width: output image's width
566+
:param extension: source images' file extension
567+
:return: True
568+
"""
569+
if not os.path.exists(source_path):
570+
raise FileExistsError('path not found! : %s' % source_path)
571+
pbar = tqdm(os.scandir(source_path))
572+
for img_files in pbar:
573+
extension = os.path.splitext(img_files.path)[1][1:]
574+
if extension == source_extension:
575+
pbar.set_description("Processing %s" % img_files.name)
576+
img_data = cv2.imread(img_files.path, -1)
577+
filename_no_extension, _ = os.path.splitext(img_files.name)
578+
padding_img = __matrix_padding_force_to_get_fix_sized_matrix(img_data, padding_height, padding_width)
579+
cv2.imwrite(os.path.join(save_path, category.name, filename_no_extension+'.png'), padding_img)
580+
return True
581+
582+
557583
def padding_to_fix_sized_and_save_imgs_with_category(source_path, save_path, padding_height, padding_width,
558584
source_extension='png'):
559585
"""

0 commit comments

Comments
 (0)