Page 1 of 2
Support required: Cropping Operation
Posted: 2008-07-24T12:46:03-07:00
by jasonhoward
Hello together,
i am working on an image processing automation tool for a printshop - having vast difficultes getting to a satisfying result with an IM -crop operation.
I have researched for hours, but i just can not get it right. I guess the problem is on my side - i am pretty certain, most people can tell me the correct command right away.
The goal is to resize and crop a given image to fit into a specific area on a template. If the resized image does not perfectly fit the target area, it must be cropped (in the vertical and horizontal center) so that it fits.
I use those commands:
Code: Select all
convert files/afoch/UZ4V0121.jpg -resize 236x236 -crop 236x236+0+0 files/afoch/UZ4V0121.jpg_resized.jpg
composite -geometry +0+0 "files/afoch/UZ4V0121.jpg_resized.jpg" "files/afoch__processed/UZ4V0121.jpg" "files/afoch__processed/UZ4V0121.jpg"
The result is always the same: the source image gets resized to fit either height or width of the area placed into. It does not get cropped. Therefore i always and up with my target image to contain the source image, but also thick white borders.
I would appreciate any help on that matter!
There is two more things i wondered about:
1.) The image quality of the result is not satisfying even though it never gets enlarged but rather shrinked and even though the source image is Hi-Resolution. I tried using -quality 100 -depth 8, but the result remains the same. Why?
2.) Is there a possibility to merge the two commands i use into one, so that imagemagick has not got to be launched twice - therefore saving time and cpu.
Thank you very much!
Re: Support required: Cropping Operation
Posted: 2008-07-24T12:56:37-07:00
by fmw42
Re: Support required: Cropping Operation
Posted: 2008-07-24T13:06:13-07:00
by Bonzo
The old forum seems a bit slow tonight
Your quality problems comes from saving the tempory images as jpg so getting one lot of jpg compression then saving the final images as jpg again so another lot of jpg compression. Save the tempory images a png or similar.
Try:
Code: Select all
convert ( files/afoch/UZ4V0121.jpg -resize 236x236 -gravity center -crop 236x236+0+0 +repage ) -gravity center "files/afoch__processed/UZ4V0121.jpg" +swap -composite "files/afoch__processed/UZ4V0121.jpg"
Re: Support required: Cropping Operation
Posted: 2008-07-24T13:19:30-07:00
by jasonhoward
fmw42: i have tried while reading through the IM docs - with no success. Adding +repage gives me exactly the same results as not using it at all
Thanks though. Maybe you have some other idea about what i could do?
Bonzo: Thank you! That sounds clear. I will do that.
Re: Support required: Cropping Operation
Posted: 2008-07-24T13:30:27-07:00
by Bonzo
Maybe you could also add -trim to your command:
Code: Select all
convert ( files/afoch/UZ4V0121.jpg -resize 236x236 -gravity center -crop 236x236+0+0 -trim ) -gravity center "files/afoch__processed/UZ4V0121.jpg" +swap -composite "files/afoch__processed/UZ4V0121.jpg"
Re: Support required: Cropping Operation
Posted: 2008-07-24T13:38:27-07:00
by jasonhoward
Bonzo: Thanks for the hint. I tried it, but it resulted in the target image containing the source image, centered, in the middle, but not stretched and then cropped to fill the whole area. Instead, now i have white borders on the left and on the right
Re: Support required: Cropping Operation
Posted: 2008-07-24T13:52:27-07:00
by Bonzo
I am afraid I do not understand fully what you are looking to do then; can you post an image of the final result you are after?
Re: Support required: Cropping Operation
Posted: 2008-07-24T14:01:46-07:00
by jasonhoward
Certainly.
This is my source image:
This is the result of my current code, based on the imagemagick command you posted before:
Code: Select all
convert files/afoch/UZ4V0121.jpg -resize 236x236 -gravity center -crop 236x236+0+0 -trim files/afoch/UZ4V0121.jpg_resized.jpg
And this is, what it should result in:
Re: Support required: Cropping Operation
Posted: 2008-07-24T14:31:15-07:00
by Bonzo
I have to go now and will have a look tomorrow; but I do not see why you needed the compose step.
You may need to use a ^ see
http://redux.imagemagick.org/script/com ... php#resize
What version of IM are you using ?
Re: Support required: Cropping Operation
Posted: 2008-07-24T14:36:57-07:00
by jasonhoward
The compose step i need, because i layer multiple variants of the same image onto a blank one, in certain sizes and positions. For example to create a photo strip.
The case mentioned above, is just a test image. In my processing tool, the user can create templates, defining at where - in a given area - pictures get placed, as well as what sizes they are.
A template looks like this:
I am using IM 6.2.8 10/22/06 Q16
Re: Support required: Cropping Operation
Posted: 2008-07-24T15:13:47-07:00
by el_supremo
Bonzo's right. For your example image, the resize should be 236x236^
Pete
Re: Support required: Cropping Operation
Posted: 2008-07-24T15:24:09-07:00
by jasonhoward
I tried it, but it results in the same image as above (2nd example, with the white borders left and right)
I used this:
Code: Select all
convert ( files/afoch/UZ4V0121.jpg -resize 236x236^ -gravity center -crop 236x236+0+0 -trim ) -gravity center files/afoch__processed/UZ4V0121.jpg +swap -composite files/afoch__processed/UZ4V0121.jpg
Re: Support required: Cropping Operation
Posted: 2008-07-24T16:06:21-07:00
by el_supremo
Aha. I've just been (re)reading Anthony's IM examples about resizing and he notes that the ^ modifier was added in IM 6.3.8-3 and you've indicated that you're using IM 6.2.8.
You'll have to upgrade IM to be able to use ^
Anthony has a workaround for versions earlier than 6.3.8-3 which might work for you until you can upgrade.
http://www.imagemagick.org/Usage/resize/#space_fill
Only problem with it is that you need to know beforehand if the image is landscape or portrait.
Pete
Re: Support required: Cropping Operation
Posted: 2008-07-24T17:29:00-07:00
by fmw42
Your problem is that you are trying to resize to a DIFFERENT aspect ratio than your original image. IM -resize cannot do that. It will always resize to the same aspect ratio, by either using the larger dimension (default) or using the smaller dimension (^). To do what you want, you need to use the ^ option with -resize so that it will scale the smaller dimension (width) to 236 and make the larger dimension (height) bigger than 236. Then you can follow up with -crop, but will need to center the crop as you will not want to start at the top left corner. Thus you will need to use the -gravity center command and do +repage to reset the virtual canvas.
convert 1.jpg -resize 236x236^ -gravity center -crop 236x236+0+0 +repage 2.jpg
( I have verified with your image that the above does work)
You will need to have IM 6.3.8-3 or higher to use ^ with -resize.
Otherwise, you will need to do some calculations to figure out how to set -resize to generate the image you want before cropping.
I built a Unix script (called aspect) to do that before the inclusion of ^ for -resize. So it is not IM version limited. It will allow the option of cropping or padding when the desired image has a different aspect ratio than the original.
You can either use the script or see how I programmed it, if you want to make use of the calculation on you own. It is available at
http://www.fmwconcepts.com/imagemagick/index.html
( I have verified with your image that the script produces the same result as above)
Re: Support required: Cropping Operation
Posted: 2008-07-24T22:15:48-07:00
by anthony
It is a shame your IM is so old. You don't even have the new -extent general image size adjustment operator. This operator will pad and crop images to fit the size given.
Remember as a replacement for -extent you can use either a -border -crop technique, OR a -compose -composite technique, a variation of the latter is -resize -flatten, though that does not allow the use of -gravity.
However there are also three resize methods to consider...
default - Place whole image into area given
^ - Resize image to fill the given area
@ - Resize image to that same number of pixels
That last falls between the first too, so will need BOTH cropping
and padding.
All these methods are shown in IM Examples, Thumbnails.