def _create_test_image(): image = pdb.gimp_image_new(100, 100, gimpenums.RGB) layers = [ pdb.gimp_layer_new(image, 50, 20, gimpenums.RGBA_IMAGE, "Layer 1", 100.0, gimpenums.NORMAL_MODE), pdb.gimp_layer_new(image, 10, 10, gimpenums.RGBA_IMAGE, "Layer 2", 50.0, gimpenums.DISSOLVE_MODE), ] channels = [ pdb.gimp_channel_new(image, 100, 100, "Channel 1", 100.0, gimpcolor.RGB(0, 0, 0)), pdb.gimp_channel_new(image, 100, 100, "Channel 2", 50.0, gimpcolor.RGB(1, 0, 0)), ] vectors_list = [ pdb.gimp_vectors_new(image, "Vectors 1"), pdb.gimp_vectors_new(image, "Vectors 2"), ] for layer, channel, vectors in reversed( list(zip(layers, channels, vectors_list))): pdb.gimp_image_insert_layer(image, layer, None, 0) pdb.gimp_image_insert_channel(image, channel, None, 0) pdb.gimp_image_insert_vectors(image, vectors, None, 0) return image
def load_layer(filepath, image, strip_file_extension=False, layer_to_load_index=0): """ Load an image as a layer given its file path to an existing `image`. Return the layer. The layer is loaded at the end of the image. Layers names are basenames of the corresponding files. If `strip_file_extension` is `True`, remove the file extension from layer names. If the file contains multiple layers, specify the index of the desired layer to load. Only top-level layers are supported (i.e. not layers inside layer groups). If the index is greater than the number of layers in the loaded image or is negative, load and return the last layer. """ loaded_image = pdb.gimp_file_load(filepath, os.path.basename(filepath)) if layer_to_load_index >= len(image.layers) or layer_to_load_index < 0: layer_to_load_index = -1 layer = pdb.gimp_layer_new_from_drawable( loaded_image.layers[layer_to_load_index], image) layer.name = os.path.basename(filepath) if strip_file_extension: layer.name = os.path.splitext(layer.name)[0] pdb.gimp_image_insert_layer(image, layer, None, len(image.layers)) pdb.gimp_image_delete(loaded_image) return layer
def _apply_layer_attributes(image, layer, parent_group): temp_group = pdb.gimp_layer_group_new(image) pdb.gimp_image_insert_layer(image, temp_group, parent_group, 0) pdb.gimp_image_reorder_item(image, layer, temp_group, 0) layer = merge_layer_group(temp_group) return layer
def _create_test_image(): image = pdb.gimp_image_new(100, 100, gimpenums.RGB) layers = [ pdb.gimp_layer_new( image, 50, 20, gimpenums.RGBA_IMAGE, "Layer 1", 100.0, gimpenums.NORMAL_MODE), pdb.gimp_layer_new( image, 10, 10, gimpenums.RGBA_IMAGE, "Layer 2", 50.0, gimpenums.DISSOLVE_MODE), ] channels = [ pdb.gimp_channel_new(image, 100, 100, "Channel 1", 100.0, gimpcolor.RGB(0, 0, 0)), pdb.gimp_channel_new(image, 100, 100, "Channel 2", 50.0, gimpcolor.RGB(1, 0, 0)), ] vectors_list = [ pdb.gimp_vectors_new(image, "Vectors 1"), pdb.gimp_vectors_new(image, "Vectors 2"), ] for layer, channel, vectors in reversed(list(zip(layers, channels, vectors_list))): pdb.gimp_image_insert_layer(image, layer, None, 0) pdb.gimp_image_insert_channel(image, channel, None, 0) pdb.gimp_image_insert_vectors(image, vectors, None, 0) return image
def load_layer(filepath, image, strip_file_extension=False, layer_to_load_index=0): """ Load an image as a layer given its file path to an existing `image`. Return the layer. The layer is loaded at the end of the image. Layers names are basenames of the corresponding files. If `strip_file_extension` is True, remove the file extension from layer names. If the file contains multiple layers, specify the index of the desired layer to load. Only top-level layers are supported (i.e. not layers inside layer groups). If the index is greater than the number of layers in the loaded image or is negative, load and return the last layer. """ loaded_image = pdb.gimp_file_load(filepath, os.path.basename(filepath)) if layer_to_load_index >= len(image.layers) or layer_to_load_index < 0: layer_to_load_index = -1 layer = pdb.gimp_layer_new_from_drawable( loaded_image.layers[layer_to_load_index], image) layer.name = os.path.basename(filepath) if strip_file_extension: layer.name = os.path.splitext(layer.name)[0] pdb.gimp_image_insert_layer(image, layer, None, len(image.layers)) pdb.gimp_image_delete(loaded_image) return layer
def copy_and_insert_layer(image, layer, parent=None, position=0): layer_copy = pdb.gimp_layer_new_from_drawable(layer, image) pdb.gimp_image_insert_layer(image, layer_copy, parent, position) pdb.gimp_item_set_visible(layer_copy, True) if pdb.gimp_item_is_group(layer_copy): layer_copy = pgpdb.merge_layer_group(layer_copy) return layer_copy
def copy_and_insert_layer(image, layer, parent=None, position=0): layer_copy = pdb.gimp_layer_new_from_drawable(layer, image) pdb.gimp_image_insert_layer(image, layer_copy, parent, position) pdb.gimp_item_set_visible(layer_copy, True) if pdb.gimp_item_is_group(layer_copy): layer_copy = pg.pdbutils.merge_layer_group(layer_copy) return layer_copy
def _copy_layers(image, layers, parent=None, position=0): layer_group = pdb.gimp_layer_group_new(image) pdb.gimp_image_insert_layer(image, layer_group, parent, position) for layer in layers: copy_and_paste_layer(layer, image, parent=layer_group) for layer in layer_group.children: layer.visible = True return layer_group
def copy_and_paste_layer(layer, image, parent=None, position=0): """ Copy the specified layer into the specified image, parent layer group and position in the group. Return the copied layer. If `parent` is `None`, insert the layer in the main stack (outside of any layer group). """ layer_copy = pdb.gimp_layer_new_from_drawable(layer, image) pdb.gimp_image_insert_layer(image, layer_copy, parent, position) return layer_copy
def _postprocess_layer(self, image, layer): if not self._keep_image_copy: pdb.gimp_image_remove_layer(image, layer) else: if self._use_another_image_copy: another_layer_copy = pdb.gimp_layer_new_from_drawable( layer, self._another_image_copy) pdb.gimp_image_insert_layer( self._another_image_copy, another_layer_copy, None, len(self._another_image_copy.layers)) another_layer_copy.name = layer.name pdb.gimp_image_remove_layer(image, layer)
def _insert_tagged_layer(image, layer_exporter, tag, position=0): if not layer_exporter.tagged_layer_elems[tag]: return if layer_exporter.tagged_layer_copies[tag] is None: layer_exporter.inserted_tagged_layers[tag] = ( _insert_merged_tagged_layer(image, layer_exporter, tag, position)) layer_exporter.tagged_layer_copies[tag] = ( pdb.gimp_layer_copy(layer_exporter.inserted_tagged_layers[tag], True)) else: layer_exporter.inserted_tagged_layers[tag] = ( pdb.gimp_layer_copy(layer_exporter.tagged_layer_copies[tag], True)) pdb.gimp_image_insert_layer( image, layer_exporter.inserted_tagged_layers[tag], None, position)
def _insert_tagged_layer(image, tag, layer_exporter, positon=0): if not layer_exporter.tagged_layer_elems[tag]: return if layer_exporter.tagged_layer_copies[tag] is None: layer_group = pdb.gimp_layer_group_new(image) pdb.gimp_image_insert_layer(image, layer_group, None, positon) for i, layer_elem in enumerate(layer_exporter.tagged_layer_elems[tag]): layer_copy = copy_and_insert_layer(image, layer_elem.item, layer_group, i) layer_exporter.operation_executor.execute( ["after_insert_layer"], image, layer_copy, layer_exporter) layer_exporter.inserted_tagged_layers[tag] = pgpdb.merge_layer_group(layer_group) layer_exporter.tagged_layer_copies[tag] = ( pdb.gimp_layer_copy(layer_exporter.inserted_tagged_layers[tag], True)) else: layer_exporter.inserted_tagged_layers[tag] = ( pdb.gimp_layer_copy(layer_exporter.tagged_layer_copies[tag], True)) pdb.gimp_image_insert_layer( image, layer_exporter.inserted_tagged_layers[tag], None, positon)