I'm trying to write a script to detect whether an image has a white border, and, if so, apply a black border around it.
Doing a bit of research in the forum as to how to get the color value of a pixel, I found this:
http://www.imagemagick.org/Usage/quantize/#extract
My initial attempt would be to write a script that got the pixel dimensions of the image, then use these dimensions to iterate through each pixel around the entire border and determine whether it is white. But I'm wondering if there is a smarter/faster way to do this kind of analysis on an image?
Any suggestions?
Detect whether an image has a white border?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detect whether an image has a white border?
cut out the borders on each side and test if pure white. Try this
# create white border around rose: image
convert rose: -bordercolor white -border 2 rose_white_border.png
# test if white border
infile="rose_white_border.png"
ww=`convert $infile -ping -format "%w" info:`
hh=`convert $infile -ping -format "%h" info:`
ww1=$((ww-1))
hh1=$((hh-1))
left=`convert $infile[1x${hh}+0+0] -scale 1x1! -format "%[fx:u]" info:`
top=`convert $infile[${ww}x1+0+0] -scale 1x1! -format "%[fx:u]" info:`
right=`convert $infile[1x${hh}+${ww1}+0] -scale 1x1! -format "%[fx:u]" info:`
bottom=`convert $infile[${ww}x1+0+${hh1}] -scale 1x1! -format "%[fx:u]" info:`
test=`convert xc: -format "%[fx:($left+$top+$right+$bottom)==4?1:0]" info:`
[ $test -eq 1 ] && echo "pure white border" || echo "not pure white border"
Note the above is Unix script. For windows users, see http://www.imagemagick.org/Usage/windows/
# create white border around rose: image
convert rose: -bordercolor white -border 2 rose_white_border.png
# test if white border
infile="rose_white_border.png"
ww=`convert $infile -ping -format "%w" info:`
hh=`convert $infile -ping -format "%h" info:`
ww1=$((ww-1))
hh1=$((hh-1))
left=`convert $infile[1x${hh}+0+0] -scale 1x1! -format "%[fx:u]" info:`
top=`convert $infile[${ww}x1+0+0] -scale 1x1! -format "%[fx:u]" info:`
right=`convert $infile[1x${hh}+${ww1}+0] -scale 1x1! -format "%[fx:u]" info:`
bottom=`convert $infile[${ww}x1+0+${hh1}] -scale 1x1! -format "%[fx:u]" info:`
test=`convert xc: -format "%[fx:($left+$top+$right+$bottom)==4?1:0]" info:`
[ $test -eq 1 ] && echo "pure white border" || echo "not pure white border"
Note the above is Unix script. For windows users, see http://www.imagemagick.org/Usage/windows/
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Detect whether an image has a white border?
To see if an image has a white border, do the following.
First add a 1 pixel white border around the image. this sets the color to look for.
Now use -trim and output the resulting virtual pixel offset and final image size.
If the resulting image is smaller than the original, the image has a white border.
The offset and size will let you know how big that border is.
See Cutting and Bordering, Trim, the 'Auto-Crop' Operator
http://www.imagemagick.org/Usage/
You can add a -fuzz factor to let you match 'near white' colors
http://www.imagemagick.org/Usage/color_basics/#fuzz
and Trimming 'Noisy' Images -- Scanned Images
http://www.imagemagick.org/Usage/crop/#trim_noisy
First add a 1 pixel white border around the image. this sets the color to look for.
Now use -trim and output the resulting virtual pixel offset and final image size.
If the resulting image is smaller than the original, the image has a white border.
The offset and size will let you know how big that border is.
See Cutting and Bordering, Trim, the 'Auto-Crop' Operator
http://www.imagemagick.org/Usage/
You can add a -fuzz factor to let you match 'near white' colors
http://www.imagemagick.org/Usage/color_basics/#fuzz
and Trimming 'Noisy' Images -- Scanned Images
http://www.imagemagick.org/Usage/crop/#trim_noisy
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/