diff --git a/core/imagelib/__init__.py b/core/imagelib/__init__.py index 682bd65..23fb889 100644 --- a/core/imagelib/__init__.py +++ b/core/imagelib/__init__.py @@ -14,7 +14,7 @@ from .reduce_colors import reduce_colors from .color_transfer import color_transfer, color_transfer_mix, color_transfer_sot, color_transfer_mkl, color_transfer_idt, color_hist_match, reinhard_color_transfer, linear_color_transfer -from .common import normalize_channels, cut_odd_image, overlay_alpha_image +from .common import random_crop, normalize_channels, cut_odd_image, overlay_alpha_image from .SegIEPolys import * diff --git a/core/imagelib/common.py b/core/imagelib/common.py index 6566819..4219d7d 100644 --- a/core/imagelib/common.py +++ b/core/imagelib/common.py @@ -1,5 +1,16 @@ import numpy as np +def random_crop(img, w, h): + height, width = img.shape[:2] + + h_rnd = height - h + w_rnd = width - w + + y = np.random.randint(0, h_rnd) if h_rnd > 0 else 0 + x = np.random.randint(0, w_rnd) if w_rnd > 0 else 0 + + return img[y:y+height, x:x+width] + def normalize_channels(img, target_channels): img_shape_len = len(img.shape) if img_shape_len == 2: