Command line to batch resize images to specific size

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
MattRothschild
Posts: 3
Joined: 2017-09-15T11:08:48-07:00
Authentication code: 1151

Command line to batch resize images to specific size

Post by MattRothschild » 2017-09-15T11:13:09-07:00

Hi all,

Thanks for the resource and great work here.

I am looking for some guidance on how to batch resize a collection of images to 72KB while maintaining their WxH aspect ratio.

I have looked at a few options and can % resize files but I do not see an option to set a specific desired file size. As each of my images have different contents they range in size from 1.5MB to 3MB+ but I would like to resize them all to 72KB.

Any suggestions on a command line I can use for this?

Many thanks.

snibgo
Posts: 9385
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Command line to batch resize images to specific size

Post by snibgo » 2017-09-15T11:38:41-07:00

KB is short for kilobytes, so I suppose you want to limit your filesize, by varying the image quality. Correct? Then I suppose your output format is JPEG? Then you would use "-define jpeg:extent=72KB". See http://www.imagemagick.org/script/comma ... php#define
snibgo's IM pages: im.snibgo.com

MattRothschild
Posts: 3
Joined: 2017-09-15T11:08:48-07:00
Authentication code: 1151

Re: Command line to batch resize images to specific size

Post by MattRothschild » 2017-09-15T11:50:03-07:00

That is great .. thank you ... that seemed to do the trick.

Is there a way I can also add a rescale command line to that so that I can reduce the WxH to 800 x 800? All of my images are already square so the aspect ratio will stay the same but would be ideal to reduce to 800 x 800 and max file size of 72KB?

Any suggestions on this command line apprecaited!

Many, many thanks

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Command line to batch resize images to specific size

Post by fmw42 » 2017-09-15T11:53:57-07:00

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://http://www.imagemagick.org/scrip ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

snibgo
Posts: 9385
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Command line to batch resize images to specific size

Post by snibgo » 2017-09-15T11:54:54-07:00

If your image is larger than 800x800, "-resize 800x800" will make it fit into a box of that size. So if it is square, it will finish up as exactly 800x800.

As you haven't told us what version of IM you use nor your script language, I can't suggest a command.
snibgo's IM pages: im.snibgo.com

MattRothschild
Posts: 3
Joined: 2017-09-15T11:08:48-07:00
Authentication code: 1151

Re: Command line to batch resize images to specific size

Post by MattRothschild » 2017-09-15T12:01:34-07:00

Thank you for the guidance ... that worked perfectly!

I am using IM 707-2 and using Windows 10 Command line.

One last question ... now that you have so expertly given me those commands is there a way to do this as a batch command on several hundred files in multiple recursive directories?

Many thanks - this is very very helpful!

snibgo
Posts: 9385
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Command line to batch resize images to specific size

Post by snibgo » 2017-09-15T12:05:42-07:00

Put it in a "for" loop. See "help for".
snibgo's IM pages: im.snibgo.com

rootx
Posts: 8
Joined: 2017-09-13T12:53:42-07:00
Authentication code: 1151

Re: Command line to batch resize images to specific size

Post by rootx » 2017-09-16T11:31:48-07:00

Without bla bla bla...
You need this.

Code: Select all

For /R "C:\Users\rootx\Desktop\jpgfolder\" %G IN (*.jpg) do  -resize 800x800 "%G" >> "C:\Users\root\Desktop\jpgfolder\log.txt"

snibgo
Posts: 9385
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Command line to batch resize images to specific size

Post by snibgo » 2017-09-16T11:58:42-07:00

In the code shown by rootx, "do" should be followed by a valid CMD command. "-resize" isn't a valid CMD command. A "convert" command cold be used. That needs an input and output file, which can be the same name. For example:

Code: Select all

for /R "myfolder" %G in (*.jpg) do convert %G -resize 800x800 -define jpeg:extent=72KB %G
snibgo's IM pages: im.snibgo.com

Post Reply