I have a list of .JPG files on a Mac. I want to export them to a format taking less than 500 kilobytes per image. I know how to do that using the Preview application one image at a time; but I want to be able to do the same in batch, meaning on several files at once. Is there a command line way to do it so I could write a script and run it in the terminal? Or some other way that I could use?
Install ImageMagick. (Really.. it's lightweight and amazing) It's nice to install using Homebrew. Then...
cd [FilepathWithImages] && mogrify -define jpeg:extent=60kb -resize 400 *.JPG
du -sh *
to see the size of each file in the directory you're in.NOTE: *.JPG
must be uppercase for it to work
How this works:cd [yourfilepath]
will naviage to the directory you want to be in&&
is used for chaining commandsmogrify
is used when you want to keep the same filename-define jpeg:extent=60kb
sets the maximum filesize to 60kb-resize 400
will set the width*.JPG
is for all files in the directory you're in.
There are many additional commands you can use with imagemagick convert and mogrify. After installing it, you can use man mogrify
to see the commands you can chain to it.
According to the docs, "Restrict the maximum JPEG file size, for example -define jpeg:extent=400KB
. The JPEG encoder will search for the highest compression quality level that results in an output file that does not exceed the value. The -quality
option also will be respected starting with version 6.9.2-5. Between 6.9.1-0 and 6.9.2-4, add -quality 100
in order for the jpeg:extent
to work properly. Prior to 6.9.1-0, the -quality
setting was ignored."
This is an example from the command line using convert (brew info imagemagick
) converting all *.jpg
images in one directory to .png
:
$ for i in *.jpg; do
convert "$i" "${i%.jpg}.png"
done
To test before (dry-run) you could use echo
instead of the <command>
:
$ for i in *.jpg; do
echo "$i" "${i%.jpg}.png"
done
This will search for files within the directory having the extension .jpg
then execute the command convert
passing as arguments the file name $i
and then using as an output the same file name removing the extension and adding the new one .png
, this is done using:
"${i%.jpg}.png"
The use of double quotes "
is for the case file could contain spaces, check this for more details: shell parameter expansion
For example, to just change the quality of the file you could use:
convert "$i" -quality 80% "${i%.jpg}-new.jpg"
Or if no need to keep the original:
mogrify -quality 80% *.jpg
The main difference is that ‘convert‘ tends to be for working on individual images, whereas ‘mogrify‘ is for batch processing multiple files.
Install ImageMagick from Homebrew or MacPorts or from https://imagemagick.org/script/download.php#macosx. Then use mogrify to process all files in a folder using -define jpeg:extent=500KB saving to JPG.
I have two files in folder test1 on my desktop. Processing will put them into folder test2 on my desktop
Before Processing:
mandril.tif 3.22428MB (3.2 MB)
zelda.png 726153B (726 KB)
cd
cd desktop/test1
mogrify -path ../test2 -format jpg -define jpeg:extent=500KB *
After Processing:
mandril.jpg 358570B (359 KB)
zelda.jpg 461810B (462 KB)
See https://imagemagick.org/Usage/basics/#mogrify
The * at the end means to process all files in the folder. If you want to restrict to only jpg then change it to *.jpg. The -format means you intend the output to be jpg.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With