Last active
May 21, 2025 03:19
-
-
Save aarongundel/bf93bb4ec34a7ed70cb7dc0d8f62f53f to your computer and use it in GitHub Desktop.
Arches Simple Thumbnailer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import filetype | |
class SimpleThumbnailer(ThumbnailGenerator): | |
def make_thumbnail(self, inputfile_path, outputfile_path, **kwargs): | |
""" | |
Args: | |
inputfile_path: a path to a file on disk to create a thumbnail from | |
outputfile_path: a path to a file on disk where the thumbnail will be written to | |
Returns: | |
None | |
""" | |
try: | |
# image extensions | |
image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp') | |
if not inputfile_path.lower().endswith(image_extensions): | |
return False | |
with Image.open(inputfile_path) as img: | |
img.thumbnail((300, 300)) | |
if img.mode in ('RGBA', 'P'): | |
img = img.convert('RGB') | |
img.save(outputfile_path) | |
return True | |
except Exception as e: | |
print(_("Thumbnail generation failed: %s") % str(e)) | |
return False |
@ydrea I'm happy to update this to something that you've actually tested! Thanks! Is there a warranty on these changes? 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to use this with 'GENERATE_THUMBNAILS_ON_DEMAND = False', but it broke my gunicorn (took too much CPU time).
A first attempt of a fix is: