-1

Basically, I have a image with a thick yellow border around it something like NetGeo image. I want to check if there is border or not (NetGeo is just an example, border color can be of any width and any color), I want to detect that and shave it if it exists.

I see this question is been answered in Trim whitespace using PIL for PIL but I am looking for same solution in imagemagick with a single command or java

enter image description here

But I don't want to crop the image if there is not solid border.

For example enter image description here

I don't want to remove the yellow color as it is not a solid lined border. Hope it helps to understand the question.

plzdontkillme
  • 1,497
  • 3
  • 20
  • 38

1 Answers1

1

This code seems to work in ImageMagick

convert NG.png -fuzz 20% -background "rgb(239,204,60)" -define trim:percent-background=0% -trim +repage x.png

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thank for the answer. I really appreciate, this works well if I know the color, what if I don't know the color of the border? My main task is to know if the image has a border or not or only trim if the image has order. This fails if the the image doesn't have one. – plzdontkillme Jan 09 '21 at 03:43
  • Just measure the color at the top left corner of the image as `convert logo: -format "%[pixel:p{0,0}]" info:` – fmw42 Jan 09 '21 at 05:54
  • Thanks @fmw42 above command helps me to find the color and remove it from the image but, it also crops if the there is not solid border. I just want to crop if only there is a border like the one in NP. Let me update the question to help – plzdontkillme Jan 12 '21 at 00:07
  • First crop the input on each side how ever many pixels wide you want (even just 1 or 2 pixel). Then -append the top and bottom and the 90 deg rotated left and right into one image. Then append each of the 3 channel into one grayscale channel and get the average ( -format "%[fx: mean>0?0:1]" info:). So if the result is 0, it has a border. I the result is 1, it does not have a border. Or you can set a threshold for the border test as -format "%[fx: mean>threshold?0:1]" info:v – fmw42 Jan 12 '21 at 04:04
  • Thanks for reply again, m not very versed with imagemagic but will give it a try – plzdontkillme Jan 13 '21 at 03:07
  • Let me know if you have problems with my suggestions. I have not had time to verify that they work. – fmw42 Jan 13 '21 at 19:12