Crop out white part of image?
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Crop out white part of image?
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
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?
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
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Re: Crop out white part of image?
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
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?
I wonder if your "white" is not totally white.
I am unable to try your images at the moment.
Code: Select all
convert input -bordercolor White -border 10x10 -fuzz 10 -trim output
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Re: Crop out white part of image?
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
TIA
Andy
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Crop out white part of image?
If you:
and look at s.png, you can easily see where the current border isn't white (at the bottom).
Code: Select all
convert steamusb.jpg -bordercolor White -border 10 s.png
snibgo's IM pages: im.snibgo.com
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Re: Crop out white part of image?
Hi,
Sorry, not sure what you mean? I tried it - and it looks fine to me:
TIA
Andy
Sorry, not sure what you mean? I tried it - and it looks fine to me:
TIA
Andy
- 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?
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
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.
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.
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Re: Crop out white part of image?
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:
..but I still get:
https://steampunkjunkies.net/daily/test6.jpg
Thanks!
Andy
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
https://steampunkjunkies.net/daily/test6.jpg
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?Your white is not pure white.
Thanks!
Andy
- 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?
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
Try -fuzz 40%
What version of IM and platform are you using?
This works just fine for me
Code: Select all
convert test4.jpg -fuzz 30% -trim +repage test6.jpg
What version of IM and platform are you using?
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Re: Crop out white part of image?
Ah - how weird! (about it not being pure white)
Thanks though - this worked:
Now to just make my code do this with every image =)
Thanks everyone for the quick help!
Cheers
Andy
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Crop out white part of image?
I have ringed the dark pixels on the left.
snibgo's IM pages: im.snibgo.com
- 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?
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
-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.
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
-
- Posts: 41
- Joined: 2009-02-12T02:05:15-07:00
Re: Crop out white part of image?
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
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
- 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?
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?
If your site needs the white padding, why are you trying to remove it?