def _coreImageFromNumpyArray( ndarray: np.ndarray, inputColorFormat: ColorFormat, colorFormat: Optional[ColorFormat] = None) -> CoreImage: """ Load VLImage from numpy array into `self`. Args: ndarray: numpy pixel array inputColorFormat: numpy pixel array format colorFormat: pixel format to cast into Returns: core image instance """ baseCoreImage = CoreImage() baseCoreImage.setData(ndarray, inputColorFormat.coreFormat) if colorFormat is None or baseCoreImage.getFormat( ) == colorFormat.coreFormat: return baseCoreImage error, convertedCoreImage = baseCoreImage.convert( colorFormat.coreFormat) if error.isError: raise LunaSDKException(LunaVLError.fromSDKError(error)) return convertedCoreImage
def __init__( self, body: Union[bytes, bytearray, PilImage, CoreImage], colorFormat: Optional[ColorFormat] = None, filename: str = "", ): """ Init. Args: body: body of image - bytes numpy array or core image colorFormat: img format to cast into filename: user mark a source of image Raises: TypeError: if body has incorrect type LunaSDKException: if failed to load image to sdk Image """ if isinstance(body, bytearray): body = bytes(body) if isinstance(body, CoreImage): if colorFormat is None or colorFormat.coreFormat == body.getFormat(): self.coreImage = body else: error, self.coreImage = body.convert(colorFormat.coreFormat) assertError(error) elif isinstance(body, bytes): self.coreImage = CoreImage() imgFormat = (colorFormat or ColorFormat.R8G8B8).coreFormat error = self.coreImage.loadFromMemory(body, len(body), imgFormat) assertError(error) elif isinstance(body, np.ndarray): mode = getNPImageType(body) self.coreImage = self._coreImageFromNumpyArray( ndarray=body, inputColorFormat=ColorFormat.load(mode), colorFormat=colorFormat or ColorFormat.R8G8B8 ) elif isinstance(body, PilImage): # prevent palette-mode colorful images conversion to grayscale image by converting it # with considering palette info # modes description: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes blackNWhiteModes = ("1", "L", "LA", "La") if body.mode == "P" and body.palette is not None and body.palette.mode not in blackNWhiteModes: body = body.convert() array = pilToNumpy(body) inputColorFormat = ColorFormat.load(body.mode) self.coreImage = self._coreImageFromNumpyArray( ndarray=array, inputColorFormat=inputColorFormat, colorFormat=colorFormat or ColorFormat.R8G8B8 ) else: raise TypeError(f"Bad image type: {type(body)}") self.source = body self.filename = filename
def __init__( self, body: Union[bytes, bytearray, PilImage, CoreImage], colorFormat: Optional[ColorFormat] = None, filename: str = "", ): """ Init. Args: body: body of image - bytes numpy array or core image colorFormat: img format to cast into filename: user mark a source of image Raises: TypeError: if body has incorrect type LunaSDKException: if failed to load image to sdk Image """ if isinstance(body, bytearray): body = bytes(body) if isinstance(body, CoreImage): if colorFormat is None or colorFormat.coreFormat == body.getFormat(): self.coreImage = body else: error, self.coreImage = body.convert(colorFormat.coreFormat) assertError(error) elif isinstance(body, bytes): self.coreImage = CoreImage() imgFormat = (colorFormat or ColorFormat.R8G8B8).coreFormat error = self.coreImage.loadFromMemory(body, len(body), imgFormat) assertError(error) elif isinstance(body, np.ndarray): mode = getNPImageType(body) self.coreImage = self._coreImageFromNumpyArray( ndarray=body, inputColorFormat=ColorFormat.load(mode), colorFormat=colorFormat or ColorFormat.R8G8B8 ) elif isinstance(body, PilImage): array = pilToNumpy(body) inputColorFormat = ColorFormat.load(body.mode) self.coreImage = self._coreImageFromNumpyArray( ndarray=array, inputColorFormat=inputColorFormat, colorFormat=colorFormat or ColorFormat.R8G8B8 ) else: raise TypeError(f"Bad image type: {type(body)}") self.source = body self.filename = filename
def __init__( self, body: Union[bytes, bytearray, ndarray, CoreImage], imgFormat: Optional[ColorFormat] = None, filename: str = "", ): """ Init. Args: body: body of image - bytes numpy array or core image imgFormat: img format filename: user mark a source of image Raises: TypeError: if body has incorrect type LunaSDKException: if failed to load image to sdk Image """ if imgFormat is None: imgFormat = ColorFormat.R8G8B8 self.coreImage = CoreImage() if isinstance(body, CoreImage): self.coreImage = body elif isinstance(body, bytes): error = self.coreImage.loadFromMemory(body, len(body), imgFormat.coreFormat) if error.isError: raise LunaSDKException(LunaVLError.fromSDKError(error)) elif isinstance(body, bytearray): error = self.coreImage.loadFromMemory(bytes(body), len(body), imgFormat.coreFormat) if error.isError: raise LunaSDKException(LunaVLError.fromSDKError(error)) elif isinstance(body, ndarray): #: todo, format ????? self.coreImage.setData(body, imgFormat.coreFormat) else: raise TypeError("Bad image type") self.source = body self.filename = filename