#!/usr/bin/env python

import sys
from os import path
from PIL import Image, ImageFile, ImageOps

ImageFile.LOAD_TRUNCATED_IMAGES = True # accept extra infos in png

if not len(sys.argv) in [5, 6]:
    print("Use: merge.py R G B OUT (invert)")
    exit(1)

trues = [1, "true", "True"]
[_, r, g, b, out, inv] = sys.argv

r = Image.open(r).convert('L')
g = Image.open(g).convert('L')
b = Image.open(b).convert('L')
if inv in trues:
    b = ImageOps.invert(b)
Image.merge("RGB", (r, g, b)).save(out)