Image not trimmed properly

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
kraftydevil
Posts: 10
Joined: 2017-05-01T22:24:31-07:00
Authentication code: 1151

Image not trimmed properly

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image not trimmed properly

Post 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.
snibgo's IM pages: im.snibgo.com
kraftydevil
Posts: 10
Joined: 2017-05-01T22:24:31-07:00
Authentication code: 1151

Re: Image not trimmed properly

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

Re: Image not trimmed properly

Post 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.
Post Reply