Esempio n. 1
0
def image_from_pixels ( pixels ):
    """ Returns a new ImageResource object containing an image derived from
        the array of RGBA pixels specified by *pixels*.
    """
    from facets.api import toolkit
    from facets.ui.pyface.image_resource import ImageResource

    dy, dx, _ = pixels.shape
    bits      = pixels.tostring()
    image     = ImageResource( bitmap = toolkit().create_bitmap( bits, dx, dy ) )

    # Save the pixel data to avoid it being garbage collected:
    image._buffer = bits

    return image
Esempio n. 2
0
    def scale ( self, size, filter = 'Lanczos3' ):
        """ Returns a new ImageResource object derived from this one which has
            been scaled to the size specified by *size* using the image filter
            specified by *filter*.

            *Size* may either be a tuple of the form (width, height) specifying
            the target image width and height in pixels (e.g. (100,80)), or a
            single float value representing the image scaling factor (e.g. 0.5).

            If the tuple form is used, one of the two values may be zero (e.g.
            (100,0)). In this case, the zero value will be replaced by the
            scaled width or height needed to preserve the current aspect ratio
            of the image based on the other, non-zero value specified. This
            could be useful when trying to create thumbnail images with a
            common width (or height) for example.

            *filter* specifies the optional name of the image filter to use
            while scaling the image. The possible values are:
              - Bell
              - Box
              - CatmullRom
              - Cosine
              - CubicConvolution
              - CubicSpline
              - Hermite
              - Lanczos3 (default)
              - Lanczos8
              - Mitchell
              - Quadratic
              - QuadraticBSpline
              - Triangle
        """
        global filter_map
        from facets.api import toolkit
        from facets.ui.pyface.image_resource import ImageResource

        if isinstance( size, tuple ):
            width, height = size
            if width == 0:
                width = round( float( self.width * height ) / self.height )
            elif height == 0:
                height = round( float( self.height * width ) / self.width )
        else:
            width  = round( self.width  * size )
            height = round( self.height * size )

        width  = clamped( int( width ),  1, 8192 )
        height = clamped( int( height ), 1, 8192 )
        if (width == self.width) and (height == self.height):
            return self

        pixels = image_transform(
            self.pixels.tostring(),
            self.width, self.height, width, height, filter_map[ filter ]
        )

        image = ImageResource(
            bitmap = toolkit().create_bitmap( pixels, width, height )
        )

        # Save the pixel data to avoid it being garbage collected:
        image._buffer = pixels

        return image