Crop out white part of image?

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?".
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Crop out white part of image?

Post by ultranerds »

Hi,

I'm trying to "crop" the white part out of images, like so:

Image

The white part won't always be the same size wise / position (for example, some may have a little to the side, and a lot at the top/bottom - whereas others may not have any)

Any suggestions on how I can do this? I tried playing with the "trim" function, but that doesn't seem to do what I'm after

Thanks :D

Andy
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Crop out white part of image?

Post by Bonzo »

The trick is to add a white border to the image and then -trim; something like:

Code: Select all

convert input  -bordercolor White -border 10x10 -trim output 
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Crop out white part of image?

Post by ultranerds »

Hi Bonzo,

Thanks for the reply. I gave this a go:

convert test3.jpg -bordercolor White -border 10x10 -trim test4.jpg

However, it didn't seem to work :/

https://steampunkjunkies.net/daily/test4.jpg

I'm on ImageMagick 6.8.6-8 - would that make a difference?

For what its worth - the border part seems to work ok:

convert test3.jpg -bordercolor White -border 10x10 test5.jpg ===> https://steampunkjunkies.net/daily/test5.jpg

...but the command higher up, simply removes it - but not the rest of the white :(

Thanks!

Andy
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Crop out white part of image?

Post by Bonzo »

I wonder if your "white" is not totally white.

Code: Select all

convert input -bordercolor White -border 10x10 -fuzz 10 -trim output 
I am unable to try your images at the moment.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Crop out white part of image?

Post by ultranerds »

Thanks for the reply. It comes up as pure ffffff in GIMP, so pretty sure its white :) I tried it with the "fuzz" option, but no difference I'm afraid :(

TIA

Andy
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop out white part of image?

Post by snibgo »

If you:

Code: Select all

convert steamusb.jpg -bordercolor White -border 10 s.png
and look at s.png, you can easily see where the current border isn't white (at the bottom).
snibgo's IM pages: im.snibgo.com
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Crop out white part of image?

Post by ultranerds »

Hi,

Sorry, not sure what you mean? I tried it - and it looks fine to me:

Image

TIA

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

Re: Crop out white part of image?

Post by fmw42 »

Your white is not pure white, because JPG compression does not work well on large patches of solid color. This works for me with a larger fuzz value. IM 6.9.0.6 Q16 Mac OSX Snow Leopard

Code: Select all

convert test4.jpg -fuzz 30% -trim +repage resultimage

You need to add +repage, if you are going to use some other image format than jpg, such as png to remove the virtual canvas. It is not needed for jpg, but won't hurt to include it.

This was on your previous image. I am not sure why you just changed the example?

P.S. Typically you need to use -fuzz for trimming jpg images due to the compression changing values. Though I have not seen one that needed 30% before. It could be that there is some small gray spec on the border or interior to the white that requires a larger fuzz value to make it work past that region.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Crop out white part of image?

Post by ultranerds »

Hi,

Thanks for the reply. I'm using lots of different images, and those are the 2 examples I'm playing with :) (one has white to the edge, the other does not)

I just tried:

Code: Select all

convert test4.jpg -bordercolor white -border 1 -fuzz 30% -trim +repage test6.jpg
..but I still get:

https://steampunkjunkies.net/daily/test6.jpg
Your white is not pure white.
What are you seeing it as? In GIMP, I see it as ffffff, or 255,255,255 rgb .... which is as white as white gets, no? :)

Thanks!

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

Re: Crop out white part of image?

Post by fmw42 »

you do not need the border and border color. see my edited post above. I used a Mac tool called GraphicConvert and it showed values at places of 254. I suspect there is some gray spot on the border that is causing the problem and thus requires a larger fuzz value.

This works just fine for me

Code: Select all

convert test4.jpg -fuzz 30% -trim +repage test6.jpg
Try -fuzz 40%

What version of IM and platform are you using?
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Crop out white part of image?

Post by ultranerds »

Ah - how weird! (about it not being pure white)

Thanks though - this worked:

Code: Select all

convert test4.jpg -fuzz 30% -trim +repage test7.jpg
Image

Now to just make my code do this with every image =)

Thanks everyone for the quick help!

Cheers

Andy
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop out white part of image?

Post by snibgo »

I have ringed the dark pixels on the left.
Image
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop out white part of image?

Post by fmw42 »

Thanks, snibgo. I knew it was there somewhere, but did not find it.

If this is a general issue with your image. Then you might want to shave off one row and column on each side of the image before using -trim. That might get rid of the problem areas so that a smaller fuzz value would work.


This works for me

Code: Select all

convert test4.jpg -shave 1x1 -fuzz 5% -trim +repage test6.jpg
-fuzz 5% is more the norm for jpg images. You could try smaller but 1% does not work. Better to error a little on the high side.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Crop out white part of image?

Post by ultranerds »

Thanks - I'll give the trim a go :)

These images are really for people sharing on social media. The images we use on our site, and fixed squares - thus the white background, as you can't have transparency in jpg's (right PITA!)

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

Re: Crop out white part of image?

Post by fmw42 »

Correct, jpg does not support transparency. But 8-bit pseudocolor PNG does support it as binary transparency as does GIF.

If your site needs the white padding, why are you trying to remove it?
Post Reply