Reputation: 377
I have images like this. White border and a polygonal-ish swatches-like slanted-sides "inset" pictures in the middle with always four sides, although of varying side-angles, sizes and proportions
(Please note that these (examples) images are not the real images but I've made them to illustrate the exact sources I'll be working with.)
(The thick gray outlines are NOT part of the images below. I've added the gray outline just to make the original images stand out against stackoverflow pages' white background)
I want to crop out the sides (including the outside white)...
... so that I end up with a perfect largest possible straight-sided cropped rectangle from inside, and with no white around it, like so:
I need to do this for images which will have different irregular insets, like these examples, in both portrait and landscape orientation.
(Other images will have different slants and the amount of white background border will vary, BUT will always have only four corners). So I'd need an adaptive, dynamic solution.
I was thinking, could it be possible by calculating white pixels of the four corners somehow, then cropping the sides one by one using the corner coordinates. The borders will be white, but the images are all jpegs so some "fuzz" percentage might be needed.
I've tried to do it by calculating white pixels from corners, but am struggling at how or what should be my steps in achieving that.
Can it be done?
(I'm on windows, with ImageMagick 7).
Upvotes: 0
Views: 231
Reputation: 53089
You can do that in Imagemagick (6.9.12-54 or higher) using a special define for -trim.
(Some bugs have been found and fixed for -trim, so use the latest version of Imagemagick)
Image:
convert bubbles.jpg -background white -define trim:percent-background=0% -trim +repage result.jpg
Result
Upvotes: 2