From 5ae2b7641f276ced32e1d31523f99f42d0780540 Mon Sep 17 00:00:00 2001 From: iperov Date: Sat, 24 Jul 2021 09:09:53 +0400 Subject: [PATCH] fix to_grayscale() --- xlib/image/ImageProcessor.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/xlib/image/ImageProcessor.py b/xlib/image/ImageProcessor.py index 4a095a5..69430b1 100644 --- a/xlib/image/ImageProcessor.py +++ b/xlib/image/ImageProcessor.py @@ -441,7 +441,27 @@ class ImageProcessor: self._img = img return self - + + def to_grayscale(self) -> 'ImageProcessor': + """ + Converts 3 ch bgr to grayscale. + """ + img, xp = self._img, self._xp + _,_,_,C = img.shape + if C != 1: + dtype = self.get_dtype() + + if C == 2: + img = img[...,:1] + elif C >= 3: + img = img[...,:3] + + img = xp.dot(img, xp.array([0.1140, 0.5870, 0.2989], xp.float32)) [...,None] + img = img.astype(dtype) + self._img = img + + return self + def resize(self, size : Tuple, interpolation : 'ImageProcessor.Interpolation' = None, new_ip=False ) -> 'ImageProcessor': """ resize to (W,H)