def showWin(width, height):
    img = gimp.Image(width,height,RGB)
    lyr = gimp.Layer(img, 'layer1', width, height, RGB, 100, NORMAL_MODE) 
    lyr.fill(BACKGROUND_FILL)
    img.add_layer(lyr)
    gimp.Display(img)
    gimp.displays_flush()
Exemple #2
0
def ConvertColormap(image, lut):
    nbytes, colormap = pdb.gimp_image_get_colormap(image)
    max_progress = nbytes
    # Create empty colormap
    ncolomap = []
    # For progress bar
    progress = 0
    # Fill new colormap by converting from old colormap
    for ii in xrange(nbytes / 3):
        valshl1, valnor1, shad1, high1 = lut[colormap[3 * ii + 0]]
        valshl2, valnor2, shad2, high2 = lut[colormap[3 * ii + 1]]
        valshl3, valnor3, shad3, high3 = lut[colormap[3 * ii + 2]]

        if shad1 == shad2 == shad3 == True or high1 == high2 == high3 == True:
            ncolomap.append(valshl1)
            ncolomap.append(valshl2)
            ncolomap.append(valshl3)
        else:
            ncolomap.append(valnor1)
            ncolomap.append(valnor2)
            ncolomap.append(valnor3)
        progress = progress + 3
        gimp.progress_update(float(progress) / max_progress)
    # Activate the new colormap
    pdb.gimp_image_set_colormap(image, nbytes, ncolomap)
    gimp.displays_flush()
Exemple #3
0
def showWin(width, height):
    img = gimp.Image(width, height, RGB)
    lyr = gimp.Layer(img, 'layer1', width, height, RGB, 100, NORMAL_MODE)
    lyr.fill(BACKGROUND_FILL)
    img.add_layer(lyr)
    gimp.Display(img)
    gimp.displays_flush()
Exemple #4
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)
Exemple #5
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)
Exemple #6
0
    def on_goto(self, widget, to, update=False, index=0):
        """
        This method change the atual active frame to where the variable
        (to) indicate, the macros are (START, END, NEXT, PREV,POS,GIMP_ACTIVE)
        - called once per frame when is_playing is enabled.
        """
        self.layers_show(False)

        if update:
            self.frames[self.active].update_layer_info()

        if to == START:
            self.active = 0

        elif to == END:
            self.active = len(self.frames) - 1

        elif to == NEXT:
            i = self.active + 1
            if i > len(self.frames) - 1:
                i = 0
            self.active = i

        elif to == PREV:
            i = self.active - 1
            if i < 0:
                i = len(self.frames) - 1
            self.active = i
        elif to == POS:
            self.active = index

        elif to == GIMP_ACTIVE:
            if self.image.active_layer in self.image.layers:
                self.active = self.image.layers.index(self.image.active_layer)
                self.active = len(self.image.layers) - 1 - self.active
            else:
                self.active = 0

        self.layers_show(True)
        self.image.active_layer = self.frames[self.active].layer

        gimp.displays_flush()  # update the gimp GUI
Exemple #7
0
def imgarray_to_image(array, name):
    h, w, d = array.shape
    img = gimp.Image(w, h, image_base_type_map[d])
    imgarray_to_layer(array, img, name)
    gimp.Display(img)
    gimp.displays_flush()
Exemple #8
0
def create_wraps(
        src_image,  # type: gimp.Image
        box_width_mm,  # type: float
        box_height_mm,  # type: float
        box_depth_mm,  # type: float
        thickness_mm,  # type: float
        flap_size_mm,  # type: float
        inside_size_mm,  # type: float
        crop_mark_size_mm,  # type: float
        crop_mark_distance_mm  # type: float
):
    # type: (...) -> None
    """Creates two wrap images from a template image."""

    # Convert the dimensions from mm to px
    box_width = mm_to_px(box_width_mm)  # type: int
    box_height = mm_to_px(box_height_mm)  # type: int
    box_depth = mm_to_px(box_depth_mm)  # type: int
    thickness = mm_to_px(thickness_mm)  # type: int
    flap_size = mm_to_px(flap_size_mm)  # type: int
    inside_size = mm_to_px(inside_size_mm)  # type: int
    crop_mark_size = mm_to_px(crop_mark_size_mm)  # type: int
    crop_mark_distance = mm_to_px(crop_mark_distance_mm)  # type: int

    half_box_height = box_height // 2  # type: int
    half_box_height_plus_extra = \
        half_box_height + thickness + inside_size  # type: int

    # Coordinates in the source image
    src_xs, src_ys = template_coordinates(box_width, box_height,
                                          box_depth)  # type: int, int
    src_image_width = src_xs[-1] - src_xs[0]  # type: int
    src_image_height = src_ys[-1] - src_ys[0]  # type: int

    # Coordinates in the destination images
    dst_xs, dst_ys = wrap_coordinates(box_width, box_height, box_depth,
                                      thickness, inside_size, flap_size,
                                      crop_mark_size, crop_mark_distance)
    dst_image_width = dst_xs[-1] - dst_xs[0]  # type: int
    dst_image_height = dst_ys[-1] - dst_ys[0]  # type: int

    # Make sure we have the right dimensions
    if src_image.width != src_image_width or \
       src_image.height != src_image_height:
        gimp.message(
            "Template image has the wrong size. "
            "Expected %dpx x %dpx (%dmm x %dmm) "
            "but got %dpx x %dpx (%dmm x %dmm)." %
            (src_image_width, src_image_height, px_to_mm(src_image_width),
             px_to_mm(src_image_height), src_image.width, src_image.height,
             px_to_mm(src_image.width), px_to_mm(src_image.height)))
        return

    # Draw stuff onto both destination images in the same way
    def draw(dst_image, copy_and_rotate_definitions):
        """Copies regions from the input image to a wrap image."""

        dst_layer = gimp.Layer(dst_image, "Wrap", dst_image_width,
                               dst_image_height, gimpfu.RGB_IMAGE, 100,
                               gimpfu.NORMAL_MODE)  # type: gimp.Layer
        dst_layer.fill(gimpfu.WHITE_FILL)
        dst_image.add_layer(dst_layer, 0)

        # Add guides
        for x in dst_xs:  # type: int
            dst_image.add_vguide(x)
        for y in dst_ys:  # type: int
            dst_image.add_hguide(y)

        # Take the layers from the template and move and rotate them
        # into position
        for d in copy_and_rotate_definitions:
            pdb.gimp_progress_pulse()
            copy_and_rotate_rectangle(
                src_image,  # src_image
                d[0],  # src_x
                d[1],  # src_y
                d[2],  # src_width
                d[3],  # src_height
                dst_layer,  # dst_layer
                d[4],  # dst_x
                d[5],  # dst_y
                d[6],  # dst_corner
                d[7])  # rotation_angle

        # Copy strips from the sides to create the flaps on the front
        # and the back
        pdb.gimp_progress_pulse()
        copy_and_rotate_rectangle(dst_image, dst_xs[1], dst_ys[4],
                                  half_box_height_plus_extra, flap_size,
                                  dst_layer, dst_xs[5], dst_ys[4],
                                  Corner.BOTTOM_RIGHT, 90)

        pdb.gimp_progress_pulse()
        copy_and_rotate_rectangle(dst_image, dst_xs[1], dst_ys[6],
                                  half_box_height_plus_extra, flap_size,
                                  dst_layer, dst_xs[5], dst_ys[7],
                                  Corner.TOP_RIGHT, 270)

        pdb.gimp_progress_pulse()
        copy_and_rotate_rectangle(dst_image, dst_xs[6], dst_ys[4],
                                  half_box_height_plus_extra, flap_size,
                                  dst_layer, dst_xs[6], dst_ys[4],
                                  Corner.BOTTOM_LEFT, 270)

        pdb.gimp_progress_pulse()
        copy_and_rotate_rectangle(dst_image, dst_xs[6], dst_ys[6],
                                  half_box_height_plus_extra, flap_size,
                                  dst_layer, dst_xs[6], dst_ys[7],
                                  Corner.TOP_LEFT, 90)

        # Marks for cutting and folding
        draw_mark(dst_image, (Direction.UP, Direction.LEFT), dst_xs[4],
                  dst_ys[1], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.UP, ), dst_xs[5], dst_ys[1],
                  crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.UP, ), dst_xs[6], dst_ys[1],
                  crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.UP, Direction.RIGHT), dst_xs[7],
                  dst_ys[1], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.UP, Direction.LEFT), dst_xs[1],
                  dst_ys[4], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.UP, Direction.RIGHT), dst_xs[10],
                  dst_ys[4], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.DOWN, Direction.LEFT), dst_xs[1],
                  dst_ys[7], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.DOWN, Direction.RIGHT), dst_xs[10],
                  dst_ys[7], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.DOWN, Direction.LEFT), dst_xs[4],
                  dst_ys[10], crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.DOWN, ), dst_xs[5], dst_ys[10],
                  crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.DOWN, ), dst_xs[6], dst_ys[10],
                  crop_mark_size, crop_mark_distance)
        draw_mark(dst_image, (Direction.DOWN, Direction.RIGHT), dst_xs[7],
                  dst_ys[10], crop_mark_size, crop_mark_distance)

        pdb.gimp_selection_none(dst_image)

    # Define where from and where to we want to copy
    # Each line looks like this:
    # (src_x, src_y, src_width, src_height,
    #  dst_x, dst_y, dst_corner, rotation_angle)
    copy_and_rotate_definitions_top = (
        # Top
        (src_xs[1], src_ys[0], box_width, box_depth, dst_xs[5], dst_ys[4],
         Corner.TOP_LEFT, 0),
        # Left
        (src_xs[0], src_ys[1], box_depth, half_box_height_plus_extra,
         dst_xs[5], dst_ys[4], Corner.TOP_RIGHT, 90),
        # Front
        (src_xs[1], src_ys[1], box_width, half_box_height_plus_extra,
         dst_xs[5], dst_ys[7], Corner.TOP_LEFT, 0),
        # Right
        (src_xs[2], src_ys[1], box_depth, half_box_height_plus_extra,
         dst_xs[6], dst_ys[4], Corner.TOP_LEFT, 270),
        # Back
        (src_xs[3], src_ys[1], box_width, half_box_height_plus_extra,
         dst_xs[5], dst_ys[4], Corner.BOTTOM_LEFT, 180),
    )

    copy_and_rotate_definitions_bottom = (
        # Left
        (src_xs[0], src_ys[3] - half_box_height_plus_extra, box_depth,
         half_box_height_plus_extra, dst_xs[5], dst_ys[4], Corner.TOP_RIGHT,
         270),
        # Front
        (src_xs[1], src_ys[3] - half_box_height_plus_extra, box_width,
         half_box_height_plus_extra, dst_xs[5], dst_ys[1], Corner.TOP_LEFT, 0),
        # Right
        (src_xs[2], src_ys[3] - half_box_height_plus_extra, box_depth,
         half_box_height_plus_extra, dst_xs[6], dst_ys[4], Corner.TOP_LEFT, 90
         ),
        # Back
        (src_xs[3], src_ys[3] - half_box_height_plus_extra, box_width,
         half_box_height_plus_extra, dst_xs[5], dst_ys[10], Corner.BOTTOM_LEFT,
         180),
        # Bottom
        (src_xs[1], src_ys[3], box_width, box_depth, dst_xs[5], dst_ys[4],
         Corner.TOP_LEFT, 0),
    )

    with DefaultContext():
        dst_image_top = gimp.Image(dst_image_width, dst_image_height,
                                   gimpfu.RGB)  # type: gimp.Image
        with PausedUndo(dst_image_top):
            draw(dst_image_top, copy_and_rotate_definitions_top)
            gimp.Display(dst_image_top)

        dst_image_bottom = gimp.Image(dst_image_width, dst_image_height,
                                      gimpfu.RGB)  # type: gimp.Image
        with PausedUndo(dst_image_bottom):
            draw(dst_image_bottom, copy_and_rotate_definitions_bottom)
            gimp.Display(dst_image_bottom)
    gimp.displays_flush()
Exemple #9
0
def create_template(
        box_width_mm,  # type: float
        box_height_mm,  # type: float
        box_depth_mm  # type: float
):
    # type: (...) -> None
    """Creates an empty template image given the box size."""

    with DefaultContext():
        box_width = mm_to_px(box_width_mm)  # type: int
        box_height = mm_to_px(box_height_mm)  # type: int
        box_depth = mm_to_px(box_depth_mm)  # type: int

        xs, ys = template_coordinates(box_width, box_height, box_depth)
        image_width = xs[-1] - xs[0]  # type: int
        image_height = ys[-1] - ys[0]  # type: int

        # Create a template image with one transparent layer
        image = gimp.Image(image_width, image_height)  # type: gimp.Image

        with PausedUndo(image):
            layer = gimp.Layer(image, "Template", image_width, image_height,
                               gimpfu.RGBA_IMAGE, 100,
                               gimpfu.NORMAL_MODE)  # type: gimp.Layer
            image.add_layer(layer, 0)

            # Create guides
            for x in xs:  # type: int
                image.add_vguide(x)
            for y in ys:  # type: int
                image.add_hguide(y)

            # Fill the areas where the graphics go with white
            pdb.gimp_selection_none(image)
            pdb.gimp_progress_pulse()
            pdb.gimp_image_select_rectangle(image, gimpfu.CHANNEL_OP_ADD,
                                            xs[0], ys[1], image_width,
                                            ys[3] - ys[1])
            pdb.gimp_progress_pulse()
            pdb.gimp_image_select_rectangle(image, gimpfu.CHANNEL_OP_ADD,
                                            xs[1], ys[0], xs[2] - xs[1],
                                            image_height)
            pdb.gimp_edit_fill(layer, gimpfu.WHITE_FILL)
            pdb.gimp_selection_none(image)

            def put_text(text, left, right, top, bottom):
                """Puts some text in the center of a rectangle."""

                pdb.gimp_progress_pulse()
                text_size = DPI / 4  # type: int
                text_layer = pdb.gimp_text_layer_new(
                    image, text, "sans-serif", text_size,
                    gimpfu.PIXELS)  # type: gimp.Layer
                image.add_layer(text_layer, 0)
                move_drawable_to(text_layer, Corner.CENTER,
                                 (left + right) // 2, (top + bottom) // 2)
                pdb.gimp_image_merge_down(image, text_layer,
                                          gimpfu.CLIP_TO_BOTTOM_LAYER)

            put_text("TOP", xs[1], xs[2], ys[0], ys[1])
            put_text("LEFT", xs[0], xs[1], ys[1], ys[3])
            put_text("FRONT", xs[1], xs[2], ys[1], ys[3])
            put_text("RIGHT", xs[2], xs[3], ys[1], ys[3])
            put_text("BACK", xs[3], xs[4], ys[1], ys[3])
            put_text("BOTTOM", xs[1], xs[2], ys[3], ys[4])
            put_text(
                "Box width: %dmm (%dpx)\n"
                "Box height: %dmm (%dpx)\n"
                "Box depth: %dmm (%dpx)" %
                (box_width_mm, box_width, box_height_mm, box_height,
                 box_depth_mm, box_depth), xs[0], xs[1], ys[0], ys[1])

            gimp.Display(image)
    gimp.displays_flush()