Skip to main content

Convert molecules to grid images using mols2grid and Playwright

Project description

mols2grid-to-image

PyPI version Python License: MIT Test Lint

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

Example Output

Features

  • Pure Python: Operates entirely in Python using playwright for 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

  1. Install from PyPI:

    pip install mols2grid-to-image
    
  2. Install as a CLI tool via uv tool install:

    uv tool install mols2grid-to-image
    
  3. Install as a library via uv add:

    uv add mols2grid-to-image
    
  4. 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:

  1. 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, ...
    
  2. Transparent Background:

    uv run m2g-image data.csv --transparent -o transparent.png
    
  3. 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

MIT

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

mols2grid_to_image-0.1.0.tar.gz (191.8 kB view details)

Uploaded Source

Built Distribution

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

mols2grid_to_image-0.1.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file mols2grid_to_image-0.1.0.tar.gz.

File metadata

  • Download URL: mols2grid_to_image-0.1.0.tar.gz
  • Upload date:
  • Size: 191.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mols2grid_to_image-0.1.0.tar.gz
Algorithm Hash digest
SHA256 690f2d3cf196e268bb1d1a983288c901f480f4344bf54e77a5b6a36e386dec9f
MD5 0141604af0027fc2e65dcd65842b224d
BLAKE2b-256 b1f9eefaf616788ba45d8e34eb5a4437fcafb2dd7a6f5225477c095dcbb0c055

See more details on using hashes here.

Provenance

The following attestation bundles were made for mols2grid_to_image-0.1.0.tar.gz:

Publisher: publish.yml on N283T/mols2grid_to_image

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mols2grid_to_image-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mols2grid_to_image-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eeb4d19efc01382722b92eeae8873f58ba0a59da707306984344d87213dfdd44
MD5 2aea6de298d876014b4d039b1ee3ddea
BLAKE2b-256 f6307776ebcc259bac6098afefe6fc8370611ba0c5a9f91d72fec9e0930f2b80

See more details on using hashes here.

Provenance

The following attestation bundles were made for mols2grid_to_image-0.1.0-py3-none-any.whl:

Publisher: publish.yml on N283T/mols2grid_to_image

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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