Page 1 of 1

Image not trimmed properly

Posted: 2019-01-24T05:51:28-07:00
by kraftydevil
Version: ImageMagick 6.9.10-7 Q16 x86_64

I'm using a basic trim command:

Code: Select all

convert image.png -trim +repage image_trimmed.png
It works on some images but not all of them and I'm not sure why.

Works:
Image

Doesn't work:
Image

What's going on here and how can I ensure the trim always works?

Re: Image not trimmed properly

Posted: 2019-01-24T06:09:02-07:00
by snibgo
If you don't use "-fuzz", then "-trim" will do nothing to edges that have different pixels. Your edges in th second image have different pixels -- they are mostly transparent white, but the top-left and bottom-right pixels are not.

You can either fix those pixels, or use "-fuzz 11%" etc.

Re: Image not trimmed properly

Posted: 2019-01-24T14:48:37-07:00
by kraftydevil
snibgo wrote: 2019-01-24T06:09:02-07:00 If you don't use "-fuzz", then "-trim" will do nothing to edges that have different pixels. Your edges in th second image have different pixels -- they are mostly transparent white, but the top-left and bottom-right pixels are not.

You can either fix those pixels, or use "-fuzz 11%" etc.
Yep - the northwest and southeast corners are marked.

fuzz values from 1%-100% weren't working for me so I made some modifications by painting the corners mentioned:

Code: Select all

widthMinus1 = image_width - 1
heightMinus1 - image_height - 1
convert image.png -fill transparent -draw 'color 0,0 point' \
-draw 'color widthMinus1,heightMinus1 point' image_painted_corners.png
convert image_painted_corners.png -trim +repage image_trimmed.png

Re: Image not trimmed properly

Posted: 2019-01-24T15:03:20-07:00
by fmw42
If you have plenty of room all around, you can just shave some number of pixels from all sides. Then if needed use -border to add back the same number of pixels of the background color.

Alternately, put a 1 pixel border around the image of the background color, do a transparent fuzzy flood fill and extract the alpha channel. Then use either -morphology or -connected-components to clean the alpha channel for small specks. Then shave off the one pixel and put the processed image back into the alpha channel of your input.