def image_to_jpeg_wand(self, jpeg: typing.Union[str, typing.IO[bytes]], preview_dims: ImgDims = None) -> BytesIO: """ for jpeg, gif and bmp :param jpeg: :param size: :return: """ self.logger.info("Converting image to jpeg using wand") with WImage(file=jpeg, background=Color("white")) as image: preview_dims = ImgDims(width=preview_dims.width, height=preview_dims.height) resize_dim = compute_resize_dims(dims_in=ImgDims( width=image.size[0], height=image.size[1]), dims_out=preview_dims) image.resize(resize_dim.width, resize_dim.height) # INFO - jumenzel - 2019-03-12 - remove metadata, color-profiles from this image. image.strip() content_as_bytes = image.make_blob("jpeg") output = BytesIO() output.write(content_as_bytes) output.seek(0, 0) return output
def test_compute_resize_dims_right_limits(): dims_in = ImgDims(520, 206) # vertical dims_out = ImgDims(512, 256) # horizontal dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 512 assert dims_resize.height == 203
def image_to_jpeg_pillow( self, png: typing.Union[str, typing.IO[bytes]], preview_dims: ImgDims ) -> BytesIO: self.logger.info('Converting image to jpeg using Pillow') with Image.open(png) as image: resize_dim = compute_resize_dims( dims_in=ImgDims(width=image.size[0], height=image.size[1]), dims_out=preview_dims ) image = image.resize((resize_dim.width, resize_dim.height)) output_image = Image.new( 'RGB', (resize_dim.width, resize_dim.height), (255, 255, 255) ) try: output_image.paste(image, (0, 0), image) except ValueError: self.logger.warning( 'Failed the transparency mask superposition. ' 'Maybe your image does not contain a transparency mask') output_image.paste(image) output = BytesIO() output_image.save(output, 'jpeg') output.seek(0, 0) return output
def _convert_image(self, file_path: str, preview_dims: ImgDims) -> Image: """ refer: https://legacy.imagemagick.org/Usage/thumbnails/ like cmd: convert -layers merge -background white -thumbnail widthxheight \ -auto-orient -quality 85 -interlace plane input.jpeg output.jpeg """ img = Image(filename=file_path) resize_dim = compute_resize_dims(dims_in=ImgDims(width=img.width, height=img.height), dims_out=preview_dims) img.auto_orient() img.iterator_reset() img.background_color = Color("white") img.merge_layers("merge") if self.progressive: img.interlace_scheme = "plane" img.compression_quality = self.quality img.thumbnail(resize_dim.width, resize_dim.height) return img
def test_compute_resize_dims_different_ratio(): dims_in = ImgDims(100, 50) # horizontal dims_out = ImgDims(200, 400) # vertical dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 200 assert dims_resize.height == 100
def test_compute_resize_dims_different_ratio_inverted(): dims_in = ImgDims(198, 600) # vertical dims_out = ImgDims(400, 100) # horizontal dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 33 assert dims_resize.height == 100
def test_compute_resize_dims_same_format(): dims_in = ImgDims(100, 50) dims_out = ImgDims(90, 30) dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 60 assert dims_resize.height == 30
def test_compute_resize_dims_same_ratio(): dims_in = ImgDims(100, 50) dims_out = ImgDims(200, 100) dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 200 assert dims_resize.height == 100
def image_to_jpeg_wand(self, jpeg: typing.Union[str, typing.IO[bytes]], preview_dims: ImgDims = None) -> BytesIO: ''' for jpeg, gif and bmp :param jpeg: :param size: :return: ''' logging.info('Converting image to jpeg using wand') with WImage(file=jpeg, background=Color('white')) as image: preview_dims = ImgDims(width=preview_dims.width, height=preview_dims.height) resize_dim = compute_resize_dims(dims_in=ImgDims( width=image.size[0], height=image.size[1]), dims_out=preview_dims) image.resize(resize_dim.width, resize_dim.height) content_as_bytes = image.make_blob('jpeg') output = BytesIO() output.write(content_as_bytes) output.seek(0, 0) return output
def test_compute_resize_dims_same_format(): dims_in = ImgDims(100, 50) dims_out = ImgDims(90, 30) builder = ImagePreviewBuilderWand() dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 60 assert dims_resize.height == 30
def test_compute_resize_dims_same_ratio(): dims_in = ImgDims(100, 50) dims_out = ImgDims(200, 100) builder = ImagePreviewBuilderWand() dims_resize = compute_resize_dims(dims_in, dims_out) assert dims_resize.width == 200 assert dims_resize.height == 100
def convert_pdf_to_jpeg(pdf: typing.Union[str, typing.IO[bytes]], preview_size: ImgDims) -> BytesIO: pdf = pdf.read() images = convert_from_bytes(pdf) output = BytesIO() for image in images: resize_dims = compute_resize_dims(ImgDims(image.width, image.height), preview_size) resized = image.resize((resize_dims.width, resize_dims.height), resample=True) resized.save(output, format="JPEG") output.seek(0, 0) return output
def convert_pdf_to_jpeg(pdf, preview_size): with WImage(file=pdf) as img: # HACK - D.A. - 2017-08-01 # The following 2 lines avoid black background in case of transparent # objects found on the page. As we save to JPEG, this is not a problem img.background_color = Color('white') img.alpha_channel = 'remove' resize_dims = compute_resize_dims(ImgDims(img.width, img.height), preview_size) img.resize(resize_dims.width, resize_dims.height) content_as_bytes = img.make_blob('jpeg') output = BytesIO() output.write(content_as_bytes) output.seek(0, 0) return output
def image_to_jpeg_pillow(self, png: typing.Union[str, typing.IO[bytes]], preview_dims: ImgDims) -> BytesIO: self.logger.info("Converting image to jpeg using Pillow") with Image.open(png) as image: resize_dim = compute_resize_dims(dims_in=ImgDims( width=image.size[0], height=image.size[1]), dims_out=preview_dims) output = BytesIO() image = image.resize((resize_dim.width, resize_dim.height), resample=self.resample_filter_algorithm) image_converter = PillowImageConvertStrategyFactory( self.logger).get_strategy(image) return image_converter.save( image, output, optimize=self.optimize, progressive=self.progressive, quality=self.quality, )
def test_png_to_jpeg_with_background_white() -> None: image_file_path = os.path.join(CURRENT_DIR, "the_png.png") to_size = ImgDims(width=512, height=256) with Image(filename=image_file_path) as input_img: input_size = ImgDims(width=input_img.width, height=input_img.height) expected_size = compute_resize_dims(input_size, to_size) manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True) assert manager.has_jpeg_preview(file_path=image_file_path) is True path_to_file = manager.get_jpeg_preview( file_path=image_file_path, width=to_size.width, height=to_size.height, force=True ) assert os.path.exists(path_to_file) is True assert os.path.getsize(path_to_file) > 0 assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__JPEG, path_to_file) with Image(filename=path_to_file) as output_img: assert output_img.width == expected_size.width assert output_img.height == expected_size.height assert nearest_colour_white(output_img[5][5])