Page 1 of 1
Cropping an image with some requirements
Posted: 2010-12-12T15:27:00-07:00
by adrianTNT
Hello
(I am kind of new to ImageMacick, only used GD before).
I am trying to crop an image but with certain options, I want to specify a fixed result size for all images regardless of their original size, like
120x90px and crop the image to that size and losing vertical or horizontal areas.
Is there a predefined command for this or it is something complicated?
Thanks in advance.
From PHP I was not able to call Imagick functions, but commands of this type work in PHP: exec("convert temp/ferrari.jpg ferrari.png");
Re: Cropping an image with some requirements
Posted: 2010-12-12T15:30:39-07:00
by Bonzo
See:
http://www.imagemagick.org/script/comma ... p#geometry
I have a lot of php information on my site.
Code: Select all
exec("input.jpg -gravity center -thumbnail 120x90! output.jpg");
Re: Cropping an image with some requirements
Posted: 2010-12-12T15:35:23-07:00
by adrianTNT
Something is missing right after exec like "convert", does it need a command there ?
Re: Cropping an image with some requirements
Posted: 2010-12-12T15:36:08-07:00
by Bonzo
Woops its not what you wanted !
Code: Select all
exec("convert input.jpg -gravity center -crop120x90+0+0 output.jpg");
Sorry I was doing it in a hurry before going to bed.
Re: Cropping an image with some requirements
Posted: 2010-12-12T15:45:46-07:00
by adrianTNT
edit: there was a missing space after "crop", anyway...
What I am trying is this result (for vertical images):
Edit: with my old GD solution I was testing to compare differences between old and new vertical sizes and see which side is more close to desired one and then resize the smaller one first (height or width) and then crop the other one.
I want to do this in ImageMagick because I think it has better image quality.
Re: Cropping an image with some requirements
Posted: 2010-12-12T17:51:02-07:00
by anthony
Everything seems to be going wrong with the previous solutions..
Let me just say....
Thumbnails, Cut the Image to Fit
http://www.imagemagick.org/Usage/thumbnails/#cut
That is use a '^' flag for a
-thumbnail operator and use
-extent to do the crop. both operators take the same 'final size' as there arguments.
Code: Select all
convert -define jpeg:size=240x180 input.jpg -thumbnail 120x90^ \
-gravity center -extent 120x90 cut_to_fit.gif
-thumbnail is just like
-resize but will handle larger images faster (unless the jpeg read does its job) and also junks any large profiles (eg
-strip) that may be present. If you want to keep the profiles replace it with
-resize
Re: Cropping an image with some requirements
Posted: 2010-12-12T17:51:58-07:00
by fmw42
This seems to work for me in command line on IM 6.6.6.4
convert 26612_138x200.jpg -gravity center -crop 120x90+0+0 +repage 26612_138x200_crop_120x90.jpg
Re: Cropping an image with some requirements
Posted: 2010-12-12T18:08:58-07:00
by adrianTNT
yes, that is correct, it crops to given size and centres it; but for my needs I want it to be like in second sample of this image:
Unless there is a command to directly do that, then I will do some php tests/condition to properly resize it like that before cropping it.
Re: Cropping an image with some requirements
Posted: 2010-12-12T20:00:52-07:00
by fmw42
I am confused. You have not changed the size of the image, so you cannot have 120x90 pixels which produce two different sized cropped images. Do you really mean you want the aspect ratio to be 120:90 = 4:3 and have that be the full size of the image or something like that.
If you want the latter, then there is no function in IM to do that in one command. But you can write a script to find the input image size, take the aspect ratio you want and figure out how big to make the crop so that it fill one or the other dimension.
Re: Cropping an image with some requirements
Posted: 2010-12-12T20:14:13-07:00
by adrianTNT
@fmw42: yes, I want the second one in that image, from any image size to produce 120x90 image but without simply displaying the center of the image, but as you said (calculate and then crop).
It helps to know that there is no direct way to do that, I made it by calculation and this code works nicely after testing around 6 different image sizes/proportions, etc:
Code: Select all
if(!function_exists("crop_image")){
function crop_image($original_file, $destination_file, $new_width = 120, $new_height = 90){
list($original_width,$original_height) = getimagesize($original_file);
// first test if difference between old/new width are larger than diff betwern old/new height
$width_difference = $original_width/$new_width;
$height_difference = $original_height/$new_height;
// resize width first if width difference < height difference or if differences are the same (old/new image have same proportions);
// resize width first if width difference is smaller than height difference
if($width_difference<=$height_difference){
$resize_first = "width";
}
// resize height first if height difference is smaller than width difference
if($height_difference<$width_difference){
$resize_first = "height";
}
if($resize_first == "width"){
exec("convert $original_file -resize ".$new_width."x -gravity center -crop ".$new_width."x".$new_height."+0+0 -quality 100 $destination_file");
} else {
exec("convert $original_file -resize x".$new_height." -gravity center -crop ".$new_width."x".$new_height."+0+0 -quality 100 $destination_file");
}
}
}
// crop_image("ferrari.jpg","ferrari_new.jpg",120,90);
BTW: imagemagick has a much better image quality than GD, I really like it
Re: Cropping an image with some requirements
Posted: 2010-12-13T11:46:19-07:00
by fmw42
I still don't understand your definition of 120x90. Is that pixels or aspect ratio.
Depending upon what you want to do, if you want the crop and resize or resize and crop, that can be done in one command without any calculations. Your examples have disappeared mostly, so it is hard to know exactly what you want to happen. Perhaps if you post your input and output image from your script, we could figure out what you need and see if there is a simple way to do that.
Anthony's example above resizes the image (using -thumbnail) to 120x90 with some left over in one dimension or the other and then center crops to 120x90.
Alternately, you could compute the area you need first in one command, then crop and then resize to 120x90 in a second command.
Re: Cropping an image with some requirements
Posted: 2010-12-15T18:03:51-07:00
by adrianTNT
The code I posted fits my needs but (just to clarify things...) I will try to explain again.
The 120x90px is the final image size that I want.
I wanted to resize any given image to this new size, but because some images might have different proportions I want to leave some margins out (crop) instead of distorting the image to fit the new proportions.
If there was a simpler direct solution instead of the above function feel free to post.
Thanks for the replies.
Re: Cropping an image with some requirements
Posted: 2010-12-15T21:01:40-07:00
by fmw42
So do you want to take an arbitrary subsection of an image and output it to 120x90 by resizing it? If so you must crop first then resize (using the special character ^), then crop again. The first crop gets the desired subsection of any size, the resize makes it 120x90 preserving aspect ratio so that the smallest dimension of the input will match the appropriate specified dimension leaving the larger dimension bigger. The last crop is used to crop to trim the excess and make it exactly 120x90 and will be a center crop.
Try this:
convert logo: -gravity center -crop 90x90+0+0 +repage -resize "120x90^" -gravity center -crop 120x90+0+0 +repage logo_tst1.png
Note logo: is a special internal IM image. See
http://www.imagemagick.org/script/forma ... tin-images
Note my first crop was simply designed to get the center area. But you can leave off the -gravity and specify any offsets you want.
It is still possible that I don't understand what you want to do. But the above is my best guess.