Adding Logo to a PNG Image

Problem

You want to add your logo to a png image generated by OEDepict TK. See example in Table 1.

Table 1. Example of adding a logo to a molecule image
image generated by OEDepict TK image with OpenEye logo
../_images/molecule.png ../_images/molecule_logo.png

Difficulty Level

../_images/chilly2.png

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 PIL python image processing library. The following code illustrates how to superimpose a logo in the bottom right corner of the molecule image.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
import Image


def AddLogo(mfname, lfname, outfname):

    mimage = Image.open(mfname)
    limage = Image.open(lfname)

    # resize logo
    wsize = int(min(mimage.size[0], mimage.size[1]) * 0.25)
    wpercent = (wsize / float(limage.size[0]))
    hsize = int((float(limage.size[1]) * float(wpercent)))

    simage = limage.resize((wsize, hsize))
    mbox = mimage.getbbox()
    sbox = simage.getbbox()

    # right bottom corner
    box = (mbox[2] - sbox[2], mbox[3] - sbox[3])
    mimage.paste(simage, box)
    mimage.save(outfname)

AddLogo("molecule.png", "logo.png", "molecule_logo.png")

Discussion

See also