Page 1 of 1

Correct resize dimensions?

Posted: 2013-05-22T17:58:39-07:00
by ggking7
How can I calculate the correct dimensions for resizing an image to a certain target surface area and maintain the correct aspect ratio? I'm on the road right now and don't have access to my code but it involves a square root and some arithmetic. The problem is sometimes either the width or height is off by 1 pixel. -resize corrects it but I need to know what the final dimensions will be.

Re: Correct resize dimensions?

Posted: 2013-05-22T18:49:56-07:00
by fmw42
Why would it be off by one?

You can resize automatically so that either the larger dimension of the image matches your rectangular target or your smaller dimension matches your rectangular target, if the aspects of the image and target are not the same. The former uses -resize WxH and the latter uses -resize WxH^.

In the first case you will need to pad the result some with some color. In the latter you will need to crop the image.

convert image -resize WxH -background somecolor -gravity center -extent WxH result

or

convert image -resize WxH^ -background somecolor -gravity center -crop WxH+0+0 +repage result
or
convert image -resize WxH^ -background somecolor -gravity center -extent WxH result


Perhaps you can explain further what you are really trying to do.

Re: Correct resize dimensions?

Posted: 2013-05-22T18:56:00-07:00
by GreenKoopa
Do you mean this:

Code: Select all

W' = W * sqrt(A'/(W*H)) = sqrt(A' * (W/H))
H' = H * sqrt(A'/(W*H)) = sqrt(A' * (H/W))
IM will do this for you with area@

Re: Correct resize dimensions?

Posted: 2013-05-22T19:37:27-07:00
by anthony
Also see examples of '@' resize geometry flag...
http://www.imagemagick.org/Usage/resize/#pixel

Note this is a pixel count limit, not a 'nearest to that area'. Aspect ratio is preserved.

Re: Correct resize dimensions?

Posted: 2013-05-22T20:41:29-07:00
by ggking7
I'm trying to resize an image to a pixel count limit and a maximum width and height, then resize the original image to a new pixel count limit equal to twice the actual pixel count of the previously resized image, and finally crop the second resized image to the same dimensions as the first resized image. I'm OK with using special IM conventions or manual arithmetic to do this. What would you recommend?

Re: Correct resize dimensions?

Posted: 2013-05-23T11:05:50-07:00
by ggking7
Should I just use the Image::Size perl module (for example) to get the dimensions of the first image so I can crop the second image to the same size? Otherwise I'm not sure how to crop the second image to the same dimensions that the first image was resized to.

Re: Correct resize dimensions?

Posted: 2013-05-23T12:14:01-07:00
by GreenKoopa
ggking7 wrote:Should I just use the Image::Size perl module (for example) to get the dimensions of the first image so I can crop the second image to the same size? Otherwise I'm not sure how to crop the second image to the same dimensions that the first image was resized to.
IM can output the dimensions (or other information) to your script. These are handy because they can be placed into a larger convert command.

Code: Select all

convert in.png -format "%wx%h" info:
convert in.png -print "%wx%h\n" null:
Alternatively, you can lay an image over another to get its canvas size. Here I do it with the bulit-in image logo: and a 200x200 blank canvas, as I don't have the images you are talking about. You can set gravity to position it as you need.

Code: Select all

convert -size 200x200 xc:none logo: -gravity center -composite zzz.png
I'll help you with the algebra if you would like. An example would help, as well as feedback on the equations I already offered.

Re: Correct resize dimensions?

Posted: 2013-05-23T16:23:25-07:00
by anthony
GreenKoopa wrote:IM can output the dimensions (or other information) to your script. These are handy because they can be placed into a larger convert command.

Which is why in IMv7 you can now use percent escapes not only in -set -format -print -anotate label: etc, but in just about ALL settings and operator arguments! Of course most percent escapes need at least one image in memory, and at this time %[fx:..] needs an image in memory, even though it may not need to reference an image. :-(

There are exceptions to this general percent escape usage.
* You can not use it in filenames (security reason) unless specially prepared separate to the filename (using %[filename:...] settings) .
* Also some settings such a -format -label which delay the expansion of percent escapes until they are actually used (or applied to a specific image).

The work continues when I get time.

Re: Correct resize dimensions?

Posted: 2013-05-23T16:30:13-07:00
by anthony
GreenKoopa wrote:You can set gravity to position it as you need.

Code: Select all

convert -size 200x200 xc:none logo: -gravity center -composite zzz.png
I'll help you with the algebra if you would like. An example would help, as well as feedback on the equations I already offered.
In IMv6 you can use -set option:size '...' to set the -size setting, and allows the use of percent escapes!

For example yesterday I put up a 'square image' example, which used this to generate a canvas to 'extent' an image. It is a handy 'stopgap' technique to use until IMv7 comes out of alpha development.

IM Examples, Thumbnails, Square Padding and Cropping
http://www.imagemagick.org/Usage/thumbnails/#square

Code: Select all

  convert thumbnail.gif \
         -set option:size '%[fx:min(w,h)]x%[fx:min(w,h)]' xc:none \
         +swap -gravity center -composite square_internal_2.gif

However getting back to the OP. You can also use percent escapes with -distort. and -resize is really just a faster (slightly different) version of -distort.
You can even 'automatically crop' the output image (make it a bit faster) using a distort output 'viewport'.

See Resize Images using Distort
http://www.imagemagick.org/Usage/distorts/#resize
And Distort Resizing
http://www.imagemagick.org/Usage/resize/#distort
and for cropping results, Distort Viewport
http://www.imagemagick.org/Usage/distor ... ort_viewpo

Re: Correct resize dimensions?

Posted: 2013-05-23T22:06:39-07:00
by ggking7
GreenKoopa, I checked my code and I use that same formula to calculate the height and width. I think I'm running into trouble when rounding to the nearest pixel for each dimension. Your example here works for me:

convert in.jpg -format "%wx%h" info:

Is that a good way to go for this? How can I use that to change the crop dimensions in a command like this:

convert in.jpg -colorspace RGB -resize "$resize_width"x"$resize_height" -colorspace sRGB -gravity center -crop "$crop_width"x"$crop_height"+0+0 +repage -quantize RGB -quality 90 out.jpg

Percent escapes sound handy but I haven't delved into that yet.

Re: Correct resize dimensions?

Posted: 2013-05-23T22:23:09-07:00
by fmw42
ggking7 wrote:GreenKoopa, I checked my code and I use that same formula to calculate the height and width. I think I'm running into trouble when rounding to the nearest pixel for each dimension. Your example here works for me:

convert in.jpg -format "%wx%h" info:

Is that a good way to go for this? How can I use that to change the crop dimensions in a command like this:

convert in.jpg -colorspace RGB -resize "$resize_width"x"$resize_height" -colorspace sRGB -gravity center -crop "$crop_width"x"$crop_height"+0+0 +repage -quantize RGB -quality 90 out.jpg

Percent escapes sound handy but I haven't delved into that yet.

If you are on Linux/Mac, then just put the calculation into a variable

dimensions=$(convert in.jpg -format "%wx%h" info:)

convert in.jpg -colorspace RGB -resize "$resize_width"x"$resize_height" -colorspace sRGB -gravity center -crop "${dimensions}"+0+0 +repage -quantize RGB -quality 90 out.jpg

see
http://www.imagemagick.org/script/escape.php
and
http://www.imagemagick.org/Usage/transform/#fx_escapes

with the fx_escapes, you can do computations, for example to get the area

area=$(convert image -format "%[fx:w*h]" info:)

You can do similar things in Windows, but I don't know how since I am not a Windows user. But see http://www.imagemagick.org/Usage/windows/

Re: Correct resize dimensions?

Posted: 2013-05-23T23:02:00-07:00
by GreenKoopa
ggking7 wrote:How can I calculate the correct dimensions for resizing an image to a certain target surface area and maintain the correct aspect ratio? I'm on the road right now and don't have access to my code but it involves a square root and some arithmetic. The problem is sometimes either the width or height is off by 1 pixel. -resize corrects it but I need to know what the final dimensions will be.
ggking7 wrote:I'm trying to resize an image to a pixel count limit and a maximum width and height, then resize the original image to a new pixel count limit equal to twice the actual pixel count of the previously resized image, and finally crop the second resized image to the same dimensions as the first resized image. I'm OK with using special IM conventions or manual arithmetic to do this. What would you recommend?
Back to your first two posts, I understand the gist but not concretely enough to help further. This sounds like a complex and unique cropping need, so a detailed example would really help clarify precisely how do you want images cropped, what is "off by 1 pixel", what cases need be considered, etc.

Re: Correct resize dimensions?

Posted: 2013-05-23T23:16:53-07:00
by snibgo
In Windows:

Code: Select all

for /F %i in ('convert.exe in.jpg -format "%wx%h" info:') do set DIMS=%i
convert in.jpg {blah} -crop %DIMS%+0+0 {blah} out.jpg

Re: Correct resize dimensions?

Posted: 2013-05-28T23:32:20-07:00
by anthony
There has bee a change in that -format no longer automatically provides the final end-of-line characters. that is if you specify -format, you may need to add your own EOL. the default -identify and info: handling is unchanged.

Code: Select all

convert rose: -format "%wx%h\n" info:
also -print still works as normal, only applying itself to the first image, and no EOL addition

Code: Select all

convert rose: -print "%wx%h\n" null: