Process EXIF orientation for uploaded files

main
Thomas Sileo 2022-09-03 10:15:37 +02:00
parent 16da166ee1
commit f7671f0585
1 changed files with 13 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import blurhash # type: ignore
from fastapi import UploadFile from fastapi import UploadFile
from loguru import logger from loguru import logger
from PIL import Image from PIL import Image
from PIL import ImageOps
from sqlalchemy import select from sqlalchemy import select
from app import activitypub as ap from app import activitypub as ap
@ -46,10 +47,12 @@ async def save_upload(db_session: AsyncSession, f: UploadFile) -> models.Upload:
height = None height = None
if f.content_type.startswith("image"): if f.content_type.startswith("image"):
image_blurhash = blurhash.encode(f.file, x_components=4, y_components=3) with Image.open(f.file) as _original_image:
f.file.seek(0) # Fix image orientation (as we will remove the info from the EXIF
# metadata)
original_image = ImageOps.exif_transpose(_original_image)
with Image.open(f.file) as original_image: # Re-creating the image drop the EXIF metadata
destination_image = Image.new( destination_image = Image.new(
original_image.mode, original_image.mode,
original_image.size, original_image.size,
@ -57,13 +60,16 @@ async def save_upload(db_session: AsyncSession, f: UploadFile) -> models.Upload:
destination_image.putdata(original_image.getdata()) destination_image.putdata(original_image.getdata())
destination_image.save( destination_image.save(
dest_filename, dest_filename,
format=original_image.format, format=_original_image.format,
) )
with open(dest_filename, "rb") as dest_f:
image_blurhash = blurhash.encode(dest_f, x_components=4, y_components=3)
try: try:
width, height = original_image.size width, height = destination_image.size
original_image.thumbnail((740, 740)) destination_image.thumbnail((740, 740))
original_image.save( destination_image.save(
UPLOAD_DIR / f"{content_hash}_resized", UPLOAD_DIR / f"{content_hash}_resized",
format="webp", format="webp",
) )