imagemagick: how to get image size after resize? - imagemagick

I'm resizing my image using imagemagick, however, I'm not specifying the exact size:
convert $filename -resize 1080000# -quality 100 $reduced_filename
it's resized within a bound, now the problem is, after resize, the size in the EXIF is no longer correct, and I don't have a clue of calculating the new size (e.g. percentage resize can let me calculate new size based on percentage).
So is there a way to get image size after resizing?
Thanks!

You can use ImageMagick's "convert" command to access the image dimensions with a command like this...
convert $filename -format "%[w]x%[h]" info:

Like this...
First create a start image 1024x768:
convert -size 1024x768 xc:black start.jpg
Now resize, and output new size info in one fell swoop:
convert start.jpg -resize 50% -format "%wx%h" -write info: result.jpg
512x384

Related

convert doesn't conserve size when scaling down and up

In order to make applying blur faster I'm first scaling my image down and then scale it back up:
convert - -scale 10% -blur 0x2.5 -resize 1000% RGB:-
This works most of the time but sometimes the output resolution is slightly different from the original input. Is there a way to force the pipeline to be size-preserving?
You should be able to access the original geometry of the image via %G, so you can do:
convert input.jpg -scale 10% -blur 0x2.5 -resize '%G!' RGB:-
If you are using Windows, you probably want "%G!" in double rather than single quotes.
If you are using v7 ImageMagick, replace convert with magick.
I think you are getting errors because if you take 10% of 72 pixels (say), you will get a whole number of pixels, i.e. 7 pixels and then when you scale back up by a factor of 10 you'll get 70 rather than your initial 72.
If you are using Imagemagick 7, you can do the following:
magick input.jpg -set option:wd "%w" -set option:ht "%h" -scale "%[fx:wd/10]x%[fx:ht/10]" -blur 0x2.5 -resize "%[fx:wd]x%[fx:ht]\!" RGB:-
This stores the input width and height. Then uses the stored values to scale by 1/10 of those dimensions, then does the blur, then resizes exactly back to the origin input size. Note the ! that forces the resize to the exact dimensions.
or simpler without storing the input width and height:
magick lena.jpg -scale "%[fx:w/10]x%[fx:h/10]" -blur 0x2.5 -resize "%[fx:w]x%[fx:h]\!" lena_x.jpg

How to resize an image to a certain height in ImageMagick 7?

I need to resize an image and the result image should have 300 pixels in height. The image must keep the aspect ratio of the original image.
To specify the height and keep the aspect ratio, use -resize x300
magick convert -resize x300 in.jpg out.jpg
For Windows users:
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" convert -resize x300 in.jpg out.jpg
(change the "7.0.8-Q16" with your own version of ImageMagick)
For more ways to specify the target size, see https://imagemagick.org/script/command-line-processing.php#geometry

ImageMagick convert format, crop and resize

I have a large number of research pdf figures, and I need to preform the following actions in ImageMagick:
convert all pdf to png
crop png from left/top corner x:334/y:244; from right/bottom corner x:214/y:340;
original size 2100x2100, cropped size 1552x1552 pixels
resize cropped png to 240x240 pixels
Here is how it should be cropped for point 2, the pink area is what I want to have:
I was only be able to get 1st action done with my knowledge:
mogrify -format png -density 300 -flatten *.pdf
How can I do the 2nd and 3rd actions please? And do I need to run three separated commands or could they be combined into one command?
I do not know what exact order you need for mogrify as I do not use it. I also do not know why you need flatten Try:
mogrify -format png -density 300 -crop 1552x1552+344+244 +repage -resize 240x240 *.pdf

Resize image based on memory size

I need to resize image based on memory.
What I am getting is following option in "convert" command.
-resize 800x600
-resize 25%
Is there any command like -
-resize 10KB
Thanks
Use this:
convert input.jpg -define jpeg:extent=300kb ... output.jpg
If you want a way to do something similar with Python, I wrote an answer that works pretty well here. It does a binary search for a JPEG quality that satisfies a maximum size requirement.

Resize image by length with image magick

I'm trying to resize a image by its length, the width should remain. Is sounds quite easy, but somehow i don't get it. I'm using it together with PHP via the command line.
I'm using ImageMagick 6.7.3-9
By reading the docs, i would do the following:
/usr/local/bin/mogrify -resize x200! myimg.png
That gives me:
mogrify: negative or zero image size `bbb.png' # error/resize.c/ResizeImage/2570.
My goal is, that if bbb.png is w:400 h:400 i can resize it to w:400 h:200.
mogrify -resize x200 //Height with new width
mogrify -resize 200 //Width with new height
mogrify -resize 200! //New Width, old height
Can someone explain me, what I am doing wrong? Thanks
Known broken, now fixed. Fix should be in ImageMagick-6.7.4-0 Beta available 2011-12-08.
I have not found your Image Geometry fromat -resize x200! in documentation. I think you should use
xheight
or
widthxheight!
I.e.: -resize x200 or -resize 400x200!.

Resources