示例#1
0
def main():
    colorama.init()

    try:
        cli = Cli()

        if cli.flag_exists('version', 'v'):
            version()
            del cli
            return

        if cli.flag_exists('help', 'p'):
            show_help()
            del cli
            return

        command = cli.get_arg(1, 'command',
                              ['resize', 'trim', 'crop', 'convert'])

        path = cli.get_arg(2, 'path')
        imp = Imp(path)

        # Flags.
        w = cli.get_flag_value('width', 'w')
        h = cli.get_flag_value('height', 'h')
        t = cli.get_flag_value('top', 't')
        l = cli.get_flag_value('left', 'l')
        e = cli.get_flag_value('ext', 'e')
        o = cli.get_flag_value('out', 'o')

        if o != None:
            imp.set_path(o)
        if e != None:
            imp.set_extension(e)

        if command == 'resize':
            imp.resize(w, h)
            imp.save()
            log(f'Image resized with success in "{imp.get_path()}"!')

        elif command == 'crop':
            imp.crop(l, t, w, h)
            imp.save()
            log(f'Image cropped with success in "{imp.get_path()}"!')

        elif command == 'trim':
            imp.trim()
            imp.save()
            log(f'Image trimmed with success in "{imp.get_path()}"!')

        elif command == 'convert':
            imp.convert(e)
            imp.save()
            log(f'Image converted with success in "{imp.get_path()}"!')

        if cli in locals():
            del cli

        if imp in locals():
            del imp

    except Exception as err:
        show_help()
        error(err)