Blog · DXF · 1 Oct 2026 · 3 min read
How to render a DXF to PNG so an AI agent can see the drawing
An AI agent that only reads numbers will approve a wrong drawing. Rendering the DXF to an image lets it look. The ezdxf and PyMuPDF recipe that works on large files, the filter that broke it for a month, the pixel limit, and the renderers that do not draw dimensions.
Short answer: open the file with ezdxf.recover.readfile, draw the modelspace with ezdxf's drawing add-on to a PDF using PyMuPdfBackend, then rasterise the PDF with PyMuPDF (fitz) at the zoom you need. Do not pass a filter_func. Render the PDF once and crop views from it.
Why the agent must see the drawing
A check script once printed RESULT: ALL OK on a drawing that a person rejected by opening it, in three seconds. Since then, our rule: no DXF is declared done, delivered or shown without being looked at as an image that contains the defect being judged.
The recipe
from ezdxf import recover
from ezdxf.addons.drawing import Frontend, RenderContext, config, layout
from ezdxf.addons.drawing.pymupdf import PyMuPdfBackend
import fitz # PyMuPDF
doc, auditor = recover.readfile(path) # recover: repairs in memory
cfg = config.Configuration(
proxy_graphic_policy=config.ProxyGraphicPolicy.IGNORE,
hatch_policy=config.HatchPolicy.SHOW_OUTLINE,
background_policy=config.BackgroundPolicy.WHITE,
color_policy=config.ColorPolicy.COLOR,
)
backend = PyMuPdfBackend()
Frontend(RenderContext(doc), backend, config=cfg).draw_layout(doc.modelspace())
# no filter_func: it breaks the INSERTs inside the *D dimension blocks
open("sheet.pdf", "wb").write(backend.get_pdf_bytes(layout.Page(0, 0)))
page = fitz.open("sheet.pdf")[0]
zoom = 2600 / page.rect.width
page.get_pixmap(matrix=fitz.Matrix(zoom, zoom), alpha=False).save("sheet.png")
On a 109,830 × 25,540 mm delivery file: load 13 s, frontend 8 s, PDF 128 s, 5.4 MB.
The month lost to a filter
For a month the note said "ezdxf cannot render this file: it blows up on missing blocks". Measured: recover.readfile reported 0 errors and 0 undefined blocks, and ezdxf drew every dimension. The test had been run with a filter_func, which broke the *D blocks of the dimensions. The filter was guilty; ezdxf got the blame.
Lesson: when an attempt has an accessory, try it bare first.
The pixel limit
At 100 dpi, the same file raises FzErrorLimit: code=5: Overly large image. It is not a DXF problem, it is pixels. Render to PDF (no pixel limit), then rasterise only the zoom or crop you need. A crop can hit the limit too.
API notes:
- The extent is
backend.player().bbox().backend.bbox()does not exist. get_pixmap_bytes()wants alayout.Pageas first argument.ezdxf.bbox.extents(fast=True)can return-2e18. Crop to the zone you are judging, not the whole file.
Settings that change what you see
| Setting | Why |
|---|---|
| Black background | ACI colour 7 (white/black) disappears on white in some renderers |
line_policy=LinePolicy.ACCURATE |
Keeps dashed lines. A PNG that ignores dashes makes you fix defects that do not exist |
lineweight_scaling, min_lineweight |
Thin lines vanish at small sizes. We use 1.3 / 0.25 on white |
Renderers that do not draw dimensions
A fast PIL-based viewer we used rendered 13,473 entities in 0.7 s, against more than 120 s with ezdxf and Matplotlib. It also drew no dimensions: chamfer note blocks gave 0 strokes, dimension texts were missing. Fine for checking outlines; blind for dimensions. Know which defects your renderer can show before trusting it.
For dimension placement and overlaps we judge an Inventor PDF directly with fitz (note: in PDF the Y origin is at the top).
Do and don't
Do
- Use
recover.readfile. - Render to PDF once, rasterise crops with
fitz. - Choose a renderer that shows the defect you are judging.
- Look at the image before saying "done".
Don't
- Don't pass a
filter_func. - Don't rasterise a huge sheet at high dpi in one go.
- Don't blame the library before testing without your extras.
Questions people ask
Can Claude or another AI model look at the PNG? Yes. An agent like Claude Code reads images. It renders, opens the PNG and describes what it sees, which is the step that catches what counts miss.
Is a render enough as a check? No, it is one of two. Numbers read from the file plus an image. See how to check AI-generated CAD output.