import os, sys
try:
    import PIL
except ImportError:
    print "Installer Python Imaging Library(PIL)"
    sys.exit(-1)
from PIL import Image, ImageColor

paste_img = Image.open("NILU_logo_200.jpg")
    
#x, y = 571, 390 # upper left corner of black box
x, y = 991, 710 # upper left corner of black box
w, h = 58, 74 # width and height inside black border
#w, h = 60, 72
b = 2 # width white border

paste_img = paste_img.resize((w - 2*b, h - 2*b), Image.ANTIALIAS)

def fix_image(ipath, opath):
    img = Image.open(ipath)
    img = img.convert("RGB")
    img.paste(ImageColor.getcolor("#ffffff", img.mode), (x, y, x+w, y+h))
    img.paste(paste_img, (x+b, y+b, x+w-b, y+h-b))
    img.save(opath)

def main():
    usage = "python %s file1.png [file2.png] [file3.png] [...]" % sys.argv[0]
    if len(sys.argv) < 2:
        print usage
        return -1

    outdir = "fix"
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    for i in sys.argv[1:]:
        o = os.path.join(outdir, os.path.basename(i))
        print "%s => %s" % (i, o)
        fix_image(i, o)

    return 0

if __name__ == "__main__": sys.exit(main())
