def PIL2Mamba(pilim, imOut): """ The PIL/PILLOW image 'pilim' is used to load the Mamba image 'imOut'. """ depth = imOut.getDepth() (width, height) = imOut.getSize() next_mbIm = utils.loadFromPILFormat(pilim, size=(width,height)) err = core.MB_Convert(next_mbIm, imOut.mbIm) mamba.raiseExceptionOnError(err) imOut.update()
def convert(self, depth): """ Converts the image depth to the given 'depth'. The conversion algorithm is identical to the conversion used in the convert function (see this function for details). """ next_mbIm = utils.create(self.mbIm.width,self.mbIm.height,depth) err = core.MB_Convert(self.mbIm, next_mbIm) raiseExceptionOnError(err) del self.mbIm self.mbIm = next_mbIm if self.displayId != '': self.gd.updateWindow(self.displayId)
def load(self, path, rgbfilter=None): """ Loads the image in 'path' into the Mamba image. The optional 'rgbfilter' argument can be used to specify how to convert a color image into a greyscale image. It is a sequence of 3 float values indicating the amount of red, green and blue to take from the image to obtain the grey value. By default, the color conversion uses the ITU-R 601-2 luma transform (see PIL/PILLOW documentation for details). """ next_mbIm = utils.load(path, size=(self.mbIm.width,self.mbIm.height), rgb2l=rgbfilter) err = core.MB_Convert(next_mbIm, self.mbIm) raiseExceptionOnError(err) self.setName(os.path.split(path)[1])
def convert(imIn, imOut): """ Converts the contents of 'imIn' to the depth of 'imOut' and puts the result in 'imOut'. For greyscale/32-bit to binary conversion, value 255/0xffffffff in 'imIn' is converted to 1 in 'imOut'. All other values are transformed to 0. The reverse convention applies to binary to greyscale/32-bit conversion. 32-bit images are downscaled into greyscale images. Conversion from 8-bit to 32-bit is equivalent to copyBytePlane for plane 0. When both images have the same depth this function is a simple copy. """ err = core.MB_Convert(imIn.mbIm, imOut.mbIm) mamba.raiseExceptionOnError(err) imOut.update()
def convertToPILFormat(im_in): """ Converts a mamba C core image 'im_in' structure into a PIL image. 'palette' is used to colorize the image if wanted. 32-bit images are converted into an image that contains the four byte planes stitched together. The palette is a sequence as defined by the putpalette method of PIL images. If not given the image stays in greyscale. """ # Extracting image size information. w = im_in.width h = im_in.height # Extracting the data from the image. if im_in.depth == 32: # 32-bit images err, s = core.MB_Extract(im_in) raiseExceptionOnError(err) # Creating the PIL image pilim = Image.frombytes("I", (w, h), s) elif im_in.depth == 1: # Binary images im = create(im_in.width, im_in.height, 8) err = core.MB_Convert(im_in, im) raiseExceptionOnError(err) err, s = core.MB_Extract(im) raiseExceptionOnError(err) # Creating the PIL image pilim = Image.frombytes("L", (w, h), s) else: # Greyscale images err, s = core.MB_Extract(im_in) raiseExceptionOnError(err) # Creating the PIL image pilim = Image.frombytes("L", (w, h), s) return pilim