コード例 #1
0
    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)
コード例 #2
0
    def __init__(self, *args, **kwargs):
        """
        Constructor for a Mamba image object.
        
        This constructor allows a wide range of possibilities for defining an image:
            * imageMb(): without arguments will create an empty greyscale image.
            * imageMb(im): will create an image using the same size and depth
              as 'im'.
            * imageMb(depth): will create an image with the desired 'depth' (1, 8 or 32).
            * imageMb(path): will load the image located in 'path'.
            * imageMb(im, depth): will create an image using the same size as 'im' 
            and the specified 'depth'.
            * imageMb(path, depth): will load the image located in 'path' and 
            convert it to the specified 'depth'.
            * imageMb(width, height): will create an image with size 'width'x'height'.
            * imageMb(width, height, depth): will create an image with size 
            'width'x'height' and the specified 'depth'.
            
        When not specified, the width and height of the image will be set to 
        256x256. The default depth is 8 (greyscale).
        
        When loading an image from a file, please note that Mamba accepts all 
        kinds of images (actually all the PIL or PILLOW supported formats). You can specify
        the RGB filter that will be used to convert a color image into a greyscale 
        image by adding the rgbfilter=<your_filter> to the argument of the
        constructor.
        """
        global _image_index
        
        # List of all the parameters that must be retrieved from the arguments
        rgbfilter = None
        # First we look into the dictionnary to see if they were specified
        # specifically by the user
        if "rgbfilter" in kwargs:
            rgbfilter = kwargs["rgbfilter"]
        
        # Properties
        self.displayId = ''
        self.gd = None
            
        # We analyze the arguments given to the constructor
        if len(args)==0:
            # First case : no argument was given
            # -> imageMb()
            self.mbIm = utils.create(256, 256, 8)
            self.name = "Image "+str(_image_index)
            _image_index = _image_index + 1
        elif len(args)==1:
            # Second case : the user gives only one argument
            if isinstance(args[0], imageMb):
                # -> imageMb(im)
                self.mbIm = utils.create(args[0].mbIm.width, args[0].mbIm.height, args[0].mbIm.depth)
                self.name = "Image "+str(_image_index)
                _image_index = _image_index + 1
            elif isinstance(args[0], str):
                # -> imageMb(path)
                self.mbIm = utils.load(args[0], rgb2l=rgbfilter)
                self.name = os.path.split(args[0])[1]
            else:
                # -> imageMb(depth)
                self.mbIm = utils.create(256, 256, args[0])
                self.name = "Image "+str(_image_index)
                _image_index = _image_index + 1
        elif len(args)==2:
            # Third case : two arguments
            if isinstance(args[0], imageMb):
                # -> imageMb(im, depth)
                self.mbIm = utils.create(args[0].mbIm.width, args[0].mbIm.height, args[1])
                self.name = "Image "+str(_image_index)
                _image_index = _image_index + 1
            elif isinstance(args[0], str):
                # -> imageMb(path, depth)
                self.mbIm = utils.load(args[0], rgb2l=rgbfilter)
                if self.mbIm.depth != args[1]:
                    self.convert(args[1])
                self.name = os.path.split(args[0])[1]
            else:
                # -> imageMb(width, height)
                self.mbIm = utils.create(args[0], args[1], 8)
                self.name = "Image "+str(_image_index)
                _image_index = _image_index + 1
        else:
            # Last case: at least 3 arguments are given
            # -> imageMb(width, height, depth)
            self.mbIm = utils.create(args[0], args[1], args[2])
            self.name = "Image "+str(_image_index)
            _image_index = _image_index + 1

        if getShowImages():
            self.show()