def trim ( self, level = 0.0 ): """ Returns a new copy of the image with all external edges having alpha levels <= *level* trimmed off. """ if isinstance( level, float ): level = int( 255.0 * level ) level = clamped( level, 0, 255 ) a = self.a for x1 in xrange( 0, self.width ): if not alltrue( a[ :, x1 ] <= level ): break for x2 in xrange( self.width - 1, x1 - 1, -1 ): if not alltrue( a[ :, x2 ] <= level ): break x2 += 1 for y1 in xrange( 0, self.height ): if not alltrue( a[ y1, x1: x2 ] <= level ): break for y2 in xrange( self.height - 1, y1 - 1, -1 ): if not alltrue( a[ y2, x1: x2 ] <= level ): break return self.crop( x1, y1, x2 - x1, y2 - y1 + 1 )
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