Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use imagemagick to detect border color?

Tags:

imagemagick

I have to deal with a lot of images and I'm looking for a way to detect the border color. For example, take this image:

enter image description here

is it somehow possible to determine that the image has a white background? A workaround can be to extract a border of 1px and check if the color of that extracted border is equally white. But I don't think the shave-feature of ImageMagick supports this, right?

like image 468
Rogier Lommers Avatar asked Oct 28 '25 13:10

Rogier Lommers


2 Answers

There are lots of techniques...

  • you could get the top-left (or other) corner pixel value, or
  • all 4 corners and take the median, or
  • take a 1px wide slice off each side and select the one with smallest variance.

Let's make a test image 200x100:

convert xc:red xc:lime +append \( xc:blue xc:white +append \) -append -scale 200x100\! image.png

enter image description here

Here are some ways of getting the corner pixels:

# Top left pixel
magick image.png -format "%[hex:u.p{0,0}]\n" info:
FF0000

# Top right pixel
magick image.png -crop "1x1+%[fx:w-1]+0" -format "%[hex:u.p{0,0}]\n" info:
00FF00

# Bottom left pixel
magick image.png -crop "1x1+0+%[fx:h-1]+0" -format "%[hex:u.p{0,0}]\n" info:
0000FF

# Bottom right pixel
magick image.png -crop "1x1+%[fx:w-1]+%[fx:h-1]" -format "%[hex:u.p{0,0}]\n" info:
FFFFFF

Now let's look at the top, bottom, left and right edges for which we could use a better sample image:

convert -size 256x256 gradient:red-yellow image.png

enter image description here

# Top row mean and standard deviation
magick image.png +repage -crop x1\!+0+0 -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.333333,0

Standard deviation is zero, so all pixels are the same in this row.

# Bottom row mean and standard deviation
magick image.png +repage -crop "x1\!+0+%[fx:h-1]" -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.666667,0

Standard deviation is zero, so all pixels are the same in this row, but brighter (0.6666 vs 0.3333) because both the red and green pixels are on - making yellow.

# Left edge mean and standard deviation
magick image.png +repage -crop "1\!x+0+0" -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.5,0.0967909

There is some deviation down the edge because the colours are changing.

# Right edge mean and standard deviation
magick image.png +repage -crop "1\!x+%[fx:w-1]+0" -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.5,0.0967909
like image 129
Mark Setchell Avatar answered Oct 31 '25 10:10

Mark Setchell


In magick, you can use -trim to "auto-crop" an image


The -trim operator is very close to the -crop operator, however:

  • when using -crop you need to supply argument(s) specifying how much of the image to remove
  • the -trim operator attempts to remove any borders or edges of an image automatically by identifying where an image do not change in color or transparency -- i.e. it tries to removes the padding or 'boring bits' around an image.

magick.exe "logo.jpg" -trim "trimmed_logo.jpg"

enter image description here ➡️ enter image description here

NOTE: The original (larger) canvas size is retained by default, so you can access to the original dimensions in case you need to do any calculations or comparisons, etc. However, if you don't care about the original image dimensions, you can ignore these and reset the image dimensions by additionally including the "+repage" option...

magick.exe "logo.jpg" -trim +repage "trimmed_logo.jpg"

enter image description here ➡️ enter image description here


Credit: Answer based on -crop page (imagemagick.org) but have re-worded and made new set of demo images to try and explain this more clearly.

like image 42
Martin Avatar answered Oct 31 '25 11:10

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!