Adding blurred border to image using GraphicsMagick

How can I add a border of 100px to all sides of a jpg that is made up of the perimeter pixels stretched out and blurred? e.g. the border to the left of the image is just columns 1-3 stretched horizontally and blurred together?

migrated from photo.stackexchange.comNov 8 '14 at 4:42

This question came from our site for professional, enthusiast and amateur photographers.

• I wonder if this question should be moved to some other stackexchange site as adding borders to images is not really photography... but I don't know what site – szulat Nov 8 '14 at 1:16

I would recommend GMIC instead, it is better suited for complex processing involving multiple images, and has more clear and extendable command syntax.

A simple script defining the blurborder command:

#@gmic blurborder : add_border>0,blur_pixels>=0
#@gmic : Add border created from stretched and blurred version of the entire image
blurborder :
border=${2=100} blur=${3={$border/2}} --blur[0]$blur
-resize[-1] {@{0,w}+2*$border},{@{-1,h}+2*$border}
-image[-1] [-2],$border,$border
-keep[-1]


Calling gmic to process picture.jpg to output.jpg:

gmic my_commands.gmic picture.jpg -blurborder 100 -o output.jpg


Stretching 3 pixels to 100 pixels border involves more operations (I create 8 intermediate images, then combine). Both definitions can be placed in the same 'my_commands.gmic' file.

#@gmic stretchborder : take_pixels>0,add_border>0,blur_pixels>=0
#@gmic : Add border pixels stretched and blurred
stretchborder :
pix=${1=3} border=${2=100}
blur=${3={$border/4}}
w=@{0,w}
h=@{0,h}
# top left
--crop[0] 0,0,{$pix-1},{$pix-1}
-resize[1] $border,$border,1,3,5
# top
--crop[0] {$pix-1},0,{$w-2*$pix+1},{$pix-1}
-resize[2] $w,$border,1,3,5
# top right
--crop[0] {$w-$pix},0,{$w-1},{$pix-1}
-resize[3] $border,$border,1,3,5
# right
--crop[0] {$w-$pix},$pix,{$w-1},{$h-$pix-1}
-resize[4] $border,$h,1,3,5
# bottom right
--crop[0] {$w-$pix},{$h-$pix},{$w-1},{$h-1}
-resize[5] $border,$border,1,3,5
# bottom
--crop[0] {$pix},{$h-$pix},{$w-$pix},{$h-1}
-resize[6] $w,$border,1,3,5
# bottom left
--crop[0] 0,{$h-$pix},{$pix-1},{$h-1}
-resize[7] $border,$border,1,3,5
# left
--crop[0] 0,{$pix-1},{$pix-1},{$h-$pix-1}
-resize[8] $border,$h,1,3,5
# 1 2 3   combine
# 8 0 4
# 7 6 5
{$w+2*$border},{$h+2*$border},@{0,d},@{0,s}
-image[9] [0],$border,$border
-image[9] [3],0,0
-image[9] [4],$border,0 -image[9] [3],{$w+$border},0 -image[9] [4],{$w+$border},$border
-image[9] [5],{$w+$border},{$h+$border}
-image[9] [6],$border,{$h+$border} -image[9] [7],0,{$h+$border} -image[9] [8],0,$border
-blur[9] $blur -image[9] [0],$border,\$border
-keep[9]


Usage:

gmic my_commands.gmic picture.jpg -stretchborder 3,100 -o output.jpg


• Wow, this looks perfect. I'll try it out. – OneSolitaryNoob Nov 8 '14 at 3:05
• i tried this and after a few hours i couldn't get gmic installed. maybe there's a way to do it in gm? – OneSolitaryNoob Nov 10 '14 at 8:22
• what platform? gmic should work everywhere and there is source available. anyway, i added another answer about graphicsmagick. – szulat Nov 10 '14 at 12:06

Added another answer, because this time i'm trying to use GraphicsMagick as originally requested.

Unfortunately, if we compared gmic to a spreadsheet, then gm is merely a calculator - you can do everything in gm, but it can be tedious :-(

(They are developing some form of scripting in gm but it is still not very useful. Of course there are other scripting languages available that can call imagemagick library but that's another subject)

Here is how you could use gm for a result similar to the first gmic script (resize and blur the whole image, then add the original in the center).

gm convert -scale 110%x110% -blur 70x70 input.jpg blurred.png
gm composite input.jpg blurred.png -gravity center output.jpg


notes:

• uses 2 gm calls (is it possible to combine it into just one? I don't know)
• creates "blurred.png" file as a side effect (you may want to delete it later)
• "110%x110%" means we add 5% border on each side, you can specify e.g. "1500x900" instead if you know the exact picture dimensions to get constant border thickness, there is probably no way to add 100 pixels automatically in gm
• implementing the exact behavior specified in the question (stretching 3 pixels border to 100 pixels) would involve even more temporary files but it is of course possible too (left as an exercise for the reader ;-))