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()
def test_create_from_template(): with TempFile('.xcf') as original, TempFile('.xcf') as created: original_file = GimpFile(original).create( 'Background', np.zeros(shape=(3, 2), dtype=np.uint8)) created_file = GimpFile(created).create_from_template(original_file) assert [] == created_file.layer_names() assert (2, 3) == created_file.dimensions()
def test_copy(): with TempFile('.xcf') as original, TempFile('.xcf') as copy: original_file = GimpFile(original).create( 'Background', np.zeros(shape=(2, 2), dtype=np.uint8)) copied_file = original_file.copy(copy) original_file.add_layer_from_numpy( 'New', np.zeros(shape=(2, 2), dtype=np.uint8)) assert ['Background'] == copied_file.layer_names() assert ['New', 'Background'] == original_file.layer_names()
def test_add_layer_from_numpy_with_position_layer_name(): data = np.array([[[255, 255, 255]]], dtype=np.uint8) with TempFile('.xcf') as tmp_file: gimp_file = GimpFile(tmp_file) gimp_file.create('Background', data) gimp_file.add_layer_from_numpy('Layer 1', data, position='Background') gimp_file.add_layer_from_numpy('Layer 2', data, position='Background') assert gimp_file.layer_names() == ['Layer 1', 'Layer 2', 'Background']
def test_remove_layers_by_name(): data = np.array([[0, 255]], dtype=np.uint8) with TemporaryDirectory('_files') as dir: file1 = GimpFile(os.path.join(dir, 'file1.xcf')) \ .create('Background', data) \ .add_layer_from_numpy('Layer 1', data) \ .add_layer_from_numpy('Layer 2', data) \ .add_layer_from_numpy('Layer 3', data) file2 = GimpFile(os.path.join(dir, 'file2.xcf')) \ .create('Background', data) \ .add_layer_from_numpy('Layer 1', data) \ .add_layer_from_numpy('Layer 2', data) collection = GimpFileCollection([file1.get_file(), file2.get_file()]) collection.remove_layers_by_name(['Layer 1', 'Layer 3'], timeout_in_seconds=10) assert file1.layer_names() == ['Layer 2', 'Background'] assert file2.layer_names() == ['Layer 2', 'Background']
def test_add_layers_from_numpy(): with TempFile('.xcf') as f: gimp_file = GimpFile(f).create('Background', np.zeros(shape=(1, 2), dtype=np.uint8)) gimp_file.add_layers_from_numpy( ['Layer 1', 'Layer 2'], np.ones(shape=(2, 1, 2), dtype=np.uint8) * 255, opacity=55., visible=False, position='Background') assert gimp_file.layer_names() == ['Layer 1', 'Layer 2', 'Background']
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()
def test_add_layers_from_numpy_with_list_config(): with TempFile('.xcf') as f: gimp_file = GimpFile(f).create('Background', np.zeros(shape=(1, 2), dtype=np.uint8)) gimp_file.add_layers_from_numpy( ['Layer 1', 'Layer 2'], np.ones(shape=(2, 1, 2), dtype=np.uint8) * 255, opacity=[100., 55.], visible=[False, True], position='Background', blend_mode=gimpenums.NORMAL_MODE) assert gimp_file.layer_names() == ['Layer 1', 'Layer 2', 'Background']
def test_remove_layer(): tmp_file = tempfile.mktemp(suffix='.xcf') layer = 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(tmp_file) gimp_file.create('Background', layer) gimp_file.add_layer_from_numpy('Layer', layer) all_layers = gimp_file.layer_names() gimp_file.remove_layer('Background') remaining_layers1 = gimp_file.layer_names() gimp_file.remove_layer('Layer') remaining_layers2 = gimp_file.layer_names() os.remove(tmp_file) assert ['Layer', 'Background'] == all_layers assert ['Layer'] == remaining_layers1 assert [] == remaining_layers2
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()
def test_create_empty(): with TempFile('.xcf') as f: gimp_file = GimpFile(f).create_empty(3, 2, GimpFileType.RGB) assert (3, 2) == gimp_file.dimensions() assert [] == gimp_file.layer_names()