# nisanth-sr/image-dwg architecture

> Architecture diagram of the GitHub repository nisanth-sr/image-dwg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nisanth-sr/image-dwg
- Repository: https://github.com/nisanth-sr/image-dwg

## Overview

#### 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: [`src/jpg_dwg/cli.py`](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/cli.py) (CLI orchestration), [`src/jpg_dwg/preprocess.py`](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/preprocess.py) (Raster preprocessing), [`src/jpg_dwg/vectorize.py`](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/vectorize.py) (Line vectorization), [`src/jpg_dwg/export_dxf.py`](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/export_dxf.py) (DXF export)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["CLI Runtime"]
  node_package_entry["Package entry<br/>Python module<br/>[__main__.py]"]
  node_cli["CLI orchestration<br/>Python CLI<br/>[cli.py]"]
  node_config["Processing options<br/>configuration<br/>[config.py]"]
  node_input_image["Scanned P&amp;ID image<br/>raster input"]
end

subgraph group_pipeline["Image Pipeline"]
  node_preprocess["Raster preprocessing<br/>OpenCV processing<br/>[preprocess.py]"]
  node_binary_preview["Binary preview<br/>tuning artifact"]
  node_binary_image["Clean binary image<br/>image intermediate"]
  node_vectorize["Line vectorization<br/>OpenCV geometry<br/>[vectorize.py]"]
  node_line_geometry["Line geometry<br/>drawing geometry"]
  node_symbols["Symbol heuristics<br/>contour detection<br/>[symbols.py]"]
  node_symbol_blocks["Symbol blocks<br/>block content<br/>[.gitkeep]"]
end

subgraph group_output["Output"]
  node_dxf_export["DXF export<br/>DXF writer<br/>[export_dxf.py]"]
  node_dxf_file["DXF drawing<br/>output artifact"]
  node_oda_converter{{"ODA File Converter<br/>external executable"}}
  node_dwg_file["DWG drawing<br/>optional output"]
end

subgraph group_distribution["Distribution"]
  node_project_metadata["Project metadata<br/>packaging metadata<br/>[pyproject.toml]"]
  node_dependencies["Image dependencies<br/>dependency manifest<br/>[requirements.txt]"]
  node_bytecode_wheel["Bytecode wheel build<br/>distribution script"]
  node_wheel_entry_point["Wheel entry point<br/>package metadata<br/>[entry_points.txt]"]
end

node_package_entry -->|"invokes"| node_cli
node_input_image -->|"path input"| node_cli
node_cli -->|"applies"| node_config
node_cli -->|"runs"| node_preprocess
node_config -->|"controls"| node_preprocess
node_preprocess -->|"produces"| node_binary_image
node_preprocess -.->|"optionally emits"| node_binary_preview
node_cli -->|"runs"| node_vectorize
node_binary_image -->|"consumes"| node_vectorize
node_config -->|"scale and DPI"| node_vectorize
node_vectorize -->|"extracts"| node_line_geometry
node_cli -.->|"optionally runs"| node_symbols
node_binary_image -.->|"analyzes"| node_symbols
node_symbols -.->|"detects"| node_symbol_blocks
node_cli -->|"coordinates"| node_dxf_export
node_line_geometry -->|"writes"| node_dxf_export
node_symbol_blocks -.->|"optionally includes"| node_dxf_export
node_dxf_export -->|"exports"| node_dxf_file
node_cli -.->|"optionally invokes"| node_oda_converter
node_dxf_file -.->|"converts"| node_oda_converter
node_oda_converter -.->|"produces"| node_dwg_file
node_project_metadata -->|"packages"| node_package_entry
node_dependencies -->|"provides OpenCV"| node_preprocess
node_dependencies -->|"provides OpenCV"| node_vectorize
node_bytecode_wheel -->|"repackages wheel"| node_project_metadata
node_project_metadata -->|"defines"| node_wheel_entry_point

click node_package_entry "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/__main__.py"
click node_cli "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/cli.py"
click node_config "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/config.py"
click node_preprocess "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/preprocess.py"
click node_vectorize "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/vectorize.py"
click node_symbols "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/symbols.py"
click node_symbol_blocks "https://github.com/nisanth-sr/image-dwg/blob/main/blocks/.gitkeep"
click node_dxf_export "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/export_dxf.py"
click node_project_metadata "https://github.com/nisanth-sr/image-dwg/blob/main/pyproject.toml"
click node_dependencies "https://github.com/nisanth-sr/image-dwg/blob/main/requirements.txt"
click node_bytecode_wheel "https://github.com/nisanth-sr/image-dwg/blob/main/scripts/build_bytecode_wheel.py"
click node_wheel_entry_point "https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg.egg-info/entry_points.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package_entry,node_cli,node_config,node_input_image toneBlue
class node_preprocess,node_binary_preview,node_binary_image,node_vectorize,node_line_geometry,node_symbols,node_symbol_blocks toneAmber
class node_dxf_export,node_dxf_file,node_oda_converter,node_dwg_file toneMint
class node_project_metadata,node_dependencies,node_bytecode_wheel,node_wheel_entry_point toneRose
```

## Components

### CLI Runtime

- [Package entry](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/__main__.py): Python module · Supports python -m jpg_dwg invocation. · `src/jpg_dwg/__main__.py`
- [CLI orchestration](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/cli.py): Python CLI · Coordinates options, stages, and output files. · `src/jpg_dwg/cli.py`
- [Processing options](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/config.py): configuration · Shared processing and scale options. · `src/jpg_dwg/config.py`
- **Scanned P&ID image**: raster input

### Image Pipeline

- [Raster preprocessing](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/preprocess.py): OpenCV processing · Normalizes, thresholds, deskews, denoises, and optionally thins scans. · `src/jpg_dwg/preprocess.py`
- **Binary preview**: tuning artifact · Optional cleaned-image preview.
- **Clean binary image**: image intermediate
- [Line vectorization](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/vectorize.py): OpenCV geometry · Uses Hough-style detection for straight lines. · `src/jpg_dwg/vectorize.py`
- **Line geometry**: drawing geometry · Pixel geometry scaled into drawing units.
- [Symbol heuristics](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/symbols.py): contour detection · Optional approximate P&ID symbol detection. · `src/jpg_dwg/symbols.py`
- [Symbol blocks](https://github.com/nisanth-sr/image-dwg/blob/main/blocks/.gitkeep): block content · Optional block-oriented symbol output. · `blocks/.gitkeep`

### Output

- [DXF export](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg/export_dxf.py): DXF writer · Writes the native drawing artifact. · `src/jpg_dwg/export_dxf.py`
- **DXF drawing**: output artifact
- **ODA File Converter**: external executable · Resolved from ODA_FILE_CONVERTER or system PATH.
- **DWG drawing**: optional output

### Distribution

- [Project metadata](https://github.com/nisanth-sr/image-dwg/blob/main/pyproject.toml): packaging metadata · Defines package and console entry point. · `pyproject.toml`
- [Image dependencies](https://github.com/nisanth-sr/image-dwg/blob/main/requirements.txt): dependency manifest · Declares OpenCV and related runtime packages. · `requirements.txt`
- [Bytecode wheel build](https://github.com/nisanth-sr/image-dwg/blob/main/scripts/build_bytecode_wheel.py): distribution script · Repacks a wheel with bytecode-only package contents. · `scripts/build_bytecode_wheel.py`
- [Wheel entry point](https://github.com/nisanth-sr/image-dwg/blob/main/src/jpg_dwg.egg-info/entry_points.txt): package metadata · `src/jpg_dwg.egg-info/entry_points.txt`

## Connections

- 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
