25 lines
664 B
Python
25 lines
664 B
Python
from PIL import Image
|
|
from io import BytesIO
|
|
|
|
from app.tasks.celery import celery
|
|
|
|
|
|
@celery.task
|
|
def resize_image(
|
|
image: dict[str, bytes],
|
|
target_width: int,
|
|
) -> dict[str, bytes]:
|
|
image = image["file"]
|
|
|
|
image = Image.open(BytesIO(image))
|
|
|
|
width_percent = (target_width / float(image.size[0]))
|
|
height_size = int((float(image.size[1]) * float(width_percent)))
|
|
|
|
resized_image = image.resize((target_width, height_size))
|
|
|
|
resized_image_byte_array = BytesIO()
|
|
resized_image.save(resized_image_byte_array, format=image.format)
|
|
new_image = resized_image_byte_array.getvalue()
|
|
|
|
return {"new_image": new_image}
|