mols2grid-to-image
This tool is a Python-based utility for exporting molecule grids generated by mols2grid as PNG or HTML files. By leveraging mols2grid, it produces clean, well-organized molecular grid images with minimal effort.
The CLI allows you to input a CSV file (containing SMILES or other molecular data) and directly output a grid image. Image rendering and capture are handled using Playwright, enabling the entire workflow to run within Python.
Example Output
Features
- Pure Python: Operates entirely in Python using
playwrightfor headless browser automation. - Flexible Input: Accepts CSV files containing SMILES strings.
- Customizable Output:
- Control grid layout (columns, cell size, border, gap).
- Select specific subsets of data to display.
- Customize fonts and text alignment.
- Pagination: Split large datasets into multiple images automatically.
- Transparent Background: Generate PNGs with transparent backgrounds (including molecule images).
- Output Control: Specify output directory and automatic zero-padded filenames for batch processing.
- Configuration: Support for JSON configuration files for reproducible settings.
Installation
-
Install from PyPI:
pip install mols2grid-to-image
-
Install as a CLI tool via
uv tool install:uv tool install mols2grid-to-image
-
Install as a library via
uv add:uv add mols2grid-to-image
-
Install Playwright browser (required for screenshot functionality):
playwright install chromium
Usage
You can use mols2grid-to-image as a CLI tool or as a library.
CLI (Command Line Interface)
The package provides a m2g-image command.
Basic Usage:
uv run m2g-image input.csv -o output.png
Options:
| Option | Shorthand | Description | Default |
|---|---|---|---|
--version |
-v |
Show version and exit. | |
--output |
-o |
Path to output PNG file (base name). | result.png |
--output-dir |
-od |
Directory to save output images. Overrides --output dir. |
None |
--output-html |
-oh |
Path to save intermediate HTML file (Optional). | None (Not saved) |
--config |
-c |
Path to JSON configuration file. | None |
--smiles-col |
-sc |
Column name for SMILES. | smiles |
--n-cols |
-nc |
Number of columns in grid. | 5 |
--subset |
Columns to display in grid. | None |
|
--cell-width |
-w |
Cell width in pixels. | 150 |
--cell-height |
-ch |
Cell height in pixels. | 150 |
--fontsize |
-fs |
Font size in points. | 12 |
--per-page |
-p |
Number of items per image (pagination). | None (All in one) |
--transparent |
-t |
Enable transparent background for grid and molecules. | False |
--border |
CSS border for cells (e.g., "1px solid black"). | None |
|
--gap |
Gap between cells in pixels. | None |
|
--font-family |
-ff |
Font family for text. | None |
--text-align |
-ta |
Text alignment (left, center, right). | None |
--sort-by |
Column to sort by. | None |
|
--remove-hs |
Remove hydrogens from depiction. | None |
|
--use-coords |
Use existing coordinates from input. | None |
|
--coord-gen |
Use CoordGen for 2D coordinate generation. | None |
Examples:
-
Pagination: Split into images with 50 molecules each.
uv run m2g-image data.csv --per-page 50 -o batch.png # Generates batch_01.png, batch_02.png, ...
-
Transparent Background:
uv run m2g-image data.csv --transparent -o transparent.png
-
Output Directory & Zero Padding:
uv run m2g-image data.csv --per-page 10 --output-dir results/ -o grid.png # Generates results/grid_01.png, results/grid_02.png, ...
Configuration File (JSON)
You can define parameters in a JSON file to avoid long command lines.
config.json:
{
"output_image": "custom_output.png",
"n_cols": 4,
"cell_width": 200,
"cell_height": 200,
"n_items_per_page": 20,
"transparent": true,
"subset": ["ID", "SMILES", "Activity"]
}
Run with config:
uv run m2g-image data.csv -c config.json
Python Library Usage
You can integrate the conversion logic into your own Python scripts.
import pandas as pd
import mols2grid
from m2g_image import grid_to_image, generate_grid_image
# 1. High-level wrapper (Recommended)
generate_grid_image(
pd.read_csv("data.csv"),
output_image_path="output.png",
n_cols=5,
subset=["ID"],
transparent=True,
)
# 2. With pagination
from m2g_image import generate_grid_images
for page_num, path in generate_grid_images(
pd.read_csv("data.csv"),
output_image_path="output.png",
n_items_per_page=50,
n_cols=5,
):
print(f"Page {page_num}: {path}")
# 3. Low-level usage
grid = mols2grid.display(df, ...)
# Note: For transparency, you must handle CSS and MolDrawOptions manually
output_path = grid_to_image(grid, "output.png", omit_background=True)
Development
Run Tests:
uv run pytest
License
Metadata
Release files for mols2grid-to-image 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mols2grid_to_image-0.1.0.tar.gz | 191.8 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mols2grid_to_image-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 204.2 kB
Release files / mols2grid_to_image-0.1.0.tar.gz
| Download URL | mols2grid_to_image-0.1.0.tar.gz |
|---|---|
| Size | 191.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
690f2d3cf196e268bb1d1a983288c901f480f4344bf54e77a5b6a36e386dec9f
|
|
BLAKE2b-256 checksum How to use checksums |
b1f9eefaf616788ba45d8e34eb5a4437fcafb2dd7a6f5225477c095dcbb0c055
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 12, 2026.
Transparency logRelease files / mols2grid_to_image-0.1.0-py3-none-any.whl
| Download URL | mols2grid_to_image-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eeb4d19efc01382722b92eeae8873f58ba0a59da707306984344d87213dfdd44
|
|
BLAKE2b-256 checksum How to use checksums |
f6307776ebcc259bac6098afefe6fc8370611ba0c5a9f91d72fec9e0930f2b80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 12, 2026.
Transparency logSecurity for mols2grid-to-image 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for mols2grid_to_image-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 12, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.