fix non-resized images

This commit is contained in:
Bryan Gerlach
2024-09-26 11:24:06 -05:00
parent 4d5c75bf02
commit 13a2cf13a3

View File

@@ -226,6 +226,7 @@ def update_github_run(request):
return HttpResponse('') return HttpResponse('')
def resize_and_encode_icon(imagefile): def resize_and_encode_icon(imagefile):
print(imagefile)
try: try:
with io.BytesIO() as image_buffer: with io.BytesIO() as image_buffer:
for chunk in imagefile.chunks(): for chunk in imagefile.chunks():
@@ -239,7 +240,10 @@ def resize_and_encode_icon(imagefile):
# Check if resizing is necessary # Check if resizing is necessary
if img.size[0] <= 256: if img.size[0] <= 256:
return_image = ContentFile(imgcopy.read(), name=imagefile.name) with io.BytesIO() as image_buffer:
imgcopy.save(image_buffer, format=imagefile.content_type.split('/')[1])
image_buffer.seek(0)
return_image = ContentFile(image_buffer.read(), name=imagefile.name)
return base64.b64encode(return_image.read()) return base64.b64encode(return_image.read())
# Calculate resized height based on aspect ratio # Calculate resized height based on aspect ratio
@@ -247,10 +251,10 @@ def resize_and_encode_icon(imagefile):
hsize = int((float(img.size[1]) * float(wpercent))) hsize = int((float(img.size[1]) * float(wpercent)))
# Resize the image while maintaining aspect ratio using LANCZOS resampling # Resize the image while maintaining aspect ratio using LANCZOS resampling
iconimgcopy = imgcopy.resize((256, hsize), Image.Resampling.LANCZOS) imgcopy = imgcopy.resize((256, hsize), Image.Resampling.LANCZOS)
with io.BytesIO() as resized_image_buffer: with io.BytesIO() as resized_image_buffer:
iconimgcopy.save(resized_image_buffer, format=imagefile.content_type.split('/')[1]) imgcopy.save(resized_image_buffer, format=imagefile.content_type.split('/')[1])
resized_image_buffer.seek(0) resized_image_buffer.seek(0)
resized_imagefile = ContentFile(resized_image_buffer.read(), name=imagefile.name) resized_imagefile = ContentFile(resized_image_buffer.read(), name=imagefile.name)