algoo / preview-generator

generates previews of files with cache management
https://pypi.org/project/preview-generator/
MIT License
228 stars 51 forks source link

tests/input/svg/test_svg.py test_to_jpeg fails with wand backend. #113

Closed JulienPalard closed 2 years ago

JulienPalard commented 4 years ago

I'm trying to check if every builders can build what they say they can.

For this I'm applying:

diff --git a/preview_generator/preview/builder_factory.py b/preview_generator/preview/builder_factory.py
index e08d015..110b465 100644
--- a/preview_generator/preview/builder_factory.py
+++ b/preview_generator/preview/builder_factory.py
@@ -7,6 +7,7 @@ import os
 from os.path import basename
 from os.path import dirname
 from os.path import isfile
+from random import shuffle
 from subprocess import PIPE
 from subprocess import Popen
 from threading import RLock
@@ -90,7 +91,9 @@ class PreviewBuilderFactory(object):

             from preview_generator.preview.generic_preview import PreviewBuilder  # nopep8

-            for cls in get_subclasses_recursively(PreviewBuilder):
+            subclasses = list(get_subclasses_recursively(PreviewBuilder))
+            shuffle(subclasses)
+            for cls in subclasses:
                 self.register_builder(cls)

             self.builders_loaded = True

Sometimes I'm getting:

        with Image.open(path_to_file) as jpeg:
            assert jpeg.height == 256
>           assert jpeg.width in range(358, 360)
E           assert 181 in range(358, 360)
E            +  where 181 = <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=181x256 at 0x7F758917EE10>.width
E            +  and   range(358, 360) = range(358, 360)

This means that the wand builder can handle crops, but won't actually resize.

This is because wand stores a sequence of pages when loading a PDF, but we only resized the first one, then storing them all on top of each other in the same preview.

Modifying image__wand's build_jpeg_preview like this may work:

def build_jpeg_preview(
    self,
    file_path: str,
    preview_name: str,
    cache_path: str,
    page_id: int,
    extension: str = ".jpeg",
    size: ImgDims = None,
    mimetype: str = "",
) -> None:
    self.logger.info("Converting image to jpeg using wand")

    if not size:
        size = self.default_size

    output_path = "{path}{extension}".format(
        path=cache_path + preview_name, extension=extension
    )

    with open(file_path, "rb") as ifile, open(output_path, "wb") as ofile:
        if mimetype == "application/pdf":
            ofile.write(convert_pdf_to_jpeg(ifile, size).read())
            return
        with WImage(file=ifile, background=Color("white")) as image:
            resize_dim = compute_resize_dims(
                dims_in=ImgDims(width=image.size[0], height=image.size[1]), dims_out=size
            )
            image.resize(resize_dim.width, resize_dim.height)
            with image.convert("jpeg") as converted:
                # INFO - jumenzel - 2019-03-12 - remove metadata, color-profiles from this image.
                converted.strip()
                converted.save(file=ofile)

but has to be tested.

inkhey commented 2 years ago

this has been fixed with the improved wand backend #253