diff options
| author | adamhrv <adam@ahprojects.com> | 2018-12-05 12:00:15 +0100 |
|---|---|---|
| committer | adamhrv <adam@ahprojects.com> | 2018-12-05 12:00:15 +0100 |
| commit | 90abf459d1df1f21960c1d653a1f936d1ec30256 (patch) | |
| tree | facab8e9bac6c56e69c369c2140cdbea218a01df /megapixels/app/utils/im_utils.py | |
| parent | 0529d4cd1618016319e995c37aa118bf8c2d501b (diff) | |
.
Diffstat (limited to 'megapixels/app/utils/im_utils.py')
| -rw-r--r-- | megapixels/app/utils/im_utils.py | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/megapixels/app/utils/im_utils.py b/megapixels/app/utils/im_utils.py index a0f23cd2..d5e92aa3 100644 --- a/megapixels/app/utils/im_utils.py +++ b/megapixels/app/utils/im_utils.py @@ -22,6 +22,16 @@ import datetime +def is_grayscale(im, threshold=5): + """Returns True if image is grayscale + :param im: (numpy.array) image + :return (bool) of if image is grayscale""" + b = im[:,:,0] + g = im[:,:,1] + mean = np.mean(np.abs(g - b)) + return mean < threshold + + def compute_features(fe,frames,phashes,phash_thresh=1): """ Get vector embedding using FeatureExtractor @@ -40,7 +50,7 @@ def compute_features(fe,frames,phashes,phash_thresh=1): return vals -def ensure_pil(im, bgr2rgb=False): +def np2pil(im, swap=True): """Ensure image is Pillow format :param im: image in numpy or PIL.Image format :returns: image in Pillow RGB format @@ -49,35 +59,44 @@ def ensure_pil(im, bgr2rgb=False): im.verify() return im except: - if bgr2rgb: + if swap: im = cv.cvtColor(im,cv.COLOR_BGR2RGB) return Image.fromarray(im.astype('uint8'), 'RGB') -def ensure_np(im): +def pil2np(im, swap=True): """Ensure image is Numpy.ndarry format :param im: image in numpy or PIL.Image format :returns: image in Numpy uint8 format """ if type(im) == np.ndarray: - return im - return np.asarray(im, np.uint8) + return im + im = np.asarray(im, np.uint8) + if swap: + im = cv.cvtColor(im, cv.COLOR_RGB2BGR) + return im -def resize(im,width=0,height=0): +def resize(im, width=0, height=0): """resize image using imutils. Use w/h=[0 || None] to prioritize other edge size :param im: a Numpy.ndarray image :param wh: a tuple of (width, height) """ + # TODO change to cv.resize and add algorithm choices w = width h = height if w is 0 and h is 0: return im elif w > 0 and h > 0: - return imutils.resize(im,width=w,height=h) + ws = im.shape[1] / w + hs = im.shape[0] / h + if ws > hs: + return imutils.resize(im, width=w) + else: + return imutils.resize(im, height=h) elif w > 0 and h is 0: - return imutils.resize(im,width=w) + return imutils.resize(im, width=w) elif w is 0 and h > 0: - return imutils.resize(im,height=h) + return imutils.resize(im, height=h) else: return im |
