예제 #1
0
def draw_circle_2(img, layer, c_x, c_y, rayon, color):
    """ draw a plain circle on layer """
    start_x = c_x - rayon if c_x - rayon > 0 else 0
    start_y = c_y - rayon if c_y - rayon > 0 else 0
    width = rayon * 2
    height = rayon * 2
    print(start_x, start_y, c_x, c_y, width, height)

    # Make sure this layer supports alpha so we can write to each pixel's
    # alpha component
    layer.add_alpha()

    circle_layer = pdb.gimp_layer_new(img, layer.width, layer.height,
                                      RGBA_IMAGE, "circle", 100,
                                      LAYER_MODE_NORMAL)
    pdb.gimp_image_insert_layer(img, circle_layer, None, 0)

    source_region = layer.get_pixel_rgn(start_x, start_y, width + 1,
                                        height + 1, False, False)
    pixel_size = len(source_region[start_x, start_y])
    print("pixel_size:", pixel_size)

    region = circle_layer.get_pixel_rgn(start_x, start_y, start_x + width + 1,
                                        start_y + height + 1, True, True)

    pixels = array("B", "\x00" * (width * height * pixel_size))
    for y in xrange(0, height):
        for x in xrange(0, width):
            index = (x + width * y) * pixel_size
            # pixel = source_pixels[index: index + pixel_size]
            new_pixel = array('B', [color[0], color[1], color[2], 255])
            # Write the modified pixel out to our destination array
            xx = x - rayon
            yy = y - rayon
            a = xx * xx + yy * yy
            b = rayon * rayon
            if a <= b:
                pixels[index:index + pixel_size] = new_pixel

    # Copy the whole array into the writeable pixel region
    region[start_x:start_x + width,
           start_y:start_y + height] = pixels.tostring()

    # Write our changes back over the original layer
    circle_layer.flush()
    circle_layer.merge_shadow(True)
    circle_layer.update(start_x, start_y, width, height)

    # merge the circle layer and the background layer
    new_layer = merge_layer(img, layer, circle_layer)
    return new_layer
예제 #2
0
def add_black_outline(image, drawable, original_layer_position, width, height,
                      offx, offy):

    # make selection bigger
    steps = 3
    pdb.gimp_selection_grow(image, steps)

    # create new layer
    type = RGBA_IMAGE
    name = "text background"
    opacity = 100
    mode = NORMAL_MODE
    layer_textbg = pdb.gimp_layer_new(image, width, height, type, name,
                                      opacity, mode)

    position = original_layer_position + 1
    pdb.gimp_image_add_layer(image, layer_textbg, position)

    #offset new layer by info
    pdb.gimp_layer_set_offsets(layer_textbg, offx, offy)

    # select layer
    image.active_layer = layer_textbg

    # set FG Color to black
    foreground = (0, 0, 0)
    pdb.gimp_context_set_foreground(foreground)

    # fill selection with black
    fill_mode = FG_BUCKET_FILL
    paint_mode = NORMAL_MODE
    opacity = 100
    threshold = 0
    sample_merged = 0
    x = 0
    y = 0
    pdb.gimp_bucket_fill(layer_textbg, fill_mode, paint_mode, opacity,
                         threshold, sample_merged, x, y)

    # select the text layer and merge it to the black outline
    merge_layer = image.layers[original_layer_position]
    merge_type = EXPAND_AS_NECESSARY
    layer = pdb.gimp_image_merge_down(image, merge_layer, merge_type)
    return layer
예제 #3
0
def paste_in_new_image(image2, ilayer, width, height, offx, offy):
    # create a new layer in the second image
    type = RGBA_IMAGE
    name = "information text {}".format(ilayer + 1)
    opacity = 100
    mode = NORMAL_MODE
    layer_info = pdb.gimp_layer_new(image2, width, height, type, name, opacity,
                                    mode)

    position = 0
    pdb.gimp_image_add_layer(image2, layer_info, position)

    # paste image
    drawable = image2.layers[0]
    paste_into = True

    floating_sel = pdb.gimp_edit_paste(drawable, paste_into)
    pdb.gimp_floating_sel_anchor(floating_sel)

    # move to original visor position
    pdb.gimp_layer_set_offsets(drawable, offx, offy)
def sheet(image=None, cols=0):
    if not image:
        image = gimp.image_list()[0]
    if not cols:
        best = (1, 10000000)
        for cols in range(1, len(image.layers) + 1):
            rows = (len(image.layers) + (cols - 1)) // cols
            (sheet_width, sheet_height) = (cols * image.width,
                                           rows * image.height)
            sheet_aspect_ratio = sheet_width / sheet_height if sheet_width > sheet_height else sheet_height / sheet_width
            if sheet_aspect_ratio < best[1]:
                best = (cols, sheet_aspect_ratio)
        cols = best[0]
    file_path = "{}_sheet_{}_frames_{}_columns_{}x{}.png".format(
        pdb.gimp_image_get_filename(image)[:-4], len(image.layers), cols,
        image.width, image.height)
    gimp.progress_init("Save sheet as {}".format(file_path))
    rows = (len(image.layers) + (cols - 1)) // cols
    sheet = pdb.gimp_image_new(image.width * cols, image.height * rows, 0)
    try:
        sheet_layer = pdb.gimp_layer_new(
            sheet,
            sheet.width,
            sheet.height,
            1,  # type = RGBA-IMAGE
            "sprite sheet",
            100,  # opacity = 100 %
            0  # mode = LAYER-MODE-NORMAL-LEGACY
        )
        pdb.gimp_image_insert_layer(sheet, sheet_layer, None, 0)

        (row, col) = (0, 0)
        for (layer_index, layer) in enumerate(image.layers):
            pdb.gimp_selection_none(image)
            pdb.gimp_layer_resize_to_image_size(layer)
            pdb.gimp_edit_copy(layer)
            floating = pdb.gimp_edit_paste(sheet_layer, True)
            (left, top) = floating.offsets
            pdb.gimp_layer_translate(floating, col * image.width - left,
                                     row * image.height - top)
            pdb.gimp_floating_sel_anchor(floating)
            col += 1
            if col >= cols:
                col = 0
                row += 1
            gimp.progress_update(100 * (layer_index + 1) / len(image.layers))
        pdb.file_png_save(
            sheet,
            sheet_layer,
            file_path,
            None,
            True,  # interlace
            9,  # compression
            True,  # bkgd
            True,  # gama
            True,  # offs
            True,  # phys
            True,  # time
        )
        gimp.message("All frames saved as {}".format(file_path))
    finally:
        pdb.gimp_image_delete(sheet)
예제 #5
0
def halftone_gimp(img, layer, slide_density, slide_circle_size, work_size_flag,
                  work_size_width, revert_size_flag, black_strength,
                  slide_angle):
    # Obtain layer dimensions.
    width = layer.width
    height = layer.height
    print("original size:", width, height)

    print("density", slide_density)
    D = int(slide_density)

    print("circle size", "slide_circle_size")
    C = int(slide_circle_size)

    print("black strength", black_strength)
    B = int(black_strength)

    ANGLES = POSSIBLE_ANGLES[int(slide_angle) - 1]
    print("angles", ANGLES)

    # New image dimensions
    if work_size_flag is True:
        new_width = int(work_size_width)
        new_height = int(height * (new_width / float(width)))
        print("new_size:", new_width, new_height)
        scale_image(img, new_width, new_height)
    else:
        new_width = width
        new_height = height

    add_cmyk_layers(img, layer)

    halftone_layers = [None] * 4
    for k in range(0, 4):
        current_layer = find_layer(img, COMPONENT_STRING[k])
        rotate_layer(current_layer, ANGLES[k])
        halftone_layers[k] = pdb.gimp_layer_new(
            img, current_layer.width, current_layer.height, RGBA_IMAGE,
            COMPONENT_STRING[k] + " halftone", 100, MULTIPLY_MODE)

        pdb.gimp_image_insert_layer(img, halftone_layers[k], None, 0)
        pdb.gimp_drawable_fill(halftone_layers[k], FILL_WHITE)

        start = timer()
        halftone_layer(img, current_layer, halftone_layers[k], D, COLORS[k], C,
                       B)
        end = timer()
        print("halftone in ", end - start)
        rotate_layer(halftone_layers[k], -ANGLES[k])
        x0, y0 = pdb.gimp_drawable_offsets(halftone_layers[k])
        non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(img)
        print(x0, y0)
        print(x1, y1, x2, y2)
        xx = new_width / 2 - current_layer.width / 2
        yy = new_height / 2 - current_layer.height / 2

        # print xx, yy
        pdb.gimp_layer_set_offsets(halftone_layers[k], x0 + xx, y0 + yy)

    for i_layer in COMPONENT_STRING:
        del_layer = find_layer(img, i_layer)
        img.remove_layer(del_layer)

    for i_layer in img.layers:
        if i_layer.name.endswith("halftone"):
            i_layer.visible = False
            # if revert_size_flag is True:
            #    scale_image(img, width, height)
        else:
            i_layer.visible = False

    for i_layer in img.layers:
        if i_layer.name.endswith("halftone"):
            current_layer = pdb.gimp_layer_new(img, new_width, new_height,
                                               RGBA_IMAGE,
                                               "_" + i_layer.name + "_", 100,
                                               MULTIPLY_MODE)
            pdb.gimp_image_insert_layer(img, current_layer, None, 0)
            pdb.gimp_rect_select(img, 0, 0, new_width, new_height, 2, 0, 0)
            pdb.gimp_edit_copy(i_layer)
            new_layer = pdb.gimp_edit_paste(current_layer, True)
            pdb.gimp_floating_sel_anchor(new_layer)

    for i_layer in img.layers:
        if i_layer.name.endswith("halftone"):
            print(i_layer.name)
            img.remove_layer(i_layer)

    if revert_size_flag is True:
        scale_image(img, width, height)
예제 #6
0
def add_cmyk_layers(img, layer):
    """ decompose layer in cmyk components """
    # Obtain layer dimensions.
    width = layer.width
    height = layer.height

    # Make sure this layer supports alpha so we can write to each pixel's alpha
    # component
    layer.add_alpha()

    for k in range(0, 4):
        # Grab a pixel region (readonly)  covering the entire image and copy
        # pixel data into an array
        source_region = layer.get_pixel_rgn(0, 0, width, height, False, False)
        source_pixels = array("B", source_region[0:width, 0:height])
        pixel_size = len(source_region[0, 0])
        print("pixel_size", pixel_size)

        # Create component layer in the Image
        component_layer = pdb.gimp_layer_new(img, width, height, RGBA_IMAGE,
                                             COMPONENT_STRING[k], 100,
                                             LAYER_MODE_NORMAL)
        pdb.gimp_image_insert_layer(img, component_layer, None, 0)
        pdb.gimp_drawable_fill(component_layer, FILL_WHITE)

        # Create another region (writeable) and an array that can store all
        # our modified pixels
        component_region = component_layer.get_pixel_rgn(
            0, 0, width, height, True, True)
        component_pixels = array("B", "\x00" * (width * height * pixel_size))

        gimp.progress_init("getting " + COMPONENT_STRING[k] +
                           " component layer...")

        x = 0
        y = 0

        # Loop through every pixel in the image/layer
        for y in xrange(0, (height - 1)):
            for x in xrange(0, (width - 1)):
                gimp.progress_update(1.0 * y / height)
                source_index = (x + width * y) * pixel_size
                pixel = source_pixels[source_index:source_index + pixel_size]
                # Write the modified pixel out to our destination array
                component_pixel = pixel
                # intensity = 0
                if k == 3:
                    # black specific
                    intensity = pixel[k]
                    # intensity = int(round(pixel[0] * 0.2126 +
                    # pixel[1] * 0.7152 +
                    # pixel[2] * 0.0722))

                    # c_linear = (pixel[0] / 255.0) * 0.2126
                    # + (pixel[1] / 255.0) * 0.7152
                    # + (pixel[2] / 255.0) * 0.0722
                    # intensity = 12.92 * c_linear if c_linear <= 0.0031308 else 1.055 * \
                    # pow(c_linear, 1/2.4) - 0.055
                    # intensity = int(round(intensity*255.0))

                    intensity = int(
                        round(pixel[0] * 0.299 + pixel[1] * 0.587 +
                              pixel[2] * 0.114))
                else:
                    intensity = pixel[k]
                component_pixel[3] = (255 - intensity)
                component_pixel[0] = COLORS[k][0]
                component_pixel[1] = COLORS[k][1]
                component_pixel[2] = COLORS[k][2]
                component_pixels[source_index:source_index +
                                 pixel_size] = component_pixel

        # Copy the whole array into the writeable pixel region
        component_region[0:width, 0:height] = component_pixels.tostring()

        # Write our changes back over the original layer
        component_layer.flush()
        component_layer.merge_shadow(True)
        component_layer.update(0, 0, width, height)

    pdb.gimp_progress_end()
    pdb.gimp_displays_flush()