示例#1
0
def test_copy_layer_from():
    with TemporaryDirectory('_src') as srcdir, TemporaryDirectory('_dst') as dstdir:
        src_1 = GimpFile(os.path.join(srcdir, 'file1.xcf'))\
            .create('Background', np.zeros(shape=(1, 1), dtype=np.uint8))\
            .add_layer_from_numpy('White', np.ones(shape=(1, 1), dtype=np.uint8)*255)
        src_2 = GimpFile(os.path.join(srcdir, 'file2.xcf'))\
            .create('Background', np.zeros(shape=(1, 1), dtype=np.uint8)) \
            .add_layer_from_numpy('White', np.ones(shape=(1, 1), dtype=np.uint8)*255)

        dst_1 = GimpFile(os.path.join(dstdir, 'file1.xcf')) \
            .create('Background', np.zeros(shape=(1, 1), dtype=np.uint8)) \
            .add_layer_from_numpy('White', np.zeros(shape=(1, 1), dtype=np.uint8)*255)
        dst_2 = GimpFile(os.path.join(dstdir, 'file2.xcf')) \
            .create('Background', np.zeros(shape=(1, 1), dtype=np.uint8))

        src_collection = GimpFileCollection([src_1.get_file(), src_2.get_file()])
        dst_collection = GimpFileCollection([dst_1.get_file(), dst_2.get_file()])

        dst_collection.copy_layer_from(src_collection, 'White', layer_position=1, timeout_in_seconds=10)

        assert np.all(dst_1.layer_to_numpy('White') == 255)
        assert ['Background', 'White'] == dst_1.layer_names()
        assert 'White' in dst_2.layer_names()
        assert np.all(dst_2.layer_to_numpy('White') == 255)
        assert ['Background', 'White'] == dst_2.layer_names()
示例#2
0
def test_merge_layer_from_file():
    with TempFile('.xcf') as dst, TempFile('.xcf') as src:
        layer_bg = np.array([
            [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
            [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        ],
                            dtype=np.uint8)

        src_file = GimpFile(src)
        src_file.create('Background', np.zeros(shape=(2, 3, 3),
                                               dtype=np.uint8))
        src_file.add_layer_from_numpy(
            'Yellow',
            np.array([
                [[240, 255, 0], [240, 255, 0], [240, 255, 0]],
                [[240, 255, 0], [240, 255, 0], [240, 255, 0]],
            ],
                     dtype=np.uint8))

        dst_file = GimpFile(dst)
        dst_file.create('Yellow', layer_bg)
        dst_file.merge_layer_from_file(src_file, 'Yellow')

        new_layer_contents = dst_file.layer_to_numpy('Yellow')

        assert np.all([240, 255, 0] == new_layer_contents)
示例#3
0
def test_add_layer_from_file():
    with TempFile('.xcf') as dst, TempFile('.xcf') as src:
        layer_bg = np.array([
            [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
            [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        ],
                            dtype=np.uint8)
        position = 1

        src_file = GimpFile(src)
        src_file.create('Background', np.zeros(shape=(2, 3, 3),
                                               dtype=np.uint8))
        src_file.add_layer_from_numpy(
            'Yellow',
            np.array([
                [[240, 255, 0], [240, 255, 0], [240, 255, 0]],
                [[240, 255, 0], [240, 255, 0], [240, 255, 0]],
            ],
                     dtype=np.uint8))

        dst_file = GimpFile(dst)
        dst_file.create('Background', layer_bg)
        dst_file.add_layer_from_file(src_file,
                                     'Yellow',
                                     new_name='Yellow (copied)',
                                     new_position=position)

        assert 'Yellow (copied)' == dst_file.layers()[position].name
        assert np.all(
            [240, 255, 0] == dst_file.layer_to_numpy('Yellow (copied)'))
示例#4
0
def test_add_layer_from_numpy():
    tmp_file = tempfile.mktemp(suffix='.xcf')
    layer_bg = np.array([
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
    ],
                        dtype=np.uint8)
    layer_fg = np.array([
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
    ],
                        dtype=np.uint8)

    gimp_file = GimpFile(tmp_file)
    gimp_file.create('Background', layer_bg)
    gimp_file.add_layer_from_numpy('Foreground',
                                   layer_fg,
                                   opacity=55.,
                                   visible=False)

    actual_bg = gimp_file.layer_to_numpy('Background')
    actual_fg = gimp_file.layer_to_numpy('Foreground')

    os.remove(tmp_file)

    assert np.all(layer_bg == actual_bg)
    assert np.all(layer_fg == actual_fg)
示例#5
0
def test_convert_to_indexed_using_predefined_colormap():
    tmp_file = tempfile.mktemp(suffix='.xcf')
    values = np.array([[i for i in range(0, 256)]], dtype=np.uint8)
    assert (1, 256) == values.shape

    gimp_file = GimpFile(tmp_file)
    gimp_file.create_indexed('Background', values, ColorMap.JET)
    gimp_file.add_layer_from_numpy('Values', values, type=LayerType.INDEXED)

    layer_bg = gimp_file.layer_to_numpy('Background')
    layer_values = gimp_file.layer_to_numpy('Values')

    os.remove(tmp_file)

    assert (1, 256, 1) == layer_bg.shape
    assert np.all(values == layer_bg[:, :, 0])
    assert (1, 256, 1) == layer_values.shape
    assert np.all(values == layer_values[:, :, 0])
示例#6
0
def test_merge_mask_layer_from_with_color():
    with TemporaryDirectory('_src') as srcdir, TemporaryDirectory('_dst') as dstdir:
        src_1 = GimpFile(os.path.join(srcdir, 'file1.xcf'))\
            .create('Mask', np.array([[[255, 255, 255], [0, 0, 0]]], dtype=np.uint8))

        dst_1 = GimpFile(os.path.join(dstdir, 'file1.xcf')) \
            .create('Mask', np.array([[[0, 0, 0], [255, 255, 255]]], dtype=np.uint8))
        dst_2 = GimpFile(os.path.join(dstdir, 'file2.xcf')) \
            .create('Mask', np.array([[[0, 0, 0], [255, 255, 255]]], dtype=np.uint8))

        src_collection = GimpFileCollection([src_1.get_file()])
        dst_collection = GimpFileCollection([dst_1.get_file(), dst_2.get_file()])

        dst_collection.merge_mask_layer_from(src_collection, 'Mask', MaskForegroundColor.WHITE, timeout_in_seconds=10)

        assert np.all(dst_1.layer_to_numpy('Mask') == [[255, 255, 255], [255, 255, 255]])
        assert ['Mask'] == dst_1.layer_names()
        assert 'Mask' in dst_2.layer_names()
        assert np.all(dst_2.layer_to_numpy('Mask') == [[0, 0, 0], [255, 255, 255]])
        assert ['Mask'] == dst_2.layer_names()
示例#7
0
def test_merge_mask_layer_from_with_mask_not_available_in_files_in_both_collections_and_foreground_color_black():
    with TemporaryDirectory('_src') as srcdir, TemporaryDirectory('_dst') as dstdir:
        src_1 = GimpFile(os.path.join(srcdir, 'file1.xcf')) \
            .create_empty(2, 1, GimpFileType.GRAY)

        dst_1 = GimpFile(os.path.join(dstdir, 'file1.xcf')) \
            .create_empty(2, 1, GimpFileType.GRAY)

        src_collection = GimpFileCollection([src_1.get_file()])
        dst_collection = GimpFileCollection([dst_1.get_file()])

        dst_collection.merge_mask_layer_from(src_collection, 'Mask', MaskForegroundColor.BLACK, timeout_in_seconds=10)

        assert np.all(dst_1.layer_to_numpy('Mask') == [[255], [255]])
        assert ['Mask'] == dst_1.layer_names()
示例#8
0
def test_create():
    filename = file.relative_to(__file__, 'test-resources/test-create.xcf')
    layer_bg = np.array([
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
    ],
                        dtype=np.uint8)

    gimp_file = GimpFile(filename)
    gimp_file.create('Background', layer_bg)

    exists = os.path.exists(filename)
    actual = gimp_file.layer_to_numpy('Background')
    os.remove(filename)

    assert exists
    assert np.all(layer_bg == actual)
示例#9
0
def test_merge_layer_from_file_with_cleared_selection():
    src = file.relative_to(__file__, 'test-resources/selection.xcf')
    with TempFile('.xcf') as dst:
        src_file = GimpFile(src)
        dst_file = GimpFile(dst)
        dst_file.create('Background', np.zeros(shape=(3, 3, 3),
                                               dtype=np.uint8))
        dst_file.merge_layer_from_file(src_file, 'Background')

        new_layer_contents = dst_file.layer_to_numpy('Background')

        assert np.all(
            np.array([
                [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
                [[255, 0, 0], [255, 0, 0], [255, 255, 255]],
                [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
            ],
                     dtype=np.uint8) == new_layer_contents)
示例#10
0
    # layer content
    bg = np.zeros(shape=(height, width), dtype=np.uint8)
    fg = np.ones(shape=(height, width), dtype=np.uint8) * 255
    mask = np.zeros(shape=(height, width), dtype=np.uint8)
    mask[:, width//4:3*width//4+1] = 255

    with TempFile('.xcf') as xcf, TempFile('.npz') as npz:
        # create gimp file
        gimp_file = GimpFile(xcf) \
            .create('Background', bg) \
            .add_layer_from_numpy('Foreground', fg) \
            .add_layer_from_numpy('Mask', mask)

        # save layer data to numpy arrays
        arr_bg = gimp_file.layer_to_numpy('Background')
        arr_fg = gimp_file.layer_to_numpy('Foreground')
        arr_mask = gimp_file.layer_to_numpy('Mask')

        # save data as npz
        np.savez_compressed(npz, bg=arr_bg, fg=arr_fg, mask=arr_mask)

        # load data from npz
        loaded = np.load(npz)
        loaded_bg = loaded['bg']
        loaded_fg = loaded['fg']
        loaded_mask = loaded['mask']

    # merge background and foreground using mask
    mask_idxs = loaded_mask == 255
    img = loaded_bg.copy()