Skip to main content

Convert scanned P&ID images to DXF/DWG

Project description

jpg-dwg

Convert scanned P&ID (Piping and Instrumentation) drawing images to DXF (lines only), and optionally to DWG via an external converter.

Install

From PyPI (recommended):

pip install jpg-dwg

From source:

git clone https://github.com/Nisanth-sr/image-dwg.git
cd jpg-dwg
pip install -e .

Or install dependencies only and run as module:

pip install -r requirements.txt
python -m jpg_dwg input.png -o output.dxf

Usage

# Basic: image → DXF (after pip install jpg-dwg)
jpg-dwg scan.png -o drawing.dxf

# Or as module
python -m jpg_dwg scan.png -o drawing.dxf

# With options (invert is on by default for black-on-white P&ID; use --no-invert if needed)
python -m jpg_dwg scan.png -o drawing.dxf --preview preprocessed.png --scale 0.1

# Enable P&ID symbol detection (contour-based heuristics)
python -m jpg_dwg scan.png -o drawing.dxf --symbols

# Also produce DWG (requires ODA File Converter)
python -m jpg_dwg scan.png -o drawing.dxf --dwg drawing.dwg

Options

Option Description
input Input image path (PNG, JPG, etc.)
-o, --output Output DXF path
--scale Drawing units per pixel (default 1.0)
--dpi DPI hint (default 300)
--no-deskew Disable automatic deskew
--invert Invert binary (default: on for black lines on white)
--no-invert Do not invert (for white lines on dark scan)
--adaptive Use adaptive threshold instead of Otsu
--no-denoise Disable morphology denoise
--thin Thinning/skeletonization (requires opencv-contrib)
--preview PATH Save preprocessed binary image
--symbols Run P&ID symbol detection and insert blocks
--dwg PATH Also convert DXF to DWG (see below)
-v, --verbose Verbose logging

DWG output

This tool writes DXF only. To get DWG:

  1. Install ODA File Converter.
  2. Set ODA_FILE_CONVERTER to the converter executable path, or ensure it is on PATH.
  3. Use --dwg output.dwg. The CLI will run the converter after writing the DXF.

Limitations

  • Quality depends on scan resolution and contrast. Preprocess with --invert or --preview to tune.
  • Vectorization extracts lines only (OpenCV Hough).
  • P&ID symbol detection is heuristic (contour-based). For production, consider a trained model (e.g. YOLO on P&ID symbols).

Publishing to PyPI (maintainers)

To avoid distributing source code, build and upload only a bytecode-only wheel (no .py files, no sdist).

  1. Create an account and API token at pypi.org (use test.pypi.org for testing).
  2. From the repo root:
    pip install build twine
    python scripts/build_bytecode_wheel.py
    twine upload dist/*.whl
    
    • build_bytecode_wheel.py builds a wheel, then repacks it so the package contains only .pyc (no .py source). The resulting wheel in dist/ is suitable for upload.
    • Upload only the wheel (dist/*.whl); do not run python -m build and do not upload dist/*.tar.gz (sdist), so source is not published.
  3. When prompted: username __token__, password = your PyPI API token.
  4. To test first: twine upload --repository testpypi dist/*.whl, then pip install -i https://test.pypi.org/simple/ jpg-dwg.

Note: The bytecode-only wheel is built for the Python version that runs the script (e.g. 3.11). To support multiple versions, run scripts/build_bytecode_wheel.py under each desired Python (e.g. 3.9, 3.10, 3.11, 3.12) and upload all resulting wheels.

Update version in pyproject.toml and the [project.urls] Homepage/Source to your repo before publishing.

Project layout

jpg-dwg/
├── pyproject.toml
├── requirements.txt
├── README.md
├── scripts/
│   └── build_bytecode_wheel.py   # Build wheel with only .pyc (no source)
├── src/jpg_dwg/
│   ├── cli.py
│   ├── preprocess.py
│   ├── vectorize.py
│   ├── export_dxf.py
│   ├── symbols.py
│   └── config.py
└── tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jpg_dwg-0.1.2-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file jpg_dwg-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: jpg_dwg-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for jpg_dwg-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b2bb82f1f8dcfe926a167a0e3c57c0adf6cbcb1946ce7e8677b2c9309d4b907d
MD5 7d66a4053754ed4e2fb61e26514a8f54
BLAKE2b-256 3974777c1c78fd62eefab9c5d36115d02fbefb507c7cd8f798f688877dd5eb63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page