How to trim the transparent pixels from the image border?

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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to trim the transparent pixels from the image border?

Post by fmw42 »

This seems to work for me:

Create image with transparent border:
convert -size 100x100 xc:white white.png
convert white.png -bordercolor none -border 50x50 test.png

identify test.png
test.png PNG 200x200 200x200+0+0 DirectClass 16-bit 784b

Trim border:
convert test.png -trim +repage test2.png

identify test2.png
test2.png PNG 100x100 100x100+0+0 DirectClass 16-bit 494b
gotskill10
Posts: 17
Joined: 2007-06-02T14:01:34-07:00

Re: How to trim the transparent pixels from the image border?

Post by gotskill10 »

The current implementation of "trim" removes pixels from the edges of an image until it hits a different pixel. Nothing in the example makes it discriminate solely against transparent pixels. It merely just removes color/transparent pixels from the edges until it hits one of a different color code.

How would you call the command to trim *only* transparent pixels so that it wouldn't otherwise trim off color pixels under any circumstances.

This example would trim the image even if you filled it with a non-transparent border.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to trim the transparent pixels from the image border?

Post by fmw42 »

gotskill10 is right, -trim does not look for any particular color and works the way he said it does. the first user made that assumption, not I. I just provided a demo that it works for transparent borders.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to trim the transparent pixels from the image border?

Post by anthony »

To trim with a particular color, add a border of that color. The fuzz setting will then center on that color.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply