A library for converting JSON annotation files to PNG masks
Project description
JSON2PNG Annotation Converter
A Python library for converting JSON annotation files (typically from annotation tools like MakesenseAI, VIA or LabelMe) to PNG mask images, useful for computer vision and machine learning tasks.
Features
- Convert JSON annotation files to binary PNG masks
- Process multiple JSON files in a directory
- Customizable mask dimensions
- Filter JSON files by filename pattern
- Command-line interface for easy integration into pipelines
- Programmatic API for integration into Python projects
Installation
From PyPI (recommended)
pip install json2png-annotation
From Source
git clone https://github.com/nguyentran4896/json2png-annotation.git
cd json2png-annotation
pip install -e .
Usage
Command-line Interface
# Basic usage
json2png -i /path/to/json/files -o /path/to/output/masks
# Custom dimensions
json2png -i /path/to/json/files -o /path/to/output/masks -w 1024 -t 768
# Filter JSON files by pattern
json2png -i /path/to/json/files -o /path/to/output/masks -p "car_"
Python API
from json2png_annotation import convert_annotations
# Convert all JSON files in a directory
output_files = convert_annotations(
input_folder="/path/to/json/files",
output_folder="/path/to/output/masks",
width=800,
height=800
)
print(f"Generated {len(output_files)} PNG files")
# With filename pattern filtering
output_files = convert_annotations(
input_folder="/path/to/json/files",
output_folder="/path/to/output/masks",
filename_pattern="car_"
)
# Process a single JSON object
from json2png_annotation.converter import convert_single_annotation
with open("annotation.json", "r") as f:
json_data = json.load(f)
output_path = convert_single_annotation(
json_data,
output_path="output_mask.png",
width=800,
height=800
)
Input Format
The library expects JSON annotation files in the following format:
{
"image_name.jpg": {
"regions": {
"0": {
"shape_attributes": {
"all_points_x": [100, 200, 300, 100],
"all_points_y": [100, 100, 200, 200]
}
},
"1": {
"shape_attributes": {
"all_points_x": [400, 500, 500, 400],
"all_points_y": [400, 400, 500, 500]
}
}
}
}
}
Output
The output is a PNG image with black (0) background and white (255) polygon regions based on the coordinates in the JSON file.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
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 json2png_annotation-0.1.0.tar.gz.
File metadata
- Download URL: json2png_annotation-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89bce8338611b2be999efabd44c27c838317bbd70d06e7d55fa8517a930394d1
|
|
| MD5 |
063d7d7f7e7055995edb1f321cd5789b
|
|
| BLAKE2b-256 |
9a07ef43b9d0cf40d2dcdbb81777f7a4c751aceb388c4968c6305f85846fdb13
|
File details
Details for the file json2png_annotation-0.1.0-py3-none-any.whl.
File metadata
- Download URL: json2png_annotation-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f594d5d63a2555679932d357c35778f0977ee025c4ade376f707541ae1ef097
|
|
| MD5 |
0a4eeb26acb31befd68254df582bb776
|
|
| BLAKE2b-256 |
ebe96242a2b9a7ac62d667a87a3b7ed0a41899114db6809e66b254dd50b3752d
|