The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.

I want to get bounding rect of image without its border. MagickTrimImage seems to remove the border automatically but does it regardless of the border color and does not return information about bounding box. I believe there was a function that was removed from IM - MagickGetImageBoundingBox, it would be helpful but still, I need it only if border color is fully transparent.

If you trim the image and do not clear the page information, then the trim geometry is available in the verbose information. Or it is available for the image.png in the string format %@ via command line. I do not know the API equivalent, but I suspect it is there and available for you.

BigNoub wrote:This one has a white border and should not be detected as border by IM:

When you want to trim a border but only if it is a particular colour (such as transparent black), the usual trick is to add a border with 1 of that colour, and then trim. You can do that in MagickWand.

An alternative technique is to change the four corners to your particular colour, and then trim. That might be faster in MagickWand.

@fmw42 Sadly that won't be helpful since I need to have full control over what color is considered as border. I've decided to iterate through pixels of the image and stop on y if any of the pixel in line is not transparent, repeated for all borders. I've done this logic previously with GDI+, it is much faster than IM but fails with bizarre image formats (uncommon bits per pixel etc.), whereas IM takes care of it and pixel alpha can be read with PixelGetAlpha. I believe MagickTrimImage should be expanded to trim not only based on fuzz parameter, but also compare to a specific color or alpha channel value if set.

@snibgo Color of the padding/border can be transparent black but also any RGB value (with alpha 0), so I guess a trick to add transparent black border before calling MagickTrimImage, could fail sometimes. I haven't tried, and probably won't since I have a working routine with pixel iteration.

rime wrote:Color of the padding/border can be transparent black but also any RGB value (with alpha 0), ...

"-background Black -alpha bckground" will make any fully-transparent pixel transparent black. This can be done in an API, of course.

But every operation like this needs a full pass over the image. For performance, handling special cases like this is usually better done within your own code, so that (with luck) you need only one pass over the image.