Calculating new image size using an imagemagick's format string and a width/height
In web apps, it's good practice to use the width and height attributes for img tags -- this allows the browser to render the page faster.
If I have the width and开发者_如何转开发 height of an image, and a imagemagick format string (i.e. '300>'), how can I get the size that that image would be resized to? I'm hoping that imagemagick has some hooks / functions that would allow me to get access to this information.
I don't want to resize or load the image into memory, I just want to know what the dimensions would be.
Essentially, I want the following function:
def get_image_size(width, height, format_string) # ... magic here return [new_width, new_height] end width, height = get_image_size(300, 400, '235x235!') # would return 235 and 235 width, height = get_image_size(300, 400, '700x700>') # would return 300 and 400 width, height = get_image_size(300, 400, '100x100') # would return 75 and 100
Using the following ImageMagick convert
command gives what you want. This should easily translate to whichever ImageMagick language API you choose.
$ convert -size 300x400 xc:white -resize '235x235!' info:- | cut -d\ -f3
235x235
$ convert -size 300x400 xc:white -resize '700x700>' info:- | cut -d\ -f3
300x400
$ convert -size 300x400 xc:white -resize '100x100' info:- | cut -d\ -f3
75x100
精彩评论