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
Ejemplo n.º 2
0
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
        )
Ejemplo n.º 3
0
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 _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
Ejemplo n.º 5
0
def create_image_from_metadata(image_to_copy_metadata_from):
  """
  Create a new image with metadata (dimensions, base type, parasites, etc.)
  copied from `image_to_copy_metadata_from`.
  
  Layers, channels or paths are not copied. For a full image copy, use
  `pdb.gimp_image_duplicate()`.
  """
  image = image_to_copy_metadata_from
  
  new_image = pdb.gimp_image_new(image.width, image.height, image.base_type)
  
  pdb.gimp_image_set_resolution(new_image, *pdb.gimp_image_get_resolution(image))
  pdb.gimp_image_set_unit(new_image, pdb.gimp_image_get_unit(image))
  
  if image.base_type == gimpenums.INDEXED:
    pdb.gimp_image_set_colormap(new_image, *pdb.gimp_image_get_colormap(image))
  
  # Copy image parasites
  unused_, parasite_names = pdb.gimp_image_get_parasite_list(image)
  for name in parasite_names:
    parasite = image.parasite_find(name)
    # `pdb.gimp_image_parasite_attach` fails for some reason - use
    # `gimp.Image.parasite_attach` instead.
    new_image.parasite_attach(
      gimp.Parasite(parasite.name, parasite.flags, parasite.data))
  
  return new_image
Ejemplo n.º 6
0
def duplicate(image, metadata_only=False):
  """
  Duplicate the specified image.
  
  If `metadata_only` is True, copy image metadata only (i.e. do not copy layers,
  channels or paths).
  """
  
  if not metadata_only:
    new_image = pdb.gimp_image_duplicate(image)
  else:
    new_image = pdb.gimp_image_new(image.width, image.height, image.base_type)
    
    pdb.gimp_image_set_resolution(new_image, *pdb.gimp_image_get_resolution(image))
    pdb.gimp_image_set_unit(new_image, pdb.gimp_image_get_unit(image))
    
    if image.base_type == gimpenums.INDEXED:
      pdb.gimp_image_set_colormap(new_image, *pdb.gimp_image_get_colormap(image))
    
    # Copy image parasites
    unused_, parasite_names = pdb.gimp_image_get_parasite_list(image)
    for name in parasite_names:
      parasite = image.parasite_find(name)
      # `pdb.gimp_image_parasite_attach` fails for some reason - use
      # `gimp.Image.parasite_attach` instead.
      new_image.parasite_attach(
        gimp.Parasite(parasite.name, parasite.flags, parasite.data))
  
  return new_image
Ejemplo n.º 7
0
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 create_image_from_metadata(image_to_copy_metadata_from):
    """
  Create a new image with metadata (dimensions, base type, parasites, etc.)
  copied from `image_to_copy_metadata_from`.
  
  Layers, channels or paths are not copied. For a full image copy, use
  `pdb.gimp_image_duplicate()`.
  """
    image = image_to_copy_metadata_from

    new_image = pdb.gimp_image_new(image.width, image.height, image.base_type)

    pdb.gimp_image_set_resolution(new_image,
                                  *pdb.gimp_image_get_resolution(image))
    pdb.gimp_image_set_unit(new_image, pdb.gimp_image_get_unit(image))

    if image.base_type == gimpenums.INDEXED:
        pdb.gimp_image_set_colormap(new_image,
                                    *pdb.gimp_image_get_colormap(image))

    # Copy image parasites
    unused_, parasite_names = pdb.gimp_image_get_parasite_list(image)
    for name in parasite_names:
        parasite = image.parasite_find(name)
        # `pdb.gimp_image_parasite_attach` fails for some reason - use
        # `gimp.Image.parasite_attach` instead.
        new_image.parasite_attach(
            gimp.Parasite(parasite.name, parasite.flags, parasite.data))

    return new_image
Ejemplo n.º 9
0
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)
 def setUpClass(cls):
   cls.image = pdb.gimp_image_new(1, 1, gimpenums.RGB)