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 ?
Detect transparent borders
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detect transparent borders
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Detect transparent borders
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.BigNoub wrote:This one has a white border and should not be detected as border by IM:
An alternative technique is to change the four corners to your particular colour, and then trim. That might be faster in MagickWand.
snibgo's IM pages: im.snibgo.com
Re: Detect transparent borders
@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.
@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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Detect transparent borders
"-background Black -alpha bckground" will make any fully-transparent pixel transparent black. This can be done in an API, of course.rime wrote:Color of the padding/border can be transparent black but also any RGB value (with alpha 0), ...
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.
snibgo's IM pages: im.snibgo.com