#!/usr/bin/env python3

"""
warn if the division is not clean, and suggest alternative
"""
from PIL import Image
import sys

def resize(f, scale=None, width=None, height=None, outfile="resized.png", **kwargs):
    im = Image.open(f)
    _width, _height = im.size
    new_size = (_width, _height)
    if scale is not None:
        scale = float(scale)
        new_size = (_width * scale, _height * scale)
    elif width is not None:
        width = int(width)
        new_size = (width, int(_height*(width * 1.0/_width)))
    elif height is not None:
        height = int(height)
        new_size = (int(_width*(height * 1.0/_height)), height)
    im = im.resize(new_size)
    im.save(outfile)


def size(f, **kwargs):
    im = Image.open(f)
    print("{},{}".format(im.size[0], im.size[1]))


def main():
    if len(sys.argv) == 1:
        print(
            "{} resize <filename> [--scale=<value>] [--width=<value>] [--height=<value>] [--outfile=<value>]"
            .format(sys.argv[0])
        )
        print("{} size <filename>".format(sys.argv[0]))
        return

    function = sys.argv[1]
    filename = sys.argv[2]
    args = sys.argv[3:]
    kwargs = {}
    for arg in args:
        s = arg.split("=")
        if s[:2] != "--":
            continue
        kwargs[s[0][2:]] = s[1]

    if function == "resize":
        resize(filename, **kwargs)
    elif function == "size":
        size(filename, **kwargs)


if __name__ == "__main__":
    main()

