Skip to content

Commit b938dc0

Browse files
github-actions[bot]giohappymarthamarealafabiani
authored
[Fixes #10091] improve thumbnails quality (#10092) (#10110)
* improve thumbnails quality * force convertion to jpeg * - update tests * - fix tests Co-authored-by: marthamareal <marthamareal@gmail.com> Co-authored-by: Alessio Fabiani <alessio.fabiani@geosolutionsgroup.com> Co-authored-by: Giovanni Allegri <giohappy@gmail.com> Co-authored-by: marthamareal <marthamareal@gmail.com> Co-authored-by: Alessio Fabiani <alessio.fabiani@geosolutionsgroup.com>
1 parent 82c2bc6 commit b938dc0

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

geonode/base/api/tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ def test_set_resource_thumbnail(self):
20142014
_mck.return_value = False
20152015
response = self.client.put(url, data=data, format="json")
20162016
self.assertEqual(response.status_code, 200)
2017-
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.png", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
2017+
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.jpg", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
20182018
# File upload
20192019
with patch('PIL.Image.open') as _mck:
20202020
_mck.return_value = test_image
@@ -2024,7 +2024,7 @@ def test_set_resource_thumbnail(self):
20242024
self.assertEqual(Dataset.objects.get(pk=resource.pk).thumbnail_url, None)
20252025
f = SimpleUploadedFile('test_image.png', BytesIO(test_image.tobytes()).read(), 'image/png')
20262026
response = self.client.put(url, data={"file": f})
2027-
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.png", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
2027+
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.jpg", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
20282028
self.assertEqual(response.status_code, 200)
20292029

20302030
def test_set_thumbnail_from_bbox_from_Anonymous_user_raise_permission_error(self):

geonode/base/models.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,8 @@ def has_thumbnail(self):
17231723
# that indexing (or other listeners) are notified
17241724
def save_thumbnail(self, filename, image):
17251725
upload_path = get_unique_upload_path(filename)
1726+
# force convertion to JPEG output file
1727+
upload_path = f'{os.path.splitext(upload_path)[0]}.jpg'
17261728
try:
17271729
# Check that the image is valid
17281730
if is_monochromatic_image(None, image):
@@ -1743,14 +1745,11 @@ def save_thumbnail(self, filename, image):
17431745
# Optimize the Thumbnail size and resolution
17441746
_default_thumb_size = settings.THUMBNAIL_SIZE
17451747
im = Image.open(storage_manager.open(actual_name))
1746-
im.thumbnail(
1747-
(_default_thumb_size['width'], _default_thumb_size['height']),
1748-
resample=Image.ANTIALIAS)
1749-
cover = ImageOps.fit(im, (_default_thumb_size['width'], _default_thumb_size['height']))
1748+
cover = ImageOps.fit(im, (_default_thumb_size['width'], _default_thumb_size['height'])).convert("RGB")
17501749

17511750
# Saving the thumb into a temporary directory on file system
17521751
tmp_location = os.path.abspath(f"{settings.MEDIA_ROOT}/{upload_path}")
1753-
cover.save(tmp_location, format='PNG')
1752+
cover.save(tmp_location, quality="high")
17541753

17551754
with open(tmp_location, 'rb+') as img:
17561755
# Saving the img via storage manager

geonode/documents/tasks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def create_document_thumbnail(self, object_id):
7979
logger.warning(f"Thumbnail for document #{object_id} empty.")
8080
ResourceBase.objects.filter(id=document.id).update(thumbnail_url=None)
8181
else:
82-
filename = f'document-{document.uuid}-thumb.png'
82+
filename = f'document-{document.uuid}-thumb.jpg'
8383
document.save_thumbnail(filename, thumbnail_content)
8484
logger.debug(f"Thumbnail for document #{object_id} created.")
8585

geonode/documents/tests.py

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ def test_image_documents_thumbnail(self):
301301
)
302302
file = Image.open(thumb_file)
303303
self.assertEqual(file.size, (400, 200))
304+
# check thumbnail qualty and extention
305+
self.assertEqual(file.format, 'JPEG')
304306
finally:
305307
Document.objects.filter(title='img File Doc').delete()
306308

geonode/thumbs/tests/test_integration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def test_tile_background_generic_fetch(self):
238238
)
239239
def test_tile_background_generic_fetch_zoom(self):
240240

241-
width = 240
241+
width = 500
242242
height = 200
243243

244244
bbox_3857 = [-8250483.072013094, -8221819.186406153, 4961221.562116772, 4985108.133455889, "EPSG:3857"]

0 commit comments

Comments
 (0)