QR code with a logo and a frame (ride link)¶
A QR code printed on the car window, on a poster at the bus station or in an image posted on a WhatsApp status takes the passenger straight to the ride. With the brand's logo in the middle, it also says whose link it is before anyone points a camera at it. And in a frame with the driver's photo and name, the route and the written link, the image explains itself.
The logo covers modules of the QR code. Error correction makes up for
that: level H recovers up to 30% of the symbol. tempest_fastapi_sdk.share
always generates the QR code at level H, pastes the logo on a white frame
at a size that was measured to keep reading, and refuses a logo fraction above
the measured ceiling.
By the end you will have:
render_qr(url, logo=...): the PNG of the QR code with the logo in the middle;ShareFrameRenderer: the frame in two shapes, vertical for the status and square for a chat or a group;GET /api/caronas/{trip_id}/qr.pngandGET /api/caronas/{trip_id}/moldura.png, pointing at the ride's share page.
Installation¶
The QR code is generated by segno (pure
Python, no dependencies) and drawn with Pillow. Both come with the [image]
extra:
The frame writes text, so it needs a TrueType font on the machine, like the preview card. See Font in Docker.
Step 1: the QR code with the logo¶
from pathlib import Path
from tempest_fastapi_sdk.share import render_qr
png: bytes = render_qr(
"https://app.buscar.com.br/s/42",
logo=Path("static/logo.png"),
)
Path("qr.png").write_bytes(png)
The result, with a 400×400 logo:

What render_qr does for you:
- Level
H, always. Left alone,segno1.6.6 pickedL(7% recovery) for this recipe's links, andLleaves no redundancy for the logo. - Never Micro QR.
segnopicks Micro QR for very short text (for"42"it produced anM1), and phone readers generally do not support it. - Logo at 25% of the QR side, sized against the QR code without its border, keeping its aspect ratio, on a one-module white frame. The frame separates the logo from the modules around it.
- A 35% ceiling.
logo_fractionaboveMAX_QR_LOGO_FRACTIONraisesValueErrorbefore drawing.
The other parameters: scale is pixels per module. For large prints, raise
scale instead of enlarging the finished PNG, because enlarging blurs the
module edges. border is the white margin in modules, and 4 is what the
standard asks for.
How much logo fits¶
Measured with segno 1.6.6 and zxing-cpp 3.1.1. Setup: level H QR code,
scale=10, square opaque logo on a white frame, and two links, one of 30
characters (QR version 4, 33×33 modules) and one of 64 (version 7, 45×45).
"Degraded" is the image shrunk and blurred, in four combinations (shrinking
from ½ to ¼, Gaussian blur from 0.6 to 1.0 px), to imitate a camera
photo:
| Logo side (fraction of the QR) | Area covered | 30-char link: clean / degraded | 64-char link: clean / degraded |
|---|---|---|---|
| 20% | 4% | reads / 4 of 4 | reads / 4 of 4 |
| 25% | 6% | reads / 4 of 4 | reads / 4 of 4 |
| 30% | 9% | reads / 4 of 4 | reads / 4 of 4 |
| 35% | 12% | reads / 4 of 4 | reads / 4 of 4 |
| 40% | 16% | fails / 0 of 4 | reads / 2 of 4 |
| 45% | 20% | fails / 0 of 4 | fails / 0 of 4 |
Hence the default DEFAULT_QR_LOGO_FRACTION = 0.25, three steps below the
first failure (40%), and the ceiling MAX_QR_LOGO_FRACTION = 0.35, the last value
that read in every case. The default's margin is for what the measurement
does not reproduce: glare on the window, low-resolution printing, an old
phone camera. The test tests/share/test_qr.py decodes the QR code at the
default and at the ceiling, for both links, and confirms that 40% stops
reading the short link.
This measures a reader, not a camera
The table was measured by decoding the image with zxing-cpp, not by
pointing a phone at it. Before printing in bulk, test the final QR code
with two or three different cameras, at the distance and in the light
where it will be read.
Two choices outside the logo matter too:
- Link length. At the same printed size, more modules are smaller
modules, and the camera has to come closer. Prefer
/s/42to a URL with a UUID and campaign parameters. - Colors. The table was measured with black modules on a white
background, which is what
render_qrdraws. Put the brand color in the logo.
Step 2: the frame to share¶
The frame is the image the driver posts. It carries its own way back: the QR code, for whoever sees the image on another screen, and the written link, for whoever cannot scan.
from pathlib import Path
from tempest_fastapi_sdk.share import (
ShareFrameLayoutEnum,
ShareFrameRenderer,
ShareFrameSchema,
)
renderer = ShareFrameRenderer(
background=("#0b3d91", "#061f4a"),
accent_color="#ffd166",
logo=Path("static/logo.png"),
)
frame = ShareFrameSchema(
url="https://app.buscar.com.br/s/42",
title="Ana Souza",
subtitle="Teresina → Fortaleza · Friday, 6pm",
brand="BusCar",
caption="Scan and book your seat",
)
story: bytes = renderer.render(
frame,
layout=ShareFrameLayoutEnum.STORY,
photo=Path("ana.jpg").read_bytes(),
)
square: bytes = renderer.render(
frame,
layout=ShareFrameLayoutEnum.SQUARE,
photo=Path("ana.jpg").read_bytes(),
)
Vertical (STORY, 1080×1920), for the status |
Square (SQUARE, 1080×1080), for a chat or a group |
|---|---|
![]() |
![]() |
(The images show the Portuguese version of the same frame.)
Piece by piece:
ShareFrameRenderertakes the same arguments as the preview'sShareCardRenderer: colors,logo,font_path. Use the same colors on both, and the card and the frame look alike. An invalid color, a broken logo and a missing font fail at construction.- The logo shows up twice: at the top, next to
brand, and in the middle of the QR code, under the same rule asrender_qr. photois the person's photo, asbytes. It is cropped from the center into a circle, ringed in the accent color, with the EXIF orientation applied. Without a photo, the circle carries the initials oftitle("Ana Souza"becomesAS). A photo that does not open, or has more thanMAX_PHOTO_PIXELS, raisesValueError.show_url=Truewrites the link withouthttps://under the QR code.- Long text shrinks, then is cut with
…, and never leaves the margin. A line break becomes a space.
The test tests/share/test_frame.py decodes the QR code inside the
frame in both shapes: with and without a photo, with and without a logo,
with long text and with a UUID link.
Why the photo comes from your service
photo is bytes, not a URL. Fetching on the server a URL that came
from the client opens the door to SSRF (the server making requests
wherever the attacker wants). Read the photo from your storage, where it
was already validated at upload.
Step 3: the ride's routes¶
The QR code points at the ride's share page (/s/{id}, from the
Sharing with a preview recipe), not at the app directly.
Whoever scans it lands in the browser, and the page takes them to the app.
The same link, pasted into WhatsApp, shows the preview with the card.
import asyncio
from pathlib import Path
from fastapi import FastAPI, HTTPException, Response
from tempest_fastapi_sdk.share import (
ShareFrameLayoutEnum,
ShareFrameRenderer,
ShareFrameSchema,
render_qr,
)
PUBLIC_URL: str = "https://app.buscar.com.br"
LOGO: Path = Path("static/logo.png")
TRIPS: dict[str, dict[str, str]] = {
"42": {
"driver": "Ana Souza",
"route": "Teresina → Fortaleza · Friday, 6pm",
"photo": "static/ana.jpg",
},
}
renderer = ShareFrameRenderer(
background=("#0b3d91", "#061f4a"),
accent_color="#ffd166",
logo=LOGO,
)
app = FastAPI()
def png_response(png: bytes) -> Response:
return Response(
png,
media_type="image/png",
headers={"Cache-Control": "public, max-age=3600"},
)
@app.get("/api/caronas/{trip_id}/qr.png", response_class=Response)
async def trip_qr(trip_id: str) -> Response:
if trip_id not in TRIPS:
raise HTTPException(status_code=404)
png = await asyncio.to_thread(render_qr, f"{PUBLIC_URL}/s/{trip_id}", logo=LOGO)
return png_response(png)
@app.get("/api/caronas/{trip_id}/moldura.png", response_class=Response)
async def trip_frame(
trip_id: str,
layout: ShareFrameLayoutEnum = ShareFrameLayoutEnum.STORY,
) -> Response:
trip = TRIPS.get(trip_id)
if trip is None:
raise HTTPException(status_code=404)
frame = ShareFrameSchema(
url=f"{PUBLIC_URL}/s/{trip_id}",
title=trip["driver"],
subtitle=trip["route"],
brand="BusCar",
caption="Scan and book your seat",
)
photo = Path(trip["photo"]).read_bytes()
png = await asyncio.to_thread(renderer.render, frame, layout=layout, photo=photo)
return png_response(png)
The routes live under /api/ on purpose. Behind the nginx of the
share page, only /s/ and
/api/ reach the backend, with ^~. Outside them, a path ending in .png
lands in the SPA's static block. Measured on nginx:alpine 1.31.9:
/caronas/42/qr.png gave 404, and /api/caronas/42/qr.png gave
200 image/png.
GET /api/caronas/42/moldura.png returns the vertical frame, and
GET /api/caronas/42/moldura.png?layout=square the square one. FastAPI validates
layout against the enum and answers 422 for any other value.
Off the event loop. Drawing the frame took about 105 ms (mean of 10,
vertical, with photo and logo, Pillow 13.0.0, on the SDK's development
machine). The QR code alone took about 13 ms (mean of 50, 64-character link,
logo read from disk on every call). Both go through asyncio.to_thread, so
the loop keeps serving other requests.
Short cache. The frame changes when the ride changes: time, photo,
driver. One hour of Cache-Control limits how long a stale image circulates.
If your model has updated_at, add ?v=<updated_at> to the image link, as
the share page does
with the card.
Recap¶
render_qr(url, logo=...)generates the QR code at levelH, without Micro QR, with the logo at 25% of the side on a white frame. Above 35% it refuses: 40% already failed in the measurement.ShareFrameRenderer.render(frame, layout=..., photo=...)draws the frame: logo and brand, round photo (or the initials), name, route, the QR code on a white panel, the written link and the call to action.STORY(1080×1920) is for the status,SQUARE(1080×1080) for a chat or a group.- The QR code points at
/s/{id}: whoever scans it reaches the app, and the same link has a preview on WhatsApp. - The photo comes from your storage, as
bytes, never from a URL fetched on the server. - Before printing, test with real cameras.

