How nisanth-sr/image-dwg works
Purpose and execution surface
jpg-dwg is a Python CLI pipeline that converts scanned P&ID raster images into line-only DXF drawings, with optional heuristic symbol blocks and optional post-export DWG conversion. Package invocation is supported through src/jpg_dwg/__main__.py; command orchestration resides in src/jpg_dwg/cli.py. Packaging and console-entry-point metadata are in pyproject.toml.
Pipeline orchestration
The CLI accepts an image path and output DXF path, applies selected processing options, and coordinates each stage: preprocessing, vectorization, optional symbol detection, DXF generation, then optional DWG conversion. It is the main control boundary between user-facing file/flag inputs and internal image/geometry processing: src/jpg_dwg/cli.py, src/jpg_dwg/__main__.py, src/jpg_dwg/config.py.
Image preprocessing
Raster normalization prepares scans for line extraction. This stage covers configurable inversion, thresholding (Otsu or adaptive), deskew, denoising morphology, and optional thinning/skeletonization; it can also emit a binary preview for tuning. Its output is the cleaned binary image consumed by vectorization: src/jpg_dwg/preprocess.py, src/jpg_dwg/config.py.
Raster-to-vector conversion
OpenCV-based vectorization extracts straight line geometry from the preprocessed image, using Hough-style line detection rather than general curves or text recognition. Scale and DPI options establish the pixel-to-drawing-unit boundary before geometry is written: src/jpg_dwg/vectorize.py, src/jpg_dwg/preprocess.py, src/jpg_dwg/config.py.
Optional P&ID symbol stage
When enabled, a separate contour-heuristic path detects likely P&ID symbols and supplies block-oriented content alongside extracted lines. This is intentionally approximate and independent of the core line-vectorization path; it does not use a trained detection service or model: src/jpg_dwg/symbols.py, blocks/.gitkeep, src/jpg_dwg/cli.py.
DXF and DWG output boundary
DXF is the tool’s native artifact and is written from extracted geometry, optionally including detected symbol blocks. DWG is not authored directly: after DXF completion, the CLI invokes the externally installed ODA File Converter, resolved through ODA_FILE_CONVERTER or system PATH. Output ownership is split between internal DXF export and the external converter: src/jpg_dwg/export_dxf.py, src/jpg_dwg/cli.py.
Runtime and distribution
The application runs as a Python package with image-processing dependencies declared in requirements.txt and project metadata in pyproject.toml. OpenCV is architecturally material; contrib OpenCV is needed for thinning. The repository also supports a source-hiding distribution path that repacks a wheel with bytecode-only package contents: scripts/build_bytecode_wheel.py, pyproject.toml, src/jpg_dwg.egg-info/entry_points.txt.
Main entry points: cli.py CLI orchestration · preprocess.py Raster preprocessing · vectorize.py Line vectorization · export_dxf.py DXF export
Components 19
CLI Runtime
- Package entry · Supports python -m jpg_dwg invocation.
src/jpg_dwg/__main__.py - CLI orchestration · Coordinates options, stages, and output files.
src/jpg_dwg/cli.py - Processing options · Shared processing and scale options.
src/jpg_dwg/config.py - Scanned P&ID image
Image Pipeline
- Raster preprocessing · Normalizes, thresholds, deskews, denoises, and optionally thins scans.
src/jpg_dwg/preprocess.py - Binary preview · Optional cleaned-image preview.
- Clean binary image
- Line vectorization · Uses Hough-style detection for straight lines.
src/jpg_dwg/vectorize.py - Line geometry · Pixel geometry scaled into drawing units.
- Symbol heuristics · Optional approximate P&ID symbol detection.
src/jpg_dwg/symbols.py - Symbol blocks · Optional block-oriented symbol output.
blocks/.gitkeep
Output
- DXF export · Writes the native drawing artifact.
src/jpg_dwg/export_dxf.py - DXF drawing
- ODA File Converter · Resolved from ODA_FILE_CONVERTER or system PATH.
- DWG drawing
Distribution
- Project metadata · Defines package and console entry point.
pyproject.toml - Image dependencies · Declares OpenCV and related runtime packages.
requirements.txt - Bytecode wheel build · Repacks a wheel with bytecode-only package contents.
scripts/build_bytecode_wheel.py - Wheel entry point
src/jpg_dwg.egg-info/entry_points.txt
Connections 26
- Package entry → CLI orchestration: invokes
- Scanned P&ID image → CLI orchestration: path input
- CLI orchestration → Processing options: applies
- CLI orchestration → Raster preprocessing: runs
- Processing options → Raster preprocessing: controls
- Raster preprocessing → Clean binary image: produces
- Raster preprocessing → Binary preview: optionally emits
- CLI orchestration → Line vectorization: runs
- Clean binary image → Line vectorization: consumes
- Processing options → Line vectorization: scale and DPI
- Line vectorization → Line geometry: extracts
- CLI orchestration → Symbol heuristics: optionally runs
- Clean binary image → Symbol heuristics: analyzes
- Symbol heuristics → Symbol blocks: detects
- CLI orchestration → DXF export: coordinates
- Line geometry → DXF export: writes
- Symbol blocks → DXF export: optionally includes
- DXF export → DXF drawing: exports
- CLI orchestration → ODA File Converter: optionally invokes
- DXF drawing → ODA File Converter: converts
- ODA File Converter → DWG drawing: produces
- Project metadata → Package entry: packages
- Image dependencies → Raster preprocessing: provides OpenCV
- Image dependencies → Line vectorization: provides OpenCV
- Bytecode wheel build → Project metadata: repackages wheel
- Project metadata → Wheel entry point: defines