Draw 2d molecule representation easily. Change colors, increase size, make a background cover
Project description
Chemimg
Overview
A very light package that makes Rdkit's molecule drawing simple for quick usage. It comes with 4 main core attributes.
- With one line of code create a 2D representation of a molecule from its SMILES representation.
- Change the color, size and thickness of the strokes of your molecule.
- Create a background cover (collage) of a group of molecules by randomly placing and rotating your 2D images.
- Do all these 3 easily and also for only the molecule scaffolds.
Installation
This project relies on RDKit and Cairo, which are best installed via conda.
With conda
conda env create -f environment.yml
conda activate chemimg310
pip install .
Other ways
Cairo must be installed separately.
System Requirements (Required for CairoSVG)
- Windows: Install the GTK Runtime.
- macOS:
brew install cairo - Linux:
sudo apt-get install libcairo2
pip install git+https://github.com/bsaldivaremc2/chemimg.git
Install from PyPI
pip install chemimg
Usage
Check demo.ipynb for examples.
Load the package
import os
import chemimg
Generate one example of the complete molecule or only the scaffold
border_width: thicker strokes
increase_factor: default 1, makes the image bigger proportionately
scaffold_only: only plot the molecule scaffold
ismiles = "CC(C)Cc1ccc(cc1)C(C)C(=O)O"
fo1 = "imgs/demo.png"
scaffold_only = False
chemimg.chem.scaffolds.draw_transparent_mol(ismiles, fo1,increase_factor=100,scaffold_only=scaffold_only,border_width=1)
fo2 = "imgs/demo_scaffold.png"
scaffold_only = True
chemimg.chem.scaffolds.draw_transparent_mol(ismiles, fo2,increase_factor=100,scaffold_only=scaffold_only,border_width=1)
image_paths = [fo1, fo2]
chemimg.imgproc.demo.show_images_grid(
image_paths,
input_ratio=(1, 2),
figsize_factor=3.0
)
ismiles = "CC(C)Cc1ccc(cc1)C(C)C(=O)O"
border_widths = [1, 3, 5] # N rows
scales = [50, 100, 150] # M cols
output_dir = "imgs"
# Molecule grid
prefix = "mol"
mol_grid = chemimg.imgproc.demo.generate_grid(ismiles, output_dir, border_widths, scales, scaffold_only=False, prefix=prefix)
#mol_grid.show() #Open the image in the default image viewer
fo1=f"imgs/{prefix}_grid.png"
image_paths = [fo1]
chemimg.imgproc.demo.show_images_grid(
image_paths,
input_ratio=(1, 1),
figsize_factor=6.0
)
# Scaffold grid
prefix = "scaffold"
scaffold_grid = chemimg.imgproc.demo.generate_grid(ismiles, output_dir, border_widths, scales, scaffold_only=True, prefix=prefix)
#scaffold_grid.show() #Open the image in the default image viewer
fo2=f"imgs/{prefix}_grid.png"
image_paths = [fo2]
chemimg.imgproc.demo.show_images_grid(
image_paths,
input_ratio=(1, 1),
figsize_factor=3.0
)
Change colors
fname = "mol_bw5_scale150.png"
input_path=f"imgs/{fname}"
new_fname = fname.replace(".png","_blue.png")
output_path=f"imgs/{new_fname}"
chemimg.imgproc.colors.change_color_to_color_fast(input_path, output_path,
original_color=(0,0,0), replacement_color=(0,0,255),any_color=False)
nf1 = output_path
new_fname = fname.replace(".png","_allblue.png")
output_path=f"imgs/{new_fname}"
chemimg.imgproc.colors.change_color_to_color_fast(input_path, output_path,
original_color=(0,0,0), replacement_color=(0,0,255),any_color=True)
nf2 = output_path
image_paths = [nf1,nf2]
chemimg.imgproc.demo.show_images_grid(
image_paths,
input_ratio=(1, 2),
figsize_factor=5.0
)
Make a collage/background cover
for the listed 20 molecules create their 2d representations as:
- Normal molecules\
- Scaffolds only\
- Normal molecules but blue\
- Scaffolds only but red
# A collection of 20 diverse SMILES strings
smiles_list = [
"CC(=O)Oc1ccccc1C(=O)O", # Aspirin
"CC(=O)Nc1ccc(O)cc1", # Paracetamol
"CN1C=NC2=C1C(=O)N(C(=O)N2C)C", # Caffeine
"CN(C)C(=N)N=C(N)N", # Metformin
"CC1(C(N2C(S1)C(C2=O)NC(=O)Cc3ccccc3)C(=O)O)C", # Penicillin G
"CCO", # Ethanol
"CC(=O)O", # Acetic Acid
"CC(=O)C", # Acetone
"c1ccccc1", # Benzene
"C(C1C(C(C(C(O1)O)O)O)O)O", # Glucose
"C1=CC(=C(C=C1CCN)O)O", # Dopamine
"C1=CC2=C(C=C1O)C(=CN2)CCN", # Serotonin
"CNC[C@H](C1=CC(=C(C=C1)O)O)O", # Adrenaline
"C(C(=O)O)N", # Glycine
"C1=NC(=C2C(=N1)N(C=N2)C3C(C(C(O3)COP(=O)(O)OP(=O)(O)OP(=O)(O)O)O)O)N", # ATP
"COc1cc(C=O)ccc1O", # Vanillin
"CC1=CCC(CC1)C(=C)C", # Limonene
"C1=CC=C(C=C1)C=CC=O", # Cinnamaldehyde
"CC(C)/C=C/CCCCC(=O)NCC1=CC(=C(C=C1)O)OC", # Capsaicin
"CC1CCC(C(C1)O)C(C)C" # Menthol
]
odir="imgs/imgs4collage/"
for i,ismiles in enumerate(smiles_list):
ofname = os.path.join(odir,f"mol_{i:03d}.png")
chemimg.chem.scaffolds.draw_transparent_mol(ismiles, ofname,increase_factor=10,scaffold_only=False,border_width=5,verbose=False)
odir="imgs/imgs4collageScaffold/"
for i,ismiles in enumerate(smiles_list):
ofname = os.path.join(odir,f"mol_{i:03d}.png")
chemimg.chem.scaffolds.draw_transparent_mol(ismiles, ofname,increase_factor=10,scaffold_only=True,border_width=5,verbose=False)
# Change colors for collage, to blue for the full molecules, and red for the scaffolds. The any_color=True option will change all non-white pixels to the replacement color, which is useful for images with anti-aliasing or slight variations in color.
input_path="imgs/imgs4collage/"
output_path="imgs/imgs4collageBlue/"
chemimg.imgproc.colors.change_color_to_color_fast(input_path, output_path,
original_color=(0,0,0), replacement_color=(0,0,255),any_color=True)
input_path="imgs/imgs4collageScaffold/"
output_path="imgs/imgs4collageScaffoldRed/"
chemimg.imgproc.colors.change_color_to_color_fast(input_path, output_path,
original_color=(0,0,0), replacement_color=(255,0,0),any_color=True)
Create 4 collage images
- Normal molecules\
- Scaffolds only\
- Normal molecules but blue\
- Scaffolds only but red :::
folder_paths = ["imgs/imgs4collage/", "imgs/imgs4collageScaffold/", "imgs/imgs4collageBlue/", "imgs/imgs4collageScaffoldRed/"]
output_files = ["imgs/collage_simple.png", "imgs/collage_scaffold.png", "imgs/collage_simple_blue.png", "imgs/collage_scaffold_red.png"]
for folder_path, output_file in zip(folder_paths, output_files):
chemimg.imgproc.collage.create_collage_randomNoCollapse(output_size=(1024, 1024), folder_path=folder_path,
max_time_seconds=10, max_images=1000,
output_file=output_file, min_scale_factor=-1, max_scale_factor=-1,
lower_alpha=0.5, upper_alpha=1.0,rotate_only_if_vertical=False)
Visualize all together
c1="imgs/collage_simple.png"
c2="imgs/collage_scaffold.png"
c3="imgs/collage_simple_blue.png"
c4="imgs/collage_scaffold_red.png"
chemimg.imgproc.demo.show_images_grid(
[c1,c2,c3,c4],
input_ratio=(2, 2),
figsize_factor=5.0
)
License
This project is licensed under the Creative Commons Attribution–NonCommercial 4.0 International License.
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 chemimg-2026.2.8.tar.gz.
File metadata
- Download URL: chemimg-2026.2.8.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9758507c5339634e0eeec9972fd106d6ef40ea7f5b1397c3b495a0bded9c041
|
|
| MD5 |
dcafb9be8fddc2aa099a0091cbd26f66
|
|
| BLAKE2b-256 |
19634a3c465aa5a25b35ef6ec1dc49f7f679cc32b148ce5e473b4d5959b943c8
|
Provenance
The following attestation bundles were made for chemimg-2026.2.8.tar.gz:
Publisher:
python-package.yml on bsaldivaremc2/chemimg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chemimg-2026.2.8.tar.gz -
Subject digest:
e9758507c5339634e0eeec9972fd106d6ef40ea7f5b1397c3b495a0bded9c041 - Sigstore transparency entry: 937082882
- Sigstore integration time:
-
Permalink:
bsaldivaremc2/chemimg@b68bfe457ba922d6ee014bec7c1e30993098ffe9 -
Branch / Tag:
refs/tags/v2026.02.10.1300 - Owner: https://github.com/bsaldivaremc2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@b68bfe457ba922d6ee014bec7c1e30993098ffe9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file chemimg-2026.2.8-py3-none-any.whl.
File metadata
- Download URL: chemimg-2026.2.8-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f2e6a9a95d5186a6f8b1299a4bb2bb42eeb87bfd223a9658b8924d1882d5337
|
|
| MD5 |
70ebb54bedbc2a673391e42d2451e776
|
|
| BLAKE2b-256 |
f1c149aae3b8ecc4cd29b4d4739c8a5c3a593f5caba4886de2e2e7867672922a
|
Provenance
The following attestation bundles were made for chemimg-2026.2.8-py3-none-any.whl:
Publisher:
python-package.yml on bsaldivaremc2/chemimg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chemimg-2026.2.8-py3-none-any.whl -
Subject digest:
0f2e6a9a95d5186a6f8b1299a4bb2bb42eeb87bfd223a9658b8924d1882d5337 - Sigstore transparency entry: 937082888
- Sigstore integration time:
-
Permalink:
bsaldivaremc2/chemimg@b68bfe457ba922d6ee014bec7c1e30993098ffe9 -
Branch / Tag:
refs/tags/v2026.02.10.1300 - Owner: https://github.com/bsaldivaremc2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@b68bfe457ba922d6ee014bec7c1e30993098ffe9 -
Trigger Event:
push
-
Statement type: