Generate a styled QR code with optional text, icons, and frame options.
- Python 3.10+
- Packages:
qrcode[pil],pillow
Install:
pip install --upgrade qrcode[pil] pillowpython3 qr.pyThe output image is written to custom_qr.png in the project directory.
Edit the values in the if __name__ == "__main__": block in qr.py:
DATA: the QR payload (URL or text)LINES: three text lines displayed in the centerFONT: optional path to a.ttffontout_path: output filename
Key options you can tweak:
size_px: output size in pixelsborder: quiet zone size (larger improves scan reliability)center_box_ratio: size of the text layout area (smaller is safer to scan)text_fill_mode:"solid"or"split"for two-color texttext_split_colors: text colors as RGB tuplesauto_fit_text: set toFalseto makefont_sizetake effect
The main function is make_custom_qr(...) in qr.py. Below is a concise map of what each feature group does so you can tune output without reading the whole code.
out_path: output PNG filenamesize_px: output image size (square)bg,fg: background and module colorsborder: quiet zone (increase for better scanning)use_styled_modules:Truefor rounded modules,Falsefor crisp squares
inset_px: reserves padding inside the canvas; QR is drawn smallerinset_corner_icon_paths: optional icons in the inset cornersframe_px: adds an outer frame around the final imageframe_color: fill color for the frameframe_corner_icon_paths: icons placed on the frame corners
center_box_ratio: size of the text layout region relative to the canvasdraw_center_box: toggle drawing the box (still used for layout ifFalse)center_box_fill_color: fill color for the boxcenter_box_outline_width,center_box_outline_color: optional outline
text_lines: three lines of text (tuple of 3 strings)font_path,font_size: font selection and base sizeauto_fit_text: auto scales text to fit the layout regiontext_fill_mode:"solid"or"split"for two-color texttext_colorortext_split_colors: fill color(s)text_split_angle_deg,text_split_position,text_split_invert: split gradient controlstext_render_mode:"fill","outline", or"fill_outline"text_outline_color_mode:"solid"or"split"for outline colortext_stroke_width: explicit outline thickness (pixels)letter_spacing_px,word_spacing_px: manual spacing
text_horizontal_align:"left","center","right"text_vertical_align:"top"or"center"text_left_padding_ratio,text_top_padding_ratio: alignment paddingtext_margin_ratio: inner margin for text layout regionline_spacing_ratio: vertical spacing between linesline_scales: per-line scale factorsline_x_offsets_ratio: per-line horizontal offsetline_horizontal_aligns: per-line alignment overrideline_height_mode:"bbox"(glyph-based) or"metrics"(consistent)text_vertical_offset_ratio: extra vertical shift
knockout_text_bg: clears modules behind textknockout_padding_ratio: padding around each lineknockout_mode:"lines"or"block"knockout_block_radius_ratio: roundness for block mode
icon_pathoricon_paths: add one icon or a row of iconsicon_size_ratio: icon size relative to the text areaicon_gap_ratio: vertical gap between icons and texticon_row_gap_ratio: horizontal gap between multiple iconsicon_offset_ratio: (x, y) offset relative to the text areaicon_white_to_transparent: remove white pixels from icons
Icons are drawn in the finder corners using corner_icon_paths.
Overlay modes:
ring: icon is clipped into the white ring (scan-safe)tint/tint_invert: icon tints the finder pattern (usescorner_tint_darkandcorner_tint_light)center_cutout: icon is cut out of the inner square (usescorner_center_icon_colors)overlay: icon is drawn over the full finder area (most visible, less scan-safe)
Related options:
corner_icon_size_ratio: overall icon sizecorner_icon_overlay_alpha: opacity for"overlay"corner_icon_colors: per-corner recolor (used by"ring"and"overlay")corner_center_icon_colors: per-corner color for"center_cutout"corner_icon_white_to_transparent: remove white from icon assets
Use this to preview text layout without rendering the QR:
render_text_only=Truetext_only_size: custom size for the text-only imagetext_only_bg: background color for text-only mode
Icons are drawn in the finder corners using corner_icon_paths.
Overlay modes:
ring: icon is clipped into the white ring (scan-safe)tint/tint_invert: icon tints the finder pattern (usescorner_tint_darkandcorner_tint_light)center_cutout: icon is cut out of the inner square (usescorner_center_icon_colors)overlay: icon is drawn over the full finder area (most visible, less scan-safe)
If you want the icon on top of the dots, use:
corner_icon_overlay_mode="overlay"Adjust visibility:
corner_icon_size_ratio: overall icon size (0.8 to 1.1 typical)corner_icon_overlay_alpha: icon opacity (0 to 255)
- Use
border >= 6for better scanning - Keep
center_box_ratiomodest (0.26 to 0.32) - Avoid large opaque overlays in the center
See LICENSE.