Convert grayscale images into NetworkX graph structures
Project description
i2g — Image to Graph Converter
Convert grayscale images into graph structures using NetworkX. Each pixel becomes a node connected to its neighbors (4- or 8-connectivity), with pixel intensity stored as a node attribute.
Installation
pip install i2g
Or install from source in editable mode:
git clone https://github.com/DIM-Corp/i2g.git
cd i2g
pip install -e .[dev]
Requires Python 3.10+. Dependencies: numpy, Pillow, networkx.
Usage
from i2g import ImageGraphConverter
converter = ImageGraphConverter("my_image.png", connectivity="8")
graph, img_array = converter.convert()
shape = converter.shape() # (height, width)
num_nodes, num_edges = converter.info()
print(f"Image shape: {shape}")
print(f"Graph has {num_nodes} nodes and {num_edges} edges.")
Node attributes
Each node is keyed by (row, col) and carries two attributes:
| Attribute | Type | Description |
|---|---|---|
intensity |
int |
Grayscale pixel value (0–255) |
pos |
tuple[int, int] |
(col, -row) — ready for matplotlib/networkx plotting |
Connectivity
| Value | Neighbors |
|---|---|
"4" |
Up, down, left, right |
"8" (default) |
Cardinal + diagonal (8 neighbors) |
Error handling
| Situation | Exception raised |
|---|---|
Invalid connectivity value |
ValueError at construction time |
| Image file not found | FileNotFoundError from convert() |
| File exists but is not a valid image | OSError from convert() |
| Image exceeds 10 million pixels | ValueError from convert() |
shape() or info() called before convert() |
RuntimeError |
Running tests
pytest
License
MIT License — see LICENSE for details.
Contact
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file i2g-0.2.0.tar.gz.
File metadata
- Download URL: i2g-0.2.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b648b5943962066351ce3926030c61ce19744bffcafbca036069110f4b191e29
|
|
| MD5 |
ce556044efa88a12300cc67adfda9cf4
|
|
| BLAKE2b-256 |
e39a23da23c4d9ee99fa38bb27cbe3e59f0e5af7dedc0332fc220d693e616fbf
|
File details
Details for the file i2g-0.2.0-py3-none-any.whl.
File metadata
- Download URL: i2g-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
008cc8eb5cf1780c85f9c7dd0ac24bdea3835b497c09adac205a6e8eaafcd32f
|
|
| MD5 |
1152f61390536824741260ed6e1e0e42
|
|
| BLAKE2b-256 |
0eb81697e5073307996d6e278d6017e2285b23e954a45bb88b6cb53b7b05a599
|