Page 1 of 2
Crop out white part of image?
Posted: 2015-02-18T03:47:00-07:00
by ultranerds
Hi,
I'm trying to "crop" the white part out of images, like so:
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
Andy
Re: Crop out white part of image?
Posted: 2015-02-18T05:41:29-07:00
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
Re: Crop out white part of image?
Posted: 2015-02-18T05:45:42-07:00
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
Re: Crop out white part of image?
Posted: 2015-02-18T05:54:54-07:00
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.
Re: Crop out white part of image?
Posted: 2015-02-18T06:30:56-07:00
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
Re: Crop out white part of image?
Posted: 2015-02-18T10:23:23-07:00
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).
Re: Crop out white part of image?
Posted: 2015-02-18T11:28:56-07:00
by ultranerds
Hi,
Sorry, not sure what you mean? I tried it - and it looks fine to me:
TIA
Andy
Re: Crop out white part of image?
Posted: 2015-02-18T11:32:38-07:00
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.
Re: Crop out white part of image?
Posted: 2015-02-18T11:37:47-07:00
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
Re: Crop out white part of image?
Posted: 2015-02-18T11:42:55-07:00
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?
Re: Crop out white part of image?
Posted: 2015-02-18T11:47:09-07:00
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
Now to just make my code do this with every image =)
Thanks everyone for the quick help!
Cheers
Andy
Re: Crop out white part of image?
Posted: 2015-02-18T11:54:34-07:00
by snibgo
I have ringed the dark pixels on the left.
Re: Crop out white part of image?
Posted: 2015-02-18T12:00:34-07:00
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.
Re: Crop out white part of image?
Posted: 2015-02-18T12:15:22-07:00
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
Re: Crop out white part of image?
Posted: 2015-02-18T12:41:14-07:00
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?