def process(self, descriptor: StreamDescriptor, context: dict): # Ensuring the wand package is installed. ensure_wand() # noinspection PyPackageRequirements from wand.image import Image as WandImage # Copy the original info # generating thumbnail and storing in buffer # noinspection PyTypeChecker img = WandImage(file=descriptor) if self.crop is None and ( self.format is None or img.format == self.format) and ( (self.width is None or img.width == self.width) and (self.height is None or img.height == self.height)): img.close() descriptor.prepare_to_read(backend='memory') return if 'length' in context: del context['length'] # opening the original file output_buffer = io.BytesIO() with img: # Changing format if required. if self.format and img.format != self.format: img.format = self.format # Changing dimension if required. if self.width or self.height: width, height, _ = validate_width_height_ratio( self.width, self.height, None) img.resize( width(img.size) if callable(width) else width, height(img.size) if callable(height) else height) # Cropping if self.crop: img.crop( **{ key: int( int(value[:-1]) / 100 * (img.width if key in ('width', 'left', 'right') else img.height) ) if key in ('left', 'top', 'right', 'bottom', 'width', 'height') and '%' in value else value for key, value in self.crop.items() }) img.save(file=output_buffer) context.update(content_type=img.mimetype, width=img.width, height=img.height, extension=guess_extension(img.mimetype)) output_buffer.seek(0) descriptor.replace(output_buffer, position=0, **context)
def process(self, descriptor: StreamDescriptor, context: dict): Image = get_image_factory() # This processor requires seekable stream. descriptor.prepare_to_read(backend='memory') with Image(file=descriptor) as img: context.update(width=img.width, height=img.height, content_type=img.mimetype) # prepare for next processor, calling this method is not bad and just uses the memory # temporary. descriptor.prepare_to_read(backend='memory')
def process(self, descriptor: StreamDescriptor, context: dict): ensure_wand() # noinspection PyPackageRequirements from wand.image import Image as WandImage # This processor requires seekable stream. descriptor.prepare_to_read(backend='memory') with WandImage(file=descriptor) as img: context.update(width=img.width, height=img.height, content_type=img.mimetype) # prepare for next processor, calling this method is not bad. descriptor.prepare_to_read(backend='memory')
def process(self, descriptor: StreamDescriptor, context: dict): ensure_wand() # noinspection PyPackageRequirements from wand.image import Image as WandImage from wand.exceptions import WandException # This processor requires seekable stream. descriptor.prepare_to_read(backend='memory') try: # noinspection PyUnresolvedReferences with WandImage(file=descriptor) as img: context.update(width=img.width, height=img.height, content_type=img.mimetype) except WandException: raise AnalyzeError(str(WandException)) # prepare for next processor, calling this method is not bad. descriptor.prepare_to_read(backend='memory')