Skip to main content

Convert simple diagram images into runnable code (matplotlib/graphviz).

Project description

diagram2code

Convert simple flowchart-style diagrams into runnable Python programs.

diagram2code takes a diagram image (rectangular steps + arrows), detects the flow, and generates:

  • a graph representation (graph.json)
  • a runnable Python program (generated_program.py)
  • optional debug visualizations (debug_nodes.png, debug_arrows.png)
  • an optional exportable bundle (--export)

This project is designed for learning, prototyping, and experimentation, not for production-grade diagram parsing. :contentReference[oaicite:1]{index=1}


Table of Contents

  1. Installation
  2. Quick Start
  3. Using Labels
  4. Export Bundle
  5. Generated Files
  6. Examples
  7. Limitations

Installation

Clone the repo and install in editable mode:

git clone https://github.com/Nimil785477/diagram2code.git
cd diagram2code

python -m venv .venv

Activate the environment

# Linux / macOS
source .venv/bin/activate

# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1

Install:

pip install -e .

Basic (no OCR)

pip install diagram2code

With OCR support(optional)

pip install diagram2code[ocr]

You must also install Tesseract OCR on your system:

brew install tesseract
  • Ubuntu/Debian:
sudo apt install tesseract-ocr

Then run:

diagram2code image.png --extract-labels

This matches exactly what your code already does ✔️


(Optional but recommended) Add a runtime hint

You already handle this well, but one tiny UX improvement:

In cli.py, after --extract-labels failure, you could optionally print:

safe_print("Hint: install OCR support with `pip install diagram2code[ocr]` and install Tesseract.")

Generate a labels template (no OCR)

If you want to label nodes manually, generate a template file:

diagram2code path/to/diagram.png --out outputs --labels-template

Quick Start

Run diagram2code on a simple diagram:

diagram2code tests/fixtures/branching.png --out outputs

This will write outputs (see Generated Files)

Inspect the detected graph (print summary)

You can inspect the detected nodes, edges, and labels using --print-graph.

diagram2code tests/fixtures/branching.png --out outputs --print-graph.

This will:

  • run the full detection pipeline
  • write all normal output files
  • print a human-readable graph summary to the console

Example Output:

Graph summary
Labels source: none
Nodes: 4
  - id=0 bbox=(40, 40, 76, 76) label=''
Edges: 4
  - 0 -> 1

Dry-run mode

If you only want to inspect the result without writing any files, use:

diagram2code diagram.png --dry-run --print-graph

In dry-run mode:

  • detection still runs fully
  • no files are written
  • OCR does not write labels.json
  • export bundles are not created

Using Labels

You can provide custom labels for nodes using a JSON file

Example labels.json

{
  "0": "Step_1_Load_Data",
  "1": "Step_2_Train_Model"
}

Run with labels

python -m diagram2code.cli diagram.png --out outputs --labels labels.json

The exported program will then use labeled function names (sanitized into valid Python identifiers).

Label resolution order (important)

When multiple label sources are possible, diagram2code resolves labels in the following priority order:

  1. Explicit labels file
    diagram2code diagram.png --labels labels.json
    
  2. Auto-detect labels.json inside export directory
    diagram2code diagram.png --export export_out
    

If export_out/labels.json exists, it is automatically loaded. 3. OCR extraction

diagram2code diagram.png --extract-labels
  1. Fallback
    • If none of the above are provided, nodes have empty label The active source is shown when using --print-graph:
    Labels source: auto (export_out/labels.json)
    

Export Bundle

The --export flag creates a self-contained runnable bundle(easy to share). If labels.json exists inside the export directory, it will be automatically used on subsequent runs.

python -m diagram2code.cli diagram.png --out outputs --export export_bundle

When using --export, the following files are copied:

export_bundle/
├── generated_program.py
├── graph.json
├── labels.json            (if provided)
├── debug_nodes.png        (if exists)
├── debug_arrows.png       (if exists)
├── render_graph.py        (if exists)
├── run.ps1
├── run.sh
└── README_EXPORT.md

Running the exported bundle

Windows (PowerShell):

cd export_bundle
.\run.ps1

Linux/macOS:

cd export_bundle
bash run.sh

or directly:

python generated_program.py

Generated Files

After a normal run (--out outputs):

File Description
preprocessed.png Binary image used for detection
debug_nodes.png Detected rectangles overlay
debug_arrows.png Detected arrows overlay (if enabled)
graph.json Graph structure (nodes + edges)
render_graph.py Script to visualize the graph
generated_program.py Generated executable Python program

Examples

Simple linear flow

[ A ] → [ B ] → [ C ]

Branching flow

      → [ B ]
[ A ]
      → [ C ]

OCR (Optional)

diagram2code can extract text labels using Tesseract OCR.

Requirements:

  • System: tesseract-ocr
  • Python: pytesseract

If OCR is unavailable, the pipeline still works and labels default to empty.

Limitations

  • Only rectangular nodes are supported
  • Arrow detection is heuristic-based
  • Complex curves, diagonals, or overlapping arrows may fail
  • No text extraction from inside shapes
  • Not intended for UML, BPMN, or hand-drawn diagrams

Demo

Convert a simple diagram image into runnable Python code:

diagram2code tests/fixtures/simple.png --out demo_outputs --extract-labels

Project details


Download files

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

Source Distribution

diagram2code-0.1.3.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

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

diagram2code-0.1.3-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file diagram2code-0.1.3.tar.gz.

File metadata

  • Download URL: diagram2code-0.1.3.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for diagram2code-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3cd5290d3fae32aba5e1ed62fd0ece21db05c6dc0934ee9ea60f40945b9d1f3a
MD5 9b4767fe8838224ba50a8164ac58702b
BLAKE2b-256 ed9400d54fe52baf4ae84d3f7ba93405bfd569808b9614ced155314c7a630c64

See more details on using hashes here.

File details

Details for the file diagram2code-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: diagram2code-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for diagram2code-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a1619dcf18ffd206c93033c07419b8748226b8641e242668976af43144122dbf
MD5 a36a7a4948baaa05402c8ea79ea7341b
BLAKE2b-256 6df2f9457b18d2b69a0366ecaf7d030d2b7f38fd3431e9cdca08aaa671fc3829

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