Sub topic 15
Handling Sensitive Images and Documents
This chapter assumes you can already build image and PDF requests with base64 or URL sources, as covered in earlier chapters. Here you will look at what happens to the data once it leaves your machine, and how to reduce the risk.
What leaves your machine
Every image or PDF you send is a copy of the original. The provider receives it, processes it, and may keep it for some time. Images carry more than people expect:
- Visible content such as faces, ID numbers, account details and medical records.
- Metadata such as EXIF fields, which can include GPS location, device model and timestamps.
- Hidden PDF content such as author names, comments, revision history and text layers beneath scanned pages.
Check your provider's data retention and training policies before you send real user data. These policies differ between providers, between API and consumer products, and between account tiers. Do not assume the defaults suit your case.
Base64 versus URL
The way you supply data affects exposure.
| Source | Where the data travels | Main risk |
|---|---|---|
| Base64 inline | Inside the request body, over TLS | Appears in any request logging you enable |
| Public URL | The provider fetches it from your server | Anyone with the link can read the file |
| Signed URL | The provider fetches it before the link expires | A leaked link works until expiry |
| Uploaded file | Stored by the provider until you delete it | Lingers if you forget to delete it |
Avoid making a sensitive file public just so a model can fetch it. If you use URLs, make them signed and short-lived. If you use file uploads, delete files when the job finishes.
Strip metadata before sending
Re-encoding an image with Pillow drops most EXIF data, because Pillow does not copy it unless you ask. The downscale function from the preprocessing chapter already does this. This version copies only the pixels, which makes the intent explicit:
from PIL import Image
def strip_metadata(src_path, dst_path):
"""Save a copy of an image with pixel data only, no EXIF or other metadata."""
with Image.open(src_path) as img:
img = img.convert("RGB")
clean = Image.new("RGB", img.size)
clean.putdata(list(img.getdata()))
clean.save(dst_path, format="JPEG", quality=90)
strip_metadata("scan.jpg", "scan_clean.jpg")For PDFs, rendering pages to PNG with PyMuPDF also discards document properties and hidden text, since only pixels remain.
Redact before you send
The safest sensitive field is one the model never sees. If you only need a total from an invoice, black out the account number first. Draw filled rectangles over known regions, then send the redacted copy.
from PIL import Image, ImageDraw
def redact(src_path, dst_path, boxes):
"""Cover each (left, top, right, bottom) box with solid black."""
with Image.open(src_path) as img:
img = img.convert("RGB")
draw = ImageDraw.Draw(img)
for box in boxes:
draw.rectangle(box, fill="black")
img.save(dst_path, format="PNG")
redact("invoice.png", "invoice_redacted.png", [(40, 120, 300, 160)])Redaction must change the pixels. Overlaying a black shape on a PDF page leaves the original text underneath, still extractable. Render the page to an image first, then redact the image.
Other practices
- Treat text inside images as untrusted. A photo or document can contain written instructions aimed at the model. Do not let model output trigger actions, such as sending email or running commands, without checks.
- Keep keys in environment variables, never in code or logs.
- Do not log full base64 payloads. Log the file name, size and a hash instead.
- Validate output. Models can misread or invent values, so a person should review high-stakes extractions.
- Collect only what you need. Crop to the relevant region before sending.
Exercise
A teammate draws a black rectangle over an account number in a PDF using an annotation tool, then sends the PDF natively to a model. Why is this unsafe, and what should they do instead?
Show answer
The rectangle is only a layer on top of the page. The original text is still in the file and the model, or any text extractor, can read it. They should render the page to an image, draw the redaction on the pixels, optionally strip metadata, and send that image instead.