So if I start with an image that looks like this:
what i want to do is to trim out as much whitespace as possible but i want to maintain the aspect ratio and i want the photos width/height to stay the same... so there might still be some whitespace at the end.
I know about the trim method, but that makes the photo smaller. I have tried to first capture the original images dimensions, then trim, then resize but that results in a smaller image since the white background is no longer added. So the photo above is 800x600, but then after resize it will be 719x600
I want to trim whitespace while keeping original width/height
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: I want to trim whitespace while keeping original width/height
What is your IM version and platform/OS? Please always provide that since syntax differs for both Windows vs Unix and for IM 6 vs IM 7.
After your -trim +repage, simple pad out to the desired size that gives you the correct aspect ratio. You can use -gravity center -background white -extent WxH, where W and H are the desired dimensions that achieve your aspect ratio. See http://www.imagemagick.org/Usage/crop/#extent
After your -trim +repage, simple pad out to the desired size that gives you the correct aspect ratio. You can use -gravity center -background white -extent WxH, where W and H are the desired dimensions that achieve your aspect ratio. See http://www.imagemagick.org/Usage/crop/#extent
Re: I want to trim whitespace while keeping original width/height
Now I am getting
I am using RubyonRails mini_magick gem with Carrierwave
Version: ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org
ubuntu
I am trying to write this code in ruby from the uploader.
all i need now is to get gravity center to work.
I am using RubyonRails mini_magick gem with Carrierwave
Version: ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org
ubuntu
I am trying to write this code in ruby from the uploader.
all i need now is to get gravity center to work.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: I want to trim whitespace while keeping original width/height
That may be an issue of your ancient IM 6.7.7.10 release. It is over 200 versions old. Do you need the resize or just the original trimmed image padded out.
This works for me in IM 6.9.8.6 Q16 Mac OSX without the resize. But you should test for the whether the original aspect is larger or smaller than the trimmed aspect.
Input:
aspect1=133.333; aspect2=115.044
Since the original aspect is larger than the trimmed aspect, one has to make the width larger by padding
aspect1=133.333; aspect3=133.186
Thus my final aspect ratio is the same (nearly) as the original.
This works for me in IM 6.9.8.6 Q16 Mac OSX without the resize. But you should test for the whether the original aspect is larger or smaller than the trimmed aspect.
Input:
Code: Select all
declare `convert -ping so8efd.jpg -format "ww=%w\nhh=%h" info:`
aspect1=`convert xc: -format "%[fx:100*$ww/$hh]" info:`
cropargs=`convert so8efd.jpg -fuzz 15% -format "%@" info:`
wc=`echo $cropargs | cut -d+ -f1 | cut -dx -f1`
hc=`echo $cropargs | cut -d+ -f1 | cut -dx -f2`
aspect2=`convert xc: -format "%[fx:100*$wc/$hc]" info:`
echo "aspect1=$aspect1; aspect2=$aspect2"
Since the original aspect is larger than the trimmed aspect, one has to make the width larger by padding
Code: Select all
wd=`convert xc: -format "%[fx:$aspect1*$hc/100]" info:`
convert so8efd.jpg -fuzz 15% -trim +repage -gravity center -background white -extent ${wd}x${hc} result.png
aspect3=`convert -ping result.png -format "%[fx:100*w/h]" info:`
echo "aspect1=$aspect1; aspect3=$aspect3"
Thus my final aspect ratio is the same (nearly) as the original.
Re: I want to trim whitespace while keeping original width/height
Thanks. I was able to figure it out and able to get it to work inside the rails uploader.
Hmm.. I just installed it using
sudo apt-get update
sudo apt-get install imagemagick
Hmm.. I just installed it using
sudo apt-get update
sudo apt-get install imagemagick
Re: I want to trim whitespace while keeping original width/height
this can be done with only a few simple lines of code using carrierwave/minimagick - very nice.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: I want to trim whitespace while keeping original width/height
This can be done pretty much in a single ImageMagick command. With IM 6.7.7-10 and working from the ubuntu bash shell on Windows 10 I can run this...
Code: Select all
convert input.jpg -write mpr:input -fill white -colorize 100 -fuzz 5% \
\( mpr:input -trim +repage \) \
-set option:orig "%[fx:(u.w/u.h<v.w/v.h)*s.w],%[fx:(u.w/u.h>v.w/v.h)*s.h]" \
-set option:dest "%[fx:(u.w/u.h<v.w/v.h)*u.w],%[fx:(u.w/u.h>v.w/v.h)*u.h]" \
-set option:distort:viewport "%[fx:u.w]x%[fx:u.h]" \
-virtual-pixel white -distort affine "0,0 0,0 %[orig] %[dest]" \
\( -clone 1 -trim +repage \) -delete 1 -gravity center -composite result.jpg
Note the "-fuzz 5%" – you might want to adjust that if the "-trim" takes off more or less white than you want.
If you're concerned about quality for the output JPG, you might add "-quality 100" right before the output image name at the end, or even use a less lossy output format like PNG.
I'd probably run that from a script and set variables for the "input.jpg" and "result.jpg" file names.
There would be less complicated methods to do the same thing using IM7.