示例#1
0
def test_load_image():
    """
    Load an existing image
    """
    from painterm.image import Image

    # file-based:
    with TemporaryDirectory() as name:
        path = Path(name) / 'named'

        Image.new_image(9, 7, path)

        image = Image.load(path)

        assert_equals(image.width, 9)
        assert_equals(image.height, 7)
        assert_is_none(image.copyright)
        assert_is_none(image.description)

        cursor = image._cursor

        cursor.execute('SELECT major, minor, micro, width, height FROM meta;')
        assert_equals(cursor.fetchall(), [(0, 1, 0, 9, 7)])

        cursor.execute('SELECT id, time FROM transactions;')
        assert_equals(cursor.fetchall(), [(0, 0)])

        cursor.execute(
            'SELECT transaction_id, x, y, red, green, blue, alpha FROM draws;'
        )
        assert_equals(
            frozenset(cursor),
            {(0, x, y, *BACKGROUND) for x in range(9) for y in range(7)}
        )
示例#2
0
文件: cli.py 项目: bcj/painterm
def main():
    """
    Run painterm from the command line.
    """
    parser = ArgumentParser(description="A curses-based pixel art tool")
    sub = parser.add_subparsers(help="functions")

    new = sub.add_parser('new', help="Create a new image")
    new.set_defaults(function='new')

    new.add_argument(
        'path',
        type=lambda raw: Path(raw).expanduser().absolute(),
        help="The location to save to"
    )
    new.add_argument(
        '--width', '-x',
        default=DEFAULT_WIDTH,
        type=int,
        help="The width of the new image."
    )
    new.add_argument(
        '--height', '-y',
        default=DEFAULT_HEIGHT,
        type=int,
        help="The height of the new image."
    )
    new.add_argument(
        '--platform',
        default=None,
        type=lambda name: Platform[name.lower()],
        help="The palette to use ({})".format(
            ', '.join(Platform.__members__.keys())
        )
    )

    load = sub.add_parser('load', help="Load an existing image")
    load.set_defaults(function='load')

    load.add_argument(
        'path',
        type=lambda raw: Path(raw).expanduser().absolute(),
        help="The location to load from to"
    )

    load.add_argument(
        '--platform',
        default=None,
        type=lambda name: Platform[name.lower()],
        help="The palette to use ({})".format(
            ', '.join(Platform.__members__.keys())
        )
    )

    bmp = sub.add_parser('bmp', help="Export image to a bitmap file")
    bmp.set_defaults(function='bmp')

    bmp.add_argument(
        'input',
        type=lambda raw: Path(raw).expanduser().absolute(),
        help="The location of the image"
    )

    bmp.add_argument(
        'output',
        type=lambda raw: Path(raw).expanduser().absolute(),
        help="The location to save the bitmap"
    )

    bmp.add_argument(
        '--magnification', '-m',
        default=1,
        type=int,
        help="How much to magnify the bitmap"
    )

    pxon = sub.add_parser('pxon', help="Export image to a PXON file")
    pxon.set_defaults(function='pxon')

    pxon.add_argument(
        'input',
        type=lambda raw: Path(raw).expanduser().absolute(),
        help="The location of the image"
    )

    pxon.add_argument(
        'output',
        type=lambda raw: Path(raw).expanduser().absolute(),
        help="The location to save the PXON file"
    )

    pxon.add_argument(
        '--size', '-s',
        default=PXON_SIZE,
        type=int,
        help="How large each pixel should be"
    )

    args = parser.parse_args()

    if not hasattr(args, 'function'):
        print('run painterm --help to see options')
        return

    if args.function == 'new':
        palette = get_palette(args.platform)

        image = Image.new_image(
            args.width, args.height, path=args.path,
        )
    elif args.function == 'load':
        palette = get_palette(args.platform)

        image = Image.load(args.path)
    elif args.function == 'bmp':
        Image.load(args.input).export_bitmap(
            args.output, magnification=args.magnification
        )
        return
    elif args.function == 'pxon':
        Image.load(args.input).export_pxon(
            args.output, size=args.size
        )
        return

    curses.wrapper(
        painterm,
        image=image,
        palette=palette,
    )