def set_image(self, image, change_dims=True, change_palette=False): if not PILImage: raise ImportError( 'Pillow must be installed for image manipulation support (see http://pillow.readthedocs.io/en/latest/installation.html)' ) if not isinstance(image, PILImage.Image): raise TypeError('Image must be a PILImage object') if image.mode != 'P': raise AttributeError('Image must be indexed (mode P)') if change_dims: if self.width != image.width: logger.warning("Changing width from {} to {}".format( self.width, image.width)) self.width = image.width if self.height != image.height: logger.warning("Changing height from {} to {}".format( self.height, image.height)) self.height = image.height else: if not (self.width == image.width) and (self.height == image.height): raise AttributeError( 'Image is a different size, please enable change_dims if you want to resize the image' ) old_pal = to_palette_bytes(self.palette) new_pal = image.palette.palette if old_pal != new_pal: if change_palette: self.palette = from_palette_bytes(new_pal) else: logger.warning( 'Image was provided with a different palette, please enable change_palette if you want to set the palette' ) self.source = image.tobytes()
def set_image( self, image, change_dims=True, change_palette=False ): if not PILImage: raise ImportError( 'Pillow must be installed for image manipulation support (see http://pillow.readthedocs.io/en/latest/installation.html)' ) if not isinstance( image, PILImage.Image ): raise TypeError( 'Image must be a PILImage object' ) if image.mode != 'P': raise AttributeError( 'Image must be indexed (mode P)' ) if change_dims: if self.width != image.width: logger.warning( "Changing width from {} to {}".format( self.width, image.width ) ) self.width = image.width if self.height != image.height: logger.warning( "Changing height from {} to {}".format( self.height, image.height ) ) self.height = image.height else: if not (self.width == image.width) and (self.height == image.height): raise AttributeError( 'Image is a different size, please enable change_dims if you want to resize the image' ) old_pal = to_palette_bytes( self.palette ) new_pal = image.palette.palette if old_pal != new_pal: if change_palette: self.palette = from_palette_bytes( new_pal ) else: logger.warning( 'Image was provided with a different palette, please enable change_palette if you want to set the palette' ) self.source = image.tobytes()
def get_image( self ): if not PILImage: raise ImportError( 'Pillow must be installed for image manipulation support (see http://pillow.readthedocs.io/en/latest/installation.html)' ) im = PILImage.new( 'P', (self.width, self.height) ) im.putdata( self.source[:self.width*self.height] ) im.putpalette( to_palette_bytes( self.palette ) ) return im