crop image borders based on color

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
romero619

crop image borders based on color

Post by romero619 »

I think this might be impossible to do, but I'll ask anyway...

If I have an image (png) with a border/margin around its edges of unknown width and of a specific color, would it be possible to automatically crop the border based on color?

I.E., I have a 300x300px image, with a flower in the center of a white box/background, and then a 100px black margin all around the edges of the white area. I wouldnt know beforehand how wide the black margin is, so I would need to tell imagemagick "trim off all of the black border, leave just the white box/background with flower....

Is there anything like this available through imageMagick???
Please help, thanks...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop image borders based on color

Post by fmw42 »

see -trim and -fuzz at http://www.imagemagick.org/script/comma ... ptions.php

convert <image> -trim +repage <result>

convert <image> -fuzz xx% -trim +repage <result>

-fuzz allows the trim to match colors similar to those at the corners
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: crop image borders based on color

Post by anthony »

To -trim based on a specific color first add a -border of that color. The -fuzz will then match colors centered on that color.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply