Code2Img

Python code screenshots.

How to screenshot Python code properly: what Python snippets actually look like, how auto-detection behaves, the theme that reads best, and the settings that keep the card legible.

What people actually paste

Python snippets arrive in two very different shapes, and they need different treatment. The first is a decorated function body: @app.route / @dataclass above a def, a type-hinted signature, and an indented block that carries the meaning. The second is a fragment with no def at all — a dict literal, a list comprehension, a pandas chain (df.groupby(...).agg(...).reset_index()), or a ten-line with block. Python's grammar is almost entirely by indentation, so a fragment loses the only cue that tells a reader where blocks begin; whitespace is content here in a way it is not in any brace language.

How auto-detection behaves

Python owns rule 1, the highest priority in the whole list, so def name(...):, a bare import x line, or from x import is enough to win outright — even if the snippet also contains Ruby, Go or SQL-looking lines. That is why Python detections are the most reliable of the ten. The failure mode is the opposite: fragments without those three shapes. A bare dict literal {"a": 1} matches rule 13 (a line starting with {) and renders as JavaScript; a comprehension-only snippet falls through to the typescript default. Anything you copy from a REPL also tends to arrive with >>> prompts and wrapped output, which the highlighter will faithfully colour as syntax errors.

Theme that reads best

catppuccin-latte for light cards: Python has few token classes (keyword, builtin, string, decorator) and a pastel palette keeps docstrings from becoming the loudest element. Pair it with catppuccin-mocha for dark, so the same snippet can be published in both modes without re-picking colours.

Three tips

Other languages

The same advice, for the languages people paste most.

How Code2Img compares

Feature by feature, against the other code screenshot tools.