Sub topic 6
Resizing and normalizing images before sending
This chapter assumes you can call a multimodal chat API, build a message with an image content block, and read a base64 string. It covers what to do to an image before it goes into that block.
Why preprocess
Providers resize large images on their side. If you send a 6000-pixel photo, you pay to upload every byte, and the provider throws away most of the detail anyway. Shrinking the image first gives you three things:
- A smaller request body, so the upload is faster.
- Fewer rejected requests, because many APIs cap file size and pixel dimensions.
- Predictable cost, because image tokens grow with image dimensions.
Check your provider's documentation for the actual maximum size. The value in the code below, 1568 pixels on the longest side, is an example and not a universal limit.
Downscaling
The function below shrinks an image so its longest side is at most max_side pixels. It also converts to RGB and saves as JPEG.
from PIL import Image
MAX_SIDE = 1568
def downscale(src_path, dst_path, max_side=MAX_SIDE):
"""Shrink an image so its longest side is at most max_side pixels."""
with Image.open(src_path) as img:
img = img.convert("RGB")
img.thumbnail((max_side, max_side)) # keeps aspect ratio, never enlarges
img.save(dst_path, format="JPEG", quality=90)
downscale("photo_large.png", "photo_small.jpg")Three details matter here:
thumbnailchanges the image in place and keeps the aspect ratio. It never enlarges a small image.convert("RGB")removes the alpha (transparency) channel. JPEG cannot store transparency, so saving an RGBA image as JPEG would raise an error without this step.quality=90keeps text and thin lines readable. Very low quality values add compression artifacts that the model can misread.
The preprocessing flow
When not to compress
JPEG is lossy. For screenshots, scanned forms, and charts with small labels, a JPEG can blur the characters you want read. In those cases keep PNG and reduce dimensions only. Change the save call like this:
img.save(dst_path, format="PNG", optimize=True)Also avoid shrinking too far. If the text in the original is 10 pixels tall and you cut the image to a third of its size, it becomes unreadable to the model, just as it would to you.
Normalizing
Normalizing means making every image in your pipeline look the same to your code. Useful steps are:
- Apply the camera orientation tag, so phone photos are not sent sideways.
- Convert to RGB.
- Use one output format and one size limit.
Pillow provides ImageOps.exif_transpose for the first step. Call it right after opening the file:
from PIL import Image, ImageOps
with Image.open("phone_photo.jpg") as img:
img = ImageOps.exif_transpose(img)
print(img.size)Rotated images are a common cause of poor answers, because the model sees the pixels and not the orientation tag.
Check the result
After resizing, compare file sizes and confirm the output still looks right. Open the output and read the smallest text with your own eyes. If you cannot read it, the model probably cannot either.
Exercise
A user uploads a PNG screenshot of a spreadsheet with small numbers. Your pipeline converts every image to JPEG at quality 40 and a 600-pixel maximum side. The model keeps misreading digits. Name two changes you would make.
Show answer
Keep the screenshot as PNG (or use high JPEG quality such as 90), and raise the maximum side so the digits stay legible, within the provider's documented limit. Reading the output file yourself confirms the digits are clear before sending.