Suppose an image is given consisting of a monochrome shape (polygon) and a transparent background; there is no transparent border around the shape; points or edges touching the image border. If such images are rotated the ratio of the image may increase and the shape is not any more touching the image borders.
Has IM a function that can directly and efficiently extract a shape from a transparent background (the method must be available in PerlMagick)?
Until now I use four subroutines that determine the distance from left, right, top, bottom to the rotated shape and use those values to compute the parameters for a crop-command but this is expensive because in principle row by row and column by column must be checked for non transparent pixels.
Or perhaps there is a special kind of rotation available: In http://www.imagemagick.org/Usage/distorts/#srt one can read: „One argument is just a simple rotation about the images center, basically producing a similar result to the older Rotate Operator, but without any image size increase.“ But this will cause problems by potentially cutting of pieces of the shape.
Thanks for any help!
extracting a shape from a transparent background efficiently
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: extracting a shape from a transparent background efficiently
I don't understand what you want. Does "-trim" or "-format %@" provide it? This tells you the four sides of the rectangle that encloses the shape.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: extracting a shape from a transparent background efficiently
If you rotate the image and give it a transparent background for the rotation, then as user snibgo said, you can use (the Perlmagick equivalent of ) -trim to remove all extra background to get the rotated shape so that it again touches the sides of the image. But are you also asking to unrotate the shape back to its original orientation? Can you post the before and after images to dropbox.com and put the URL here so we can see what you are trying to do?
Also please always provide your IM version and platform.
Also please always provide your IM version and platform.
Re: extracting a shape from a transparent background efficiently
Thaks for the answers. I only want to remove all extra transparent background after rotation. Version is: ImageMagick 6.7.8-7 2013-07-10 Q8
I looked in the description of the (parameterless) method:
Trim remove edges that are the background color from the image
and this means a usage like $image=>Trim();
I have not a dropbox account but I uploaded a "before" and "after" shape example here:
http://vi-anec.de/Alben/shape_before_rotation.png
http://vi-anec.de/Alben/shape_after_rotation.png
http://vi-anec.de/Alben/shape_after_trim.png
and this is exactly what I wanted!! Very helpfull! Thank you very much!
I looked in the description of the (parameterless) method:
Trim remove edges that are the background color from the image
and this means a usage like $image=>Trim();
I have not a dropbox account but I uploaded a "before" and "after" shape example here:
http://vi-anec.de/Alben/shape_before_rotation.png
http://vi-anec.de/Alben/shape_after_rotation.png
http://vi-anec.de/Alben/shape_after_trim.png
and this is exactly what I wanted!! Very helpfull! Thank you very much!