perf: Reduce memory usage of exif stripping (#35566)

This was at least 4x inefficient because of:

- Way too many copies of same buffers
- Serialization of binary data to python integers :woozy: using `list()`
This commit is contained in:
Ankush Menat 2025-12-31 17:00:13 +05:30 committed by GitHub
parent a5cf31c546
commit 987be17ea1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,13 +37,8 @@ def strip_exif_data(content, content_type) -> bytes:
if content_type == "image/jpeg" and original_image.mode in ("RGBA", "P"):
original_image = original_image.convert("RGB")
new_image = Image.new(original_image.mode, original_image.size)
new_image.putdata(list(original_image.getdata()))
new_image.save(output, format=content_type.split("/")[1])
content = output.getvalue()
return content
original_image.save(output, format=content_type.split("/")[1], exif=b"")
return output.getvalue()
def optimize_image(content, content_type, max_width=1024, max_height=768, optimize=True, quality=85):