start docs (AI) fix some caching logic
All checks were successful
Datadog Secrets Scanning / Datadog Static Analyzer (push) Successful in 9s
Datadog Software Composition Analysis / Datadog SBOM Generation and Upload (push) Successful in 15s
Datadog Static Analysis / Datadog Static Analyzer (push) Successful in 19s
Release / build (release) Successful in 39s
All checks were successful
Datadog Secrets Scanning / Datadog Static Analyzer (push) Successful in 9s
Datadog Software Composition Analysis / Datadog SBOM Generation and Upload (push) Successful in 15s
Datadog Static Analysis / Datadog Static Analyzer (push) Successful in 19s
Release / build (release) Successful in 39s
This commit is contained in:
@ -1,12 +1,9 @@
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
from functools import cache
|
||||
|
||||
@cache
|
||||
def generate_thumbnail(image_path, resize_percent, min_width):
|
||||
# Generate a unique key based on the image path, resize percentage, and minimum width
|
||||
key = f"{image_path}_{resize_percent}_{min_width}"
|
||||
from functools import lru_cache
|
||||
|
||||
@lru_cache(maxsize=512)
|
||||
def generate_thumbnail(image_path, resize_percent, min_width, max_width):
|
||||
# Open the image file
|
||||
with Image.open(image_path) as img:
|
||||
# Calculate the new size based on the resize percentage
|
||||
@ -20,13 +17,19 @@ def generate_thumbnail(image_path, resize_percent, min_width):
|
||||
new_width = min_width
|
||||
new_height = int(new_height * scale_factor)
|
||||
|
||||
# Ensure the maximum width is not exceeded
|
||||
if new_width > max_width:
|
||||
scale_factor = max_width / new_width
|
||||
new_width = max_width
|
||||
new_height = int(new_height * scale_factor)
|
||||
|
||||
# Resize the image while maintaining the aspect ratio
|
||||
img.thumbnail((new_width, new_height))
|
||||
|
||||
# Rotate the image based on the EXIF orientation tag
|
||||
try:
|
||||
exif = img._getexif()
|
||||
orientation = exif.get(0x0112, 1) # 0x0112 is the EXIF orientation tag
|
||||
exif = img.info['exif']
|
||||
orientation = img._getexif().get(0x0112, 1) # 0x0112 is the EXIF orientation tag
|
||||
if orientation == 3:
|
||||
img = img.rotate(180, expand=True)
|
||||
elif orientation == 6:
|
||||
@ -35,12 +38,12 @@ def generate_thumbnail(image_path, resize_percent, min_width):
|
||||
img = img.rotate(90, expand=True)
|
||||
except (AttributeError, KeyError, IndexError):
|
||||
# cases: image don't have getexif
|
||||
pass
|
||||
exif = None
|
||||
|
||||
# Save the thumbnail to a BytesIO object
|
||||
thumbnail_io = BytesIO()
|
||||
img_format = img.format if img.format in ["JPEG", "JPG", "PNG"] else "JPEG"
|
||||
img.save(thumbnail_io, format=img_format)
|
||||
img.save(thumbnail_io, format=img_format, exif=exif)
|
||||
thumbnail_io.seek(0)
|
||||
|
||||
return (thumbnail_io.getvalue(), img_format)
|
Reference in New Issue
Block a user