algoo / preview-generator

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

get_jpeg_preview fails with HEIC image #182

Open iqbalaydrus opened 4 years ago

iqbalaydrus commented 4 years ago

I'm using a python docker image based on debian, also tried official python3.8 image without success with HEIC image. Below is the relevant dockerfile:

FROM debian
RUN apt-get update \
    && apt-get install -y curl python3 python3-pip curl make tar autotools-dev automake libtool pkg-config g++
RUN LIBDE265_VERSION="1.0.5" \
    && curl -L https://github.com/strukturag/libde265/releases/download/v${LIBDE265_VERSION}/libde265-${LIBDE265_VERSION}.tar.gz | tar zx \
    && cd libde265-${LIBDE265_VERSION} \
    && ./autogen.sh \
    && ./configure \
    && make -j4 \
    && make install
RUN LIBHEIF_VERSION="1.6.2" \
    && curl -L https://github.com/strukturag/libheif/releases/download/v${LIBHEIF_VERSION}/libheif-${LIBHEIF_VERSION}.tar.gz | tar zx \
    && cd libheif-${LIBHEIF_VERSION} \
    && export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig \
    && export LDFLAGS=-L/usr/local/lib \
    && export CPPFLAGS=-I/usr/local/include/libde265 \
    && ./autogen.sh \
    && ./configure \
    && make -j4 \
    && make install
RUN IMAGEMAGICK_VERSION="7.0.10-0" \
    && curl -L https://imagemagick.org/download/ImageMagick-${IMAGEMAGICK_VERSION}.tar.gz | tar zx \
    && cd ImageMagick-${IMAGEMAGICK_VERSION} \
    && export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig \
    && export LDFLAGS=-L/usr/local/lib \
    && export CPPFLAGS=-I/usr/local/include/libheif \
    && ./configure --enable-shared --enable-static=yes --enable-symbol-prefix --with-heic --with-raw --with-gslib \
    && make -j4 \
    && make install \
    && ldconfig
RUN apt-get install -y zlib1g-dev libjpeg-dev inkscape xvfb poppler-utils libfile-mimeinfo-perl \
    qpdf libimage-exiftool-perl ufraw-batch ffmpeg python3-pythonmagick libreoffice \
    && rm -rf /var/lib/apt/lists/*

Running get_jpeg_preview method gave me this error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/preview_generator/preview/builder_factory.py", line 53, in get_preview_builder
    return self._builder_classes[mimetype]()  # nopep8 get class and instantiate it
KeyError: 'image/heic'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.7/dist-packages/flask_restx/api.py", line 339, in wrapper
    resp = resource(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/flask_restx/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/flask_restx/marshalling.py", line 247, in wrapper
    resp = f(*args, **kwargs)
  File "/docman_api/middlewares.py", line 23, in wrapper
    return func(*args, **kwargs)
  File "/docman_api/endpoints/document/v1_attachment/post.py", line 42, in post
    thumbnail_path = manager.get_jpeg_preview(fileinput_path, height=1920)
  File "/usr/local/lib/python3.7/dist-packages/preview_generator/manager.py", line 156, in get_jpeg_preview
    builder = self._factory.get_preview_builder(mimetype)
  File "/usr/local/lib/python3.7/dist-packages/preview_generator/preview/builder_factory.py", line 55, in get_preview_builder
    raise UnsupportedMimeType("Unsupported mimetype: {}".format(mimetype))
preview_generator.exception.UnsupportedMimeType: Unsupported mimetype: image/heic

Also this is my --check-dependencies output:

root@1f4dc6d256fb:/# preview --check-dependencies
✓ ImagePreviewBuilderWand wand 0.5.9 from /usr/local/lib/python3.7/dist-packages/wand
✓ ImagePreviewBuilderPillow PIL 7.0.0 from /usr/local/lib/python3.7/dist-packages/PIL
✓ ImagePreviewBuilderInkscape Inkscape 0.92.4 (5da689c313, 2019-01-14) from /usr/bin/inkscape
✓ ImagePreviewBuilderIMConvert Version: ImageMagick 7.0.10-0 Q16 x86_64 2020-03-11 https://imagemagick.org from /usr/local/bin/convert
✗ DocumentPreviewBuilderScribus is missing a dependency:  this builder requires scribus to be available
✓ OfficePreviewBuilderLibreoffice LibreOffice 6.1.5.2 10(Build:2) from /usr/bin/libreoffice
✓ PlainTextPreviewBuilder LibreOffice 6.1.5.2 10(Build:2) from /usr/bin/libreoffice
✓ ImagePreviewBuilderVtk VTK version :8.1.2

According to this supported_mimetypes it should be supported, but it gave me that error.

iqbalaydrus commented 4 years ago

using a little hack like this works though:

manager = PreviewManager(temp_path)
manager._factory._builder_classes['image/heic'] = manager._factory.get_builder_class('image/jpeg')

get_jpeg_preview method works well with the hack above

lebouquetin commented 4 years ago

Hi @iqbalaydrus,

Thanks for your feedback and the hack which will help fixing the issue. @inkhey do you know why the image/heic mimetype is not supported without this hack?

@iqbalaydrus do you have an example file to give us? This would allow to test the fix we'll implement and would allow us to add a non-regression test (which make sure the image format will still be supported in the future)

Thanks a lot.

inkhey commented 4 years ago

@iqbalaydrus Hello ! Which version of preview_generator are you using ? Support for HEIC is new and there was no preview_generator pypi release since.

lebouquetin commented 4 years ago

@inkhey the problem is pypi shows the develop version of supported mimetypes while they are not all in the last release.

In the past, the supported mimetype list was generated for pypi publication...

I suggest two things:

  1. re-integrate list of supported mimetype for last release in pypi documentation
  2. also add in the documentation a link to "list of supported mimetype of development branch".

This will allow users to understand why a mimetype is (or is not) supported

iqbalaydrus commented 4 years ago

@iqbalaydrus do you have an example file to give us? This would allow to test the fix we'll implement and would allow us to add a non-regression test (which make sure the image format will still be supported in the future)

Sure, this is a random photo I took, zipped it since github don't support HEIC upload (ouch) IMG_0691.HEIC.zip

@iqbalaydrus Hello ! Which version of preview_generator are you using ? Support for HEIC is new and there was no preview_generator pypi release since.

I installed the latest version (0.13) via pip. Ah ok, that explains why I don't have heic support. I guess I'll use the hack until the next release.