File size: 594 Bytes
aa151d2
 
 
 
 
 
 
 
 
e337419
 
 
 
 
aa151d2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"""Image preprocessing utilities for LEGATO OMR."""
from PIL import Image
from config import LETTER_ASPECT


def pad_to_portrait_letter(pil_image: Image.Image) -> Image.Image:
    """If aspect ratio is wider than letter, pad at the bottom to match letter aspect."""
    if pil_image.mode != "RGB":
        pil_image = pil_image.convert("RGB")
    w, h = pil_image.size
    pil_image = pil_image.resize((1050, 1050*h//w))
    if pil_image.height >= 1485:
        return pil_image
    canvas = Image.new("RGB", (1050, 1485), (255, 255, 255))
    canvas.paste(pil_image, (0, 0))
    return canvas