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)
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)'))
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)
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_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])
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