trim image whitespace but leave 30 pixel

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
abcplex
Posts: 3
Joined: 2013-10-07T02:05:10-07:00
Authentication code: 6789

trim image whitespace but leave 30 pixel

Post by abcplex »

I have found this page very help-full,
viewtopic.php?f=1&t=15675

This works great but I need to trim all but leave 30 pixel from top, bottom, left and right.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: trim image whitespace but leave 30 pixel

Post by Bonzo »

I would use -trim as normal and then add a 30px white border afterwards.
abcplex
Posts: 3
Joined: 2013-10-07T02:05:10-07:00
Authentication code: 6789

Re: trim image whitespace but leave 30 pixel

Post by abcplex »

The issue is that I don't want to apply this to all images. Only the images that have white spaces. BTW, how to do add 30 pixels to image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: trim image whitespace but leave 30 pixel

Post by Bonzo »

The issue is that I don't want to apply this to all images. Only the images that have white spaces.
This is the problem if you do not include all the information you probably will not get the answer you are looking for :)

Now you have two choices:

Are you looking to keep all the images the same size? If so you could use -extent to pad the trimed image out to the same size as the others.
In this case I have assumed the other images are 500x500

Code: Select all

convert input.jpg -background White -gravity center -extent 500x500 output.jpg
Otherwise you could use -border to add the 30 pixels

Code: Select all

convert input.jpg -bordercolor White -border 30x30 output.jpg
abcplex
Posts: 3
Joined: 2013-10-07T02:05:10-07:00
Authentication code: 6789

Re: trim image whitespace but leave 30 pixel

Post by abcplex »

I have lot of images. My background process is removing all the white spaces from all images. Now, I need to add 30px white border only to those images that have white spaces (not all images)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: trim image whitespace but leave 30 pixel

Post by fmw42 »

Can you post examples of your input and explain what you want to do? You can post an example to any free image hosting service and put a link to it here.

My guess is you want

convert your image -bordercolor white -border 1 -fuzz XX% -trim +repage resultimage

Adding the 1 pixel white border will the allow -trim to trim images with no white around them back to their original size, but will trim any image that has near-white on the border to get rid of all near-white borders.

If you want to limit the trim to 30 pixels and are on Linux/Mac or Windows with cygwind, see my script, limitedtrim at the link below.
Post Reply