def save_layers(img, layers, compression, dir_name): ''' Takes a list of layers and saves them in `dir_name` as PNGs, naming the files after their layer names. ''' for layer in layers: tmp_img = pdb.gimp_image_new(img.width, img.height, img.base_type) tmp_layer = pdb.gimp_layer_new_from_drawable(layer, tmp_img) tmp_layer.name = layer.name tmp_img.add_layer(tmp_layer, 0) filename = '%s.png' % layer.name fullpath = os.path.join(dir_name, filename) tmp_img.resize_to_layers() pdb.file_png_save( tmp_img, tmp_img.layers[0], fullpath, filename, 0, # interlace compression, # compression 1, # bkgd 1, # gama 1, # offs 1, # phys 1 # time )
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 save_layers(img, layers, compression, dir_name): ''' Takes a list of layers and saves them in `dir_name` as PNGs, naming the files after their layer names. ''' for layer in layers: tmp_img = pdb.gimp_image_new(img.width, img.height, img.base_type) tmp_layer = pdb.gimp_layer_new_from_drawable(layer, tmp_img) tmp_layer.name = layer.name tmp_img.add_layer(tmp_layer, 0) filename = '%s.png' % layer.name fullpath = os.path.join(dir_name, filename) tmp_img.resize_to_layers() pdb.file_png_save( tmp_img, tmp_img.layers[0], fullpath, filename, 0, # interlace compression, # compression 1, # bkgd 1, # gama 1, # offs 1, # phys 1 # time )
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 save_layers(img, layers, compression, base_dir): for rel_path, layer in layers: rel_path = rel_path.replace('/', os.sep) tmp_img = pdb.gimp_image_new(img.width, img.height, img.base_type) tmp_layer = pdb.gimp_layer_new_from_drawable(layer, tmp_img) tmp_layer.name = layer.name tmp_img.add_layer(tmp_layer, 0) tmp_img.resize_to_layers() full_path = os.path.join(base_dir, rel_path) filename = os.path.basename(rel_path) pdb.file_png_save( tmp_img, tmp_img.layers[0], full_path, filename, 0, # interlace compression, # compression 1, # bkgd 1, # gama 1, # offs 1, # phys 1 # time )
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_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 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 _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 save_thumb(img, base_dir): tmp_img = pdb.gimp_image_new(img.width, img.height, img.base_type) for i, layer in enumerate(img.layers): tmp_layer = pdb.gimp_layer_new_from_drawable(layer, tmp_img) tmp_img.add_layer(tmp_layer, i) flattened = tmp_img.flatten() max_dim = 255 if img.width > max_dim or img.height > max_dim: if img.width > img.height: width = max_dim height = width * img.height / img.width elif img.width < img.height: height = max_dim width = height * img.width / img.height else: width = height = max_dim pdb.gimp_image_scale(tmp_img, width, height) thumb_path = os.path.join(base_dir, 'Thumbnails') mkdirs(thumb_path) thumb_filename = 'thumbnail.png' pdb.file_png_save_defaults(tmp_img, flattened, os.path.join(thumb_path, thumb_filename), thumb_filename)