#!/usr/bin/env python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
from gimpfu import *
def py_twocolors_border(img, layer, border, inner_border, outer_color, inner_color, flatten):
# Begin of undo group
pdb.gimp_image_undo_group_start(img)
# Store foreground
fg_old = gimp.get_foreground()
# Find out smallest dimension of the image
smallest_dimension = img.height
if img.height > img.width:
smallest_dimension = img.width
# Calculate size of the borders
px_border = border / 100.0 * smallest_dimension
px_inner = inner_border / 100.0 * px_border
px_border = int(round(px_border))
px_inner = int(round(px_inner))
# Calculate size of the new image
px_image_width = int(img.width + 2*px_border)
px_image_height = int(img.height + 2*px_border)
gimp.progress_update(0.05)
# Resize image
img.resize(px_image_width, px_image_height, px_border, px_border)
gimp.progress_update(0.2)
# Create and add the border layer to the image
border_layer = gimp.Layer(img, "border", img.width, img.height, RGBA_IMAGE, 100, NORMAL_MODE)
img.add_layer(border_layer, 0)
gimp.progress_update(0.3)
# Fill layer with outer border color
gimp.set_foreground(outer_color)
pdb.gimp_edit_fill(border_layer, FOREGROUND_FILL)
gimp.progress_update(0.45)
# Make a selection the size of inner border and fill it
pdb.gimp_rect_select(img, (px_border - px_inner), (px_border - px_inner), (px_image_width - 2*(px_border - px_inner)), (px_image_height - 2*(px_border - px_inner)), 2, 0, 0) # this method is 8x faster than select_all and shrink !
gimp.set_foreground(inner_color)
pdb.gimp_edit_fill(border_layer, FOREGROUND_FILL)
gimp.progress_update(0.7)
# Delete the center of the layer (where the image must go)
pdb.gimp_rect_select(img, px_border, px_border, (px_image_width - 2*px_border), (px_image_height - 2*px_border), 2, 0, 0)
pdb.gimp_edit_clear(border_layer)
pdb.gimp_selection_none(img)
gimp.progress_update(0.95)
# Flatten image if needed
if flatten :
img.flatten()
gimp.progress_update(1)
# Set foreground to stored value
gimp.set_foreground(fg_old)
# End of undo group
pdb.gimp_image_undo_group_end(img)
register(
"python_fu_two_colors_border",
"Add a layer with a two colors border",
"Add a layer with a two colors border",
"Benjamin Graf",
"Benjamin Graf",
"2010",
"/Python-Fu/Borders/Two colors border",
"RGB*, GRAY*",
[
(PF_SPINNER, "border", "Size of the whole border (% of smallest image dimension)", 3.2, (0, 200, 0.05)),
(PF_SPINNER, "inner_border", "Size of the inner border (% of outer border)", 10.0, (0, 100, 0.5)),
(PF_COLOR, "outer_color", "Outer border color", (0,0,0)),
(PF_COLOR, "inner_color", "Inner border color", (255,255,255)),
(PF_TOGGLE, "flatten", "Flatten image ?", 0)
],
[],
py_twocolors_border)
main()