Skip to content

Commit

Permalink
[Fixes #10091] improve thumbnails quality (#10092)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Oct 6, 2022
1 parent 91a4a9d commit 254747e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions geonode/base/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ def test_set_resource_thumbnail(self):
_mck.return_value = False
response = self.client.put(url, data=data, format="json")
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.png", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.jpg", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
# File upload
with patch('PIL.Image.open') as _mck:
_mck.return_value = test_image
Expand All @@ -2024,7 +2024,7 @@ def test_set_resource_thumbnail(self):
self.assertEqual(Dataset.objects.get(pk=resource.pk).thumbnail_url, None)
f = SimpleUploadedFile('test_image.png', BytesIO(test_image.tobytes()).read(), 'image/png')
response = self.client.put(url, data={"file": f})
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.png", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
self.assertIsNotNone(re.search(f"dataset-{re_uuid}-thumb-{re_uuid}.jpg", Dataset.objects.get(pk=resource.pk).thumbnail_url, re.I))
self.assertEqual(response.status_code, 200)

def test_set_thumbnail_from_bbox_from_Anonymous_user_raise_permission_error(self):
Expand Down
9 changes: 4 additions & 5 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,8 @@ def has_thumbnail(self):
# that indexing (or other listeners) are notified
def save_thumbnail(self, filename, image):
upload_path = get_unique_upload_path(filename)
# force convertion to JPEG output file
upload_path = f'{os.path.splitext(upload_path)[0]}.jpg'
try:
# Check that the image is valid
if is_monochromatic_image(None, image):
Expand All @@ -1743,14 +1745,11 @@ def save_thumbnail(self, filename, image):
# Optimize the Thumbnail size and resolution
_default_thumb_size = settings.THUMBNAIL_SIZE
im = Image.open(storage_manager.open(actual_name))
im.thumbnail(
(_default_thumb_size['width'], _default_thumb_size['height']),
resample=Image.ANTIALIAS)
cover = ImageOps.fit(im, (_default_thumb_size['width'], _default_thumb_size['height']))
cover = ImageOps.fit(im, (_default_thumb_size['width'], _default_thumb_size['height'])).convert("RGB")

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

with open(tmp_location, 'rb+') as img:
# Saving the img via storage manager
Expand Down
2 changes: 1 addition & 1 deletion geonode/documents/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def create_document_thumbnail(self, object_id):
logger.warning(f"Thumbnail for document #{object_id} empty.")
ResourceBase.objects.filter(id=document.id).update(thumbnail_url=None)
else:
filename = f'document-{document.uuid}-thumb.png'
filename = f'document-{document.uuid}-thumb.jpg'
document.save_thumbnail(filename, thumbnail_content)
logger.debug(f"Thumbnail for document #{object_id} created.")

Expand Down
2 changes: 2 additions & 0 deletions geonode/documents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ def test_image_documents_thumbnail(self):
)
file = Image.open(thumb_file)
self.assertEqual(file.size, (400, 200))
# check thumbnail qualty and extention
self.assertEqual(file.format, 'JPEG')
finally:
Document.objects.filter(title='img File Doc').delete()

Expand Down
2 changes: 1 addition & 1 deletion geonode/thumbs/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def test_tile_background_generic_fetch(self):
)
def test_tile_background_generic_fetch_zoom(self):

width = 240
width = 500
height = 200

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

0 comments on commit 254747e

Please sign in to comment.