Hello! Commit https://github.com/stephenmcd/mezzanine/commit/92f0a102647303358bf4dc949a0be154f62d8d5c removed code comparing the size of thumbnail with original image and, if it is the same, returning the original path. Can someone explain, what was the reason of this change? Is it because image processing on every page rendering consumes CPU? Something else?
Most of images on our site are already optimized in Photoshop. Additional transformation using PIL/Pillow slightly degrades their quality. That’s why I patched the Mezzanine code to save binary copies of images as thumbnails when the size is the same. On first call it loads image and does all PIL magic to retrieve dimensions, then copies. All following calls just look at file system without any further processing. Could you, please, look at this patch and, if you like it, merge it to the original code? With best regards, Vladislav --- core/templatetags/mezzanine_tags.py.orig 2016-01-20 19:57:43.000000000 +0300 +++ core/templatetags/mezzanine_tags.py 2016-01-20 20:06:09.000000000 +0300 @@ -319,9 +319,9 @@ # Requested image does not exist, just return its URL. return image_url - f = default_storage.open(image_url) + orig_f = default_storage.open(image_url) try: - image = Image.open(f) + image = Image.open(orig_f) except: # Invalid image format. return image_url @@ -369,13 +369,16 @@ to_size = (to_width, to_height) to_pos = (left, top) try: - image = ImageOps.fit(image, to_size, Image.ANTIALIAS, 0, to_pos) - image = image.save(thumb_path, filetype, quality=quality, **image_info) - # Push a remote copy of the thumbnail if MEDIA_URL is - # absolute. - if "://" in settings.MEDIA_URL: - with open(thumb_path, "rb") as f: - default_storage.save(thumb_url, File(f)) + if not padding and from_width == to_width and from_height == to_height: + default_storage.save(thumb_url, orig_f) + else: + image = ImageOps.fit(image, to_size, Image.ANTIALIAS, 0, to_pos) + image = image.save(thumb_path, filetype, quality=quality, **image_info) + # Push a remote copy of the thumbnail if MEDIA_URL is + # absolute. + if "://" in settings.MEDIA_URL: + with open(thumb_path, "rb") as f: + default_storage.save(thumb_url, File(f)) except Exception: # If an error occurred, a corrupted image may have been saved, # so remove it, otherwise the check for it existing will just -- You received this message because you are subscribed to the Google Groups "Mezzanine Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
--- core/templatetags/mezzanine_tags.py.orig 2016-01-20 19:57:43.000000000 +0300 +++ core/templatetags/mezzanine_tags.py 2016-01-20 20:06:09.000000000 +0300 @@ -319,9 +319,9 @@ # Requested image does not exist, just return its URL. return image_url - f = default_storage.open(image_url) + orig_f = default_storage.open(image_url) try: - image = Image.open(f) + image = Image.open(orig_f) except: # Invalid image format. return image_url @@ -369,13 +369,16 @@ to_size = (to_width, to_height) to_pos = (left, top) try: - image = ImageOps.fit(image, to_size, Image.ANTIALIAS, 0, to_pos) - image = image.save(thumb_path, filetype, quality=quality, **image_info) - # Push a remote copy of the thumbnail if MEDIA_URL is - # absolute. - if "://" in settings.MEDIA_URL: - with open(thumb_path, "rb") as f: - default_storage.save(thumb_url, File(f)) + if not padding and from_width == to_width and from_height == to_height: + default_storage.save(thumb_url, orig_f) + else: + image = ImageOps.fit(image, to_size, Image.ANTIALIAS, 0, to_pos) + image = image.save(thumb_path, filetype, quality=quality, **image_info) + # Push a remote copy of the thumbnail if MEDIA_URL is + # absolute. + if "://" in settings.MEDIA_URL: + with open(thumb_path, "rb") as f: + default_storage.save(thumb_url, File(f)) except Exception: # If an error occurred, a corrupted image may have been saved, # so remove it, otherwise the check for it existing will just