File size: 1,005 Bytes
c09dbef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from PIL import Image
import io
import base64

def image_processor(uploaded_image):
    img = Image.open(uploaded_image)

    if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
        background = Image.new('RGB', img.size, (255, 255, 255))
        background.paste(img, mask=img.split()[-1])
        img = background

    max_size = 1024
    if max(img.size) > max_size:
        ratio = max_size / max(img.size)
        new_size = tuple(int(dim * ratio) for dim in img.size)
        img = img.resize(new_size, Image.Resampling.LANCZOS)

    output_buffer = io.BytesIO()
    img.save(output_buffer, format='PNG', quality=95)
    while output_buffer.tell() > 1024 * 1024:
        quality = int(95 * (1024 * 1024 / output_buffer.tell()))
        output_buffer = io.BytesIO()
        img.save(output_buffer, format='PNG', quality=quality)

    base64_encoded = base64.b64encode(output_buffer.getvalue()).decode('utf-8')

    return base64_encoded