Correct resize dimensions?
Correct resize dimensions?
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Correct resize dimensions?
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.
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.
Last edited by fmw42 on 2013-05-22T19:01:04-07:00, edited 2 times in total.
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Correct resize dimensions?
Do you mean this:
IM will do this for you with area@
Code: Select all
W' = W * sqrt(A'/(W*H)) = sqrt(A' * (W/H))
H' = H * sqrt(A'/(W*H)) = sqrt(A' * (H/W))
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Correct resize dimensions?
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.
http://www.imagemagick.org/Usage/resize/#pixel
Note this is a pixel count limit, not a 'nearest to that area'. Aspect ratio is preserved.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Correct resize dimensions?
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?
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.
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Correct resize dimensions?
IM can output the dimensions (or other information) to your script. These are handy because they can be placed into a larger convert command.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.
Code: Select all
convert in.png -format "%wx%h" info:
convert in.png -print "%wx%h\n" null:
Code: Select all
convert -size 200x200 xc:none logo: -gravity center -composite zzz.png
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Correct resize dimensions?
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Correct resize dimensions?
In IMv6 you can use -set option:size '...' to set the -size setting, and allows the use of percent escapes!GreenKoopa wrote:You can set gravity to position it as you need.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.Code: Select all
convert -size 200x200 xc:none logo: -gravity center -composite zzz.png
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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Correct resize dimensions?
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Correct resize dimensions?
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/
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Correct resize dimensions?
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.
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.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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Correct resize dimensions?
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
snibgo's IM pages: im.snibgo.com
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Correct resize dimensions?
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.
also -print still works as normal, only applying itself to the first image, and no EOL addition
Code: Select all
convert rose: -format "%wx%h\n" info:
Code: Select all
convert rose: -print "%wx%h\n" null:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/