can I auto-crop transperant edges off?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
brainlord

can I auto-crop transperant edges off?

Post by brainlord »

Using RGB+alpha PNG files.

most are 100 pixels square or so (some are taller)

problem: 1% of them are 500, 600 even 1024 pixels square with only the first 100 square pixels or so used (the top left corner). The rest is solid alpha.

I can manually crop to 100 square pixels, but that cuts stuff off.

Can IM automatically crop an image to a rectangle that throws away unused alpha space?

(this is similar to the "Crop to Mask" or "Crop to Selection" actions of photo editing programs)

aha
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: can I auto-crop transperant edges off?

Post by fmw42 »

brainlord wrote:Using RGB+alpha PNG files.

Can IM automatically crop an image to a rectangle that throws away unused alpha space?

(this is similar to the "Crop to Mask" or "Crop to Selection" actions of photo editing programs)

aha

Yes,

pad rose: image with transparency as test image:
convert rose: -bordercolor none -border 50 rose_a.png

trim transparency
convert rose_a.png -trim +repage rose_a_trim.png

If your image is not transparent all around, then pad by a few pixels all around as above, then use -trim.

See:
http://www.imagemagick.org/script/comma ... s.php#trim
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: can I auto-crop transperant edges off?

Post by anthony »

Adding a border around an image will ensure that when -trim is performed only the color of the added border will be trimmed.

Be sure you include a +repage to remove the trim virtual canvas information it leaves behind.

Question do you want all the images to be 100x100 when finished?

Also your original question implies that only the 100x100 at the top left is of interest. If so You can force the image to be 100x100 pixels, adding or removing the pixels to force them to be 100x100 but using -extent
http://www.imagemagick.org/Usage/crop/#extent

Code: Select all

   convert image.png -background none -extent 100x100  image_new.png
Note you can save the image back to its destination if you are sure this does what you want. Or you can process a whole list of such images using "mogrify".

Code: Select all

   mogrify -background none -extent 100x100 image_*.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply