def pythonSaveToPSD(image):

    # Prep
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()

    # Code
    drawable = pdb.gimp_image_get_active_drawable(image)
    filename = pdb.gimp_image_get_filename(image)
    temp = filename.replace("_CLEAN", "")
    temp2 = temp.replace("_EDIT", "")
    temp = temp2.replace("_XCF", "")
    filename = temp.replace("_PSD", "")
    path = os.path.dirname(filename)
    path += "_PSD"
    name = os.path.basename(filename)
    out_psd = os.path.join(path, name)
    out_psd = os.path.splitext(out_psd)[0] + '.psd'

    # Create folder
    if not os.path.exists(path):
        os.makedirs(path)

    # Save in PSD
    pdb.gimp_file_save(image, drawable, out_psd, out_psd)
    pdb.gimp_image_clean_all(image)

    # Finish
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_displays_flush()
Beispiel #2
0
def pythonSaveToClean(image):

    # Prep
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()

    # Code
    filename_old = pdb.gimp_image_get_filename(image)
    filename = filename_old.replace("_CLEAN", "")
    # drawable = pdb.gimp_image_get_active_drawable(image)
    path = os.path.dirname(filename)
    path += "_CLEAN"
    name = os.path.basename(filename)
    out_file = os.path.join(path, name)

    # Create CLEAN folder
    if not os.path.exists(path):
        os.makedirs(path)

    # Merge visible layers in new image and export in CLEAN folder
    new_image = pdb.gimp_image_duplicate(image)
    layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
    pdb.gimp_file_save(new_image, layer, out_file, out_file)
    pdb.gimp_image_delete(new_image)
    pdb.gimp_image_clean_all(image)

    # Finish
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_displays_flush()
def pythonSaveToEditJpeg(image):

    # Prep
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()

    # Code
    # drawable = pdb.gimp_image_get_active_drawable(image)
    filename = pdb.gimp_image_get_filename(image)
    temp = filename.replace("_CLEAN", "")
    temp2 = temp.replace("_EDIT", "")
    filename = temp2.replace("_XCF", "")
    path = os.path.dirname(filename)
    path += "_EDIT"
    name = os.path.basename(filename)
    out_file = os.path.join(path, name)
    out_file = os.path.splitext(out_file)[0] + '.jpg'

    # Create _EDIT folder
    if not os.path.exists(path):
        os.makedirs(path)

    # Merge visible layers in new image and export in JPEG
    new_image = pdb.gimp_image_duplicate(image)
    layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
    pdb.gimp_file_save(new_image, layer, out_file, out_file)
    pdb.gimp_image_delete(new_image)
    pdb.gimp_image_clean_all(image)

    # Finish
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_displays_flush()
Beispiel #4
0
 def run_outer(self, gimp_img, drawable, *extra_args):
     self.gimp_img = gimp_img
     self.drawable = drawable
     print("Running {}...".format(self.name))
     pdb.gimp_image_undo_group_start(self.gimp_img)
     gimp.progress_init("Running {}...".format(self.name))
     self.run(*extra_args)
     pdb.gimp_image_undo_group_end(self.gimp_img)
Beispiel #5
0
def MDFade(image, layer, srcmode, dstmode, fademode):
    srclut = SelectSrcLUT(srcmode)
    dstlut = SelectDstLUT(dstmode)
    gimp.progress_init("Generating palette fade...")
    pdb.gimp_image_undo_group_start(image)
    # Get the layer position.
    pos = FindLayer(image, layer)
    srcWhite = FindColor(255, srcmode)
    if (fademode == FadeMode.CurrentToBlack):
        conv = lambda jj, ss: (jj * (15 - ss)) // 15
    elif (fademode == FadeMode.BlackToCurrent):
        conv = lambda jj, ss: (jj * ss) // 15
    elif (fademode == FadeMode.CurrentToWhite):
        conv = lambda jj, ss: (jj * (15 - ss) + srcWhite * ss) // 15
    else:  #if (fademode == FadeMode.WhiteToCurrent):
        conv = lambda jj, ss: (jj * ss + srcWhite * (15 - ss)) // 15
    finalLayer = None
    for step in xrange(15):
        lut = {
            chr(ii): chr(dstlut[srclut[conv(ii, step)]])
            for ii in xrange(256)
        }
        # Create a new layer to save the results (otherwise is not possible to undo the operation).
        newLayer = layer.copy()
        image.add_layer(newLayer, pos)
        # Clear the new layer.
        pdb.gimp_edit_clear(newLayer)
        newLayer.flush()
        # Calculate the number of tiles.
        tile_width = gimp.tile_width()
        tile_height = gimp.tile_height()
        # Ceiling division
        width_tiles = -(-layer.width // tile_width)
        height_tiles = -(-layer.height // tile_height)
        # Iterate over the tiles.
        for col in xrange(width_tiles):
            for row in xrange(height_tiles):
                # Update the progress bar.
                gimp.progress_update(
                    float(step * width_tiles * height_tiles +
                          col * height_tiles + row) /
                    float(15 * width_tiles * height_tiles))
                ConvertTileNoSHL(layer.get_tile(False, row, col),
                                 newLayer.get_tile(False, row, col), lut)
        # Update the new layer.
        newLayer.flush()
        newLayer.merge_shadow(True)
        newLayer.update(0, 0, layer.width, layer.height)
        finalLayer = newLayer
    # Remove the old layer.
    if finalLayer is not None:
        layerName = layer.name
        image.remove_layer(layer)
        finalLayer.name = layerName
    gimp.displays_flush()
    pdb.gimp_image_undo_group_end(image)
Beispiel #6
0
def MDColors(image, layer, srcmode, dstmode, shlmode):
    lut = BuildColorLUT(srcmode, dstmode, shlmode)
    gimp.progress_init("Converting to MD colors...")
    # Indexed images are faster
    if layer.is_indexed:
        ConvertColormap(image, lut)
    else:
        pdb.gimp_image_undo_group_start(image)
        # Get the layer position.
        pos = FindLayer(image, layer)
        # Create a new layer to save the results (otherwise is not possible to undo the operation).
        newLayer = layer.copy()
        image.add_layer(newLayer, pos)
        layerName = layer.name
        # Clear the new layer.
        pdb.gimp_edit_clear(newLayer)
        newLayer.flush()
        # Calculate the number of tiles.
        tile_width = gimp.tile_width()
        tile_height = gimp.tile_height()
        # Ceiling division
        width_tiles = -(-layer.width // tile_width)
        height_tiles = -(-layer.height // tile_height)
        # Iterate over the tiles.
        for col in xrange(width_tiles):
            for row in xrange(height_tiles):
                # Update the progress bar.
                gimp.progress_update(
                    float(col * height_tiles + row) /
                    float(width_tiles * height_tiles))
                ConvertTile(layer.get_tile(False, row, col),
                            newLayer.get_tile(False, row, col), lut)
        # Update the new layer.
        newLayer.flush()
        newLayer.merge_shadow(True)
        newLayer.update(0, 0, layer.width, layer.height)
        # Remove the old layer.
        image.remove_layer(layer)
        newLayer.name = layerName
        # Update display and finish undo group.
        gimp.displays_flush()
        pdb.gimp_image_undo_group_end(image)
Beispiel #7
0
def stickerify_bordure(image, current_layer, black_grow=3, white_grow=12, shadow=True, canvas_increase=0, resize=False):
    def duplicate_layer():
        copy = current_layer.copy()
        image.add_layer(copy)
        # copy is added above so we want to go down a bit
        image.active_layer = current_layer
        return copy

    def fill_black():
        pdb.gimp_edit_bucket_fill(current_layer, 1, 0, 100, 255, 0, 0, 0)

    def fill_white():
        pdb.gimp_edit_bucket_fill(current_layer, 0, 0, 100, 255, 0, 0, 0)

    def set_colors():
        pdb.gimp_context_set_foreground((255, 255, 255))
        pdb.gimp_context_set_background((0, 0, 0))

    pdb.gimp_context_push()
    pdb.gimp_image_undo_group_start(image)

    # clean selection to avoid bugs
    pdb.gimp_selection_none(image)

    set_colors()

    # resize early to avoid compressing the bordure
    if resize:
        width, height = image.width, image.height

        if width == height:
            new_width, new_height = 512, 512
        elif width > height:
            new_width, new_height = 512, int(height * (512.0 / width))
        elif width < height:
            new_width, new_height = int(width * (512.0 / height)), 512

        pdb.gimp_image_scale(image, new_width, new_height)

    if canvas_increase:
        width, height = image.width, image.height

        width_increase = int(width * (canvas_increase / 100))
        height_increase = int(height * (canvas_increase / 100))

        pdb.gimp_image_resize(image,
                              width + width_increase,
                              height + height_increase,
                              int(width_increase / 2),
                              int(height_increase / 2))

        pdb.gimp_layer_resize_to_image_size(current_layer)

    duplicate_layer()

    # alpha to selection
    pdb.gimp_image_select_item(image, 0, current_layer)

    pdb.gimp_selection_grow(image, black_grow)
    fill_black()

    second_layer = duplicate_layer()

    pdb.gimp_selection_grow(image, white_grow)
    fill_white()

    if shadow:
        duplicate_layer()

        fill_black()

        current_layer.translate(8, 8)

        pdb.gimp_selection_all(image)
        pdb.plug_in_gauss(image, current_layer, 20, 20, 0)
        pdb.gimp_layer_set_opacity(current_layer, 70)

    pdb.gimp_image_merge_down(image, second_layer, 0)

    if shadow:
        pdb.gimp_image_merge_down(image, image.active_layer, 0)

    pdb.gimp_layer_set_name(image.active_layer, "Sticker bordure")

    pdb.gimp_selection_none(image)

    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_context_pop()

    pdb.gimp_displays_flush()
Beispiel #8
0
def stickerify_bordure(image,
                       tdrawable,
                       black_grow=3,
                       white_grow=12,
                       shadow=True):
    def duplicate_layer():
        copy = current_layer.copy()
        image.add_layer(copy)
        # copy is added above so we want to go down a bit
        image.active_layer = current_layer
        return copy

    def fill_black():
        pdb.gimp_edit_bucket_fill(current_layer, 1, 0, 100, 255, 0, 0, 0)

    def fill_white():
        pdb.gimp_edit_bucket_fill(current_layer, 0, 0, 100, 255, 0, 0, 0)

    def set_colors():
        pass

    pdb.gimp_context_push()
    pdb.gimp_image_undo_group_start(image)

    pdb.gimp_context_set_foreground((255, 255, 255))
    pdb.gimp_context_set_background((0, 0, 0))

    set_colors()

    current_layer = image.active_layer

    duplicate_layer()

    # alpha to selection
    pdb.gimp_image_select_item(image, 0, current_layer)

    pdb.gimp_selection_grow(image, black_grow)
    fill_black()

    second_layer = duplicate_layer()

    pdb.gimp_selection_grow(image, white_grow)
    fill_white()

    if shadow:
        duplicate_layer()

        fill_black()

        current_layer.translate(8, 8)

        pdb.gimp_selection_all(image)
        pdb.plug_in_gauss(image, current_layer, 20, 20, 0)
        pdb.gimp_layer_set_opacity(current_layer, 70)

    pdb.gimp_image_merge_down(image, second_layer, 0)

    if shadow:
        pdb.gimp_image_merge_down(image, image.active_layer, 0)

    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_context_pop()

    pdb.gimp_displays_flush()
Beispiel #9
0
def import_text_layers(
        image,
        active_layer,
        source_path,
        page_index,
        source_escaped,
        font,
        fontunit_index,
        font_size,
        antialias,
        hintstyle,
        font_color,
        justification,
        indent,
        letter_spacing,
        line_spacing,
        box_mode,
        language,
        use_markdown):
    """Import text on path (function)."""
    # special character for page jumps : u"\u2003"
    # special character for supspension marks : u"\u2026"
    # special character like WORD page jump, UTF-8 (...), etc... to be replaced
    substitutions = {u"\u2003": '', u"\u2026": '...', ' \n': '\n'}

    # Read file
    source = read_file(source_path)

    # Split pages
    pages_array = splitpages(source)
    # Clean working page
    page = replace(pages_array[int(page_index) - 1], substitutions)
    # Split into lines
    text_lines = page.splitlines()

    pdb.gimp_image_undo_group_start(image)

    layer_position = 0
    layer_height = 3056

    if active_layer:
        layer_height = pdb.gimp_image_height(image)
        layer_position = pdb.gimp_image_get_layer_position(image, active_layer)

    n_points, cpoints = get_path_points(image)

    # indexes in cpoints array
    x_index = 2
    y_index = 3

    flag_message = False

    for rawtext in text_lines:
        if source_escaped:
            text = rawtext.decode('unicode_escape')
        else:
            text = rawtext

        if text != '//' and text != '' and text != '// ':

            tlayer_width, tlayer_height = get_box_dim(layer_height, len(text))

            x_pos, y_pos, flag = get_box_position(n_points, x_index, y_index,
                                                  cpoints, layer_height)

            if flag and not flag_message:
                pdb.gimp_message('Attention !!!\n'
                                 'Le nombre de points que vous avez '
                                 'selectionné ne correspond pas au nombre '
                                 'de lignes de texte du fichier.\n'
                                 'Le script va continuer en placant les '
                                 'lignes de texte restantes en bas '
                                 'de la page.\n'
                                 'Vous devriez vérifier vos bulles, ou '
                                 'annuler pour recommencer, en pointant '
                                 'toutes les bulles et encarts de texte.')
                flag_message = True

            add_text_layer(image, text, font, font_size, fontunit_index,
                           layer_position, antialias, font_color, indent,
                           justification, hintstyle, language, letter_spacing,
                           line_spacing, tlayer_width, tlayer_height,
                           x_pos, y_pos)

            # cpoints structure is so that a point is made of 6 values
            x_index += 6
            y_index += 6
    # end of loop on rawtext

    pdb.gimp_image_undo_group_end(image)
    return  # end of function
Beispiel #10
0
def simple_plugin(filename_background, filename_foreground, filename_result):

    nameProcedure = "simple_plugin"

    sleep_timer = 0

    counter = 1

    print("%s : Enter" % (nameProcedure))

    # ----------------------------------------------------------------------------------------------
    # Get the following from both the background and foreground image files;
    #
    #   - image
    #   - drawable
    # ----------------------------------------------------------------------------------------------

    image_background = pdb.gimp_file_load(filename_background,
                                          filename_background)
    drawable_background = pdb.gimp_image_get_active_layer(image_background)

    image_foreground = pdb.gimp_file_load(filename_foreground,
                                          filename_foreground)
    drawable_foreground = pdb.gimp_image_get_active_layer(image_foreground)

    display_diagnostics(counter, image_background, image_foreground,
                        drawable_background, drawable_foreground)

    counter = counter + 1

    gimp.progress_init("Have got image and drawable from both files")
    time.sleep(sleep_timer)

    # ----------------------------------------------------------------------------------------------
    # Start a GIMP Undo group, as this will allow the actions of this Plugin to be undone in one
    # step.
    # ----------------------------------------------------------------------------------------------

    pdb.gimp_image_undo_group_start(image_background)

    # ----------------------------------------------------------------------------------------------
    # Copy the foreground image.
    # ----------------------------------------------------------------------------------------------

    copy_result = pdb.gimp_edit_copy(drawable_foreground)

    print("%s : Copy result = %s" % (nameProcedure, str(copy_result)))

    gimp.progress_init("Have copied foreground image into Buffer")
    time.sleep(sleep_timer)

    # ----------------------------------------------------------------------------------------------
    # Paste the foreground image onto the background image.
    # ----------------------------------------------------------------------------------------------

    use_gimp_edit_paste_as_new = False

    if use_gimp_edit_paste_as_new:

        pdb.gimp_edit_paste(drawable_background, True)

    else:

        image_background_new = pdb.gimp_edit_paste_as_new(
            drawable_background, True)

        if (image_background_new == -1):

            print(
                "%s : Attempted to paste from the Edit buffer, but it appears to be empty."
                % (nameProcedure))

            exception_message = "\n\nAn Exception has been raised by the method;\n\n  " + \
                                nameProcedure + \
                                "\n\nThis method attempted to paste from the Edit buffer, but it appears to be empty.\n\nAs a result, this Plugin is about to terminate!"

            raise Exception(exception_message)

    gimp.progress_init(
        "Have pasted foreground image from Buffer onto background image")
    time.sleep(sleep_timer)

    display_diagnostics(counter, image_background, image_foreground,
                        drawable_background, drawable_foreground)

    counter = counter + 1

    # ----------------------------------------------------------------------------------------------
    # Flatten the modified background image down into one layer.
    # ----------------------------------------------------------------------------------------------

    drawable_background = pdb.gimp_image_flatten(image_background)

    gimp.progress_init("Have flattened the background image")
    time.sleep(sleep_timer)

    display_diagnostics(counter, image_background, image_foreground,
                        drawable_background, drawable_foreground)

    # ----------------------------------------------------------------------------------------------
    # Save the background image into a specified file.
    # ----------------------------------------------------------------------------------------------

    if use_gimp_edit_paste_as_new:

        pdb.gimp_file_save(image_background, drawable_background,
                           filename_result, filename_result)

    else:

        pdb.gimp_file_save(image_background_new, drawable_background,
                           filename_result, filename_result)

    gimp.progress_init("Have saved the background image")
    time.sleep(sleep_timer)

    # ----------------------------------------------------------------------------------------------
    # End the GIMP Undo group which was started at the beginning of this Plugin.
    # ----------------------------------------------------------------------------------------------

    pdb.gimp_image_undo_group_end(image_background)

    # ----------------------------------------------------------------------------------------------
    # Close the GIMP images now that we have finished with them, otherwise they will use up memory
    # nnecessarily.
    # ----------------------------------------------------------------------------------------------

    pdb.gimp_image_delete(image_foreground)
    pdb.gimp_image_delete(image_background)

    print("%s : Exit" % (nameProcedure))