|
1 | 1 | # coding: utf-8 |
2 | 2 | from io import BytesIO |
| 3 | +from mimetypes import guess_type |
3 | 4 | from tempfile import NamedTemporaryFile |
4 | 5 |
|
5 | 6 | import requests |
6 | 7 | from django.conf import settings |
7 | 8 | from django.core.files.base import ContentFile |
8 | 9 | from django.core.files.storage import FileSystemStorage |
9 | 10 | from PIL import Image |
| 11 | +from pillow_heif import register_heif_opener |
10 | 12 |
|
11 | 13 | from kobo.apps.openrosa.libs.utils.viewer_tools import get_optimized_image_path |
| 14 | +from kpi.constants import UNSUPPORTED_INLINE_MIMETYPES |
12 | 15 | from kpi.deployment_backends.kc_access.storage import ( |
13 | 16 | default_kobocat_storage as default_storage, |
14 | 17 | ) |
@@ -39,9 +42,16 @@ def get_dimensions(size_, longest_side): |
39 | 42 |
|
40 | 43 |
|
41 | 44 | def _save_thumbnails(image, original_path, size, suffix): |
| 45 | + img_format = image.format |
| 46 | + |
| 47 | + # Change format to JPEG for unsupported inline mimetypes |
| 48 | + mimetype, _ = guess_type(original_path) |
| 49 | + if mimetype in UNSUPPORTED_INLINE_MIMETYPES: |
| 50 | + img_format = 'JPEG' |
| 51 | + |
42 | 52 | # Thumbnail format will be set by original file extension. |
43 | 53 | # Use same format to keep transparency of GIF/PNG |
44 | | - nm = NamedTemporaryFile(suffix='.%s' % image.format) |
| 54 | + nm = NamedTemporaryFile(suffix='.%s' % img_format) |
45 | 55 | try: |
46 | 56 | # Ensure conversion to float in operations |
47 | 57 | image.thumbnail(get_dimensions(image.size, float(size)), Image.LANCZOS) |
@@ -71,6 +81,7 @@ def _save_thumbnails(image, original_path, size, suffix): |
71 | 81 |
|
72 | 82 | def resize(filename): |
73 | 83 | image = None |
| 84 | + register_heif_opener() |
74 | 85 | if isinstance(default_storage, FileSystemStorage): |
75 | 86 | path = default_storage.path(filename) |
76 | 87 | image = Image.open(path) |
|
0 commit comments