Page 1 of 1

Detect transparent borders

Posted: 2019-01-10T20:50:24-07:00
by rime
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.

This one has transparent border:
https://i.imgur.com/qGV3Mb1.png
This one has a white border and should not be detected as border by IM:
https://i.imgur.com/p1v4EOm.png

Would PixelIterator be more promising for this task ?

Re: Detect transparent borders

Posted: 2019-01-10T23:43:31-07:00
by fmw42
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.

Code: Select all

convert image.png -trim trimimage.png

Code: Select all

identify -verbose trimimage.png
...
Geometry: 503x963+0+0
...
Page geometry: 713x1188+105+112
...

Code: Select all

convert trimimage.png -format "%P%O" info:
713x1188+105+112

Code: Select all

convert image.png -format "%wx%h\n%@" info:
convert image.png -format "%wx%h\n%@" info:
713x1188
503x963+105+112

Re: Detect transparent borders

Posted: 2019-01-11T06:43:16-07:00
by snibgo
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.

Re: Detect transparent borders

Posted: 2019-01-11T08:19:24-07:00
by rime
@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.

Anyway, thank you for your response.

Re: Detect transparent borders

Posted: 2019-01-11T08:48:57-07:00
by snibgo
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.