Adding Logo to PNG Image
Problem
You want to add your logo to a png image generated by OEDepict TK.
See example in Table 1.
image generated by OEDepict TK |
image with OpenEye logo |
|
|
Ingredients
Difficulty Level
🌶️
Download
Source Code
addlogo
#!/usr/bin/env python3
# (C) 2023 Cadence Design Systems, Inc. (Cadence)
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable Cadence offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.
"""Code snippet for add logo."""
import os
import sys
import warnings
from pathlib import Path
from PIL import Image
def main() -> int:
"""Adding logo to image."""
argv = sys.argv
if len(sys.argv) != 4: # noqa: PLR2004
warnings.warn(
f"Usage {argv[0]} <in_image> <logo_image> <out_image>", stacklevel=1
)
return 1
in_image, logo, out_image = Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3])
add_logo(in_image, logo, out_image)
return os.EX_OK
def add_logo(input_filename: Path, logo_filename: Path, output_filename: Path) -> bool:
"""Add the togo to the bottom left corner of the image."""
in_image = Image.open(input_filename)
logo_image = Image.open(logo_filename)
# resize logo
w_size = int(min(in_image.size[0], in_image.size[1]) * 0.30)
w_percent = w_size / float(logo_image.size[0])
h_size = int(float(logo_image.size[1]) * float(w_percent))
new_image = logo_image.resize((w_size, h_size))
mbox: tuple[int, int, int, int] | None = in_image.getbbox()
new_box: tuple[int, int, int, int] | None = new_image.getbbox()
if not mbox or not new_box:
warnings.warn(
"Cannot calculate bounding box of input or output image", stacklevel=2
)
return False
# right bottom corner
box = (mbox[2] - new_box[2], mbox[3] - new_box[3])
in_image.paste(new_image, box)
in_image.save(output_filename)
return True
if __name__ == "__main__":
sys.exit(main())
Solution
OEDepict TK does not provide functionality to paste a png image into the
png image generated for a molecule.
But this can be easily achieved by using the Pillow Python image processing library.
The following code illustrates how to superimpose a logo in the bottom right corner
of the molecule image.
1def add_logo(input_filename: Path, logo_filename: Path, output_filename: Path) -> bool:
2 """Add the togo to the bottom left corner of the image."""
3 in_image = Image.open(input_filename)
4 logo_image = Image.open(logo_filename)
5
6 # resize logo
7 w_size = int(min(in_image.size[0], in_image.size[1]) * 0.30)
8 w_percent = w_size / float(logo_image.size[0])
9 h_size = int(float(logo_image.size[1]) * float(w_percent))
10
11 new_image = logo_image.resize((w_size, h_size))
12 mbox: tuple[int, int, int, int] | None = in_image.getbbox()
13 new_box: tuple[int, int, int, int] | None = new_image.getbbox()
14 if not mbox or not new_box:
15 warnings.warn(
16 "Cannot calculate bounding box of input or output image", stacklevel=2
17 )
18 return False
19
20 # right bottom corner
21 box = (mbox[2] - new_box[2], mbox[3] - new_box[3])
22 in_image.paste(new_image, box)
23 in_image.save(output_filename)
24
25 return True
Usage
molecule.png and
logo.png test images
prompt > python3 addlogo.py molecule.png logo.png molecule-logo.png
Discussion
To install Pillow in a Python 3 conda environment:
prompt > conda create -n oecookbook python=3
prompt > source activate oecookbook
(oecookbook) prompt > pip install Pillow