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?
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
For a dry run (test) you can 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"
Double quotes "
are used in case the filename contains spaces. Documentation for shell parameter expansion (search the docs for ${parameter%word}
).
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. (Really.. it's lightweight and amazing) It's nice to install using Homebrew. Then...
- Open terminal.
cd [FilepathWithImages] && mogrify -define jpeg:extent=60kb -resize 400 *.JPG
- Wait until the process is complete (may take a few minutes if you have many images)
- To check file sizes, try
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 commands
mogrify
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."
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.
DISCLAIMER: BE CAREFUL BECAUSE THE FOLLOWING SOLUTION IS A "DESTRUCTIVE" COMMAND, FILES ARE REPLACED WITH LOWER QUALITY DIRECTLY
Now that you have read my disclaimer, I would recommend to get cwebp that you can download here.
You will also need parallel sudo apt-get install -y parallel
and then I coined the following script:
parallel cwebp {} -resize 0 640 -m 6 -sns 100 -q 80 -preset photo -segments 4 -f 100 -o {} ::: *.jpg && /
find -name "*.jpg" | parallel 'f="{}" ; mv -- {} ${f:0:-3}webp'
640 is the resulting file height in pixels and 0 before means that the width will adapt to the ratio between width and height. I reduced quality to 80% (-q 80), you will not notice much difference.
The second line find all the files that have been converted but still have the wrong extension file (.jpg), so it removes the last 3 characters (jpg) and add webp instead.
I went from 5 Mb to about 50k per file (.jpg images were 4000x4000 pixels) , just saved 20 Gb of storage. I hope you enjoy it !
If you don't want to bother with webp format you can use the following instead (you will need to install imageMagick perhaps):
parallel convert {} -resize x640 -sampling-factor 4:2:0 -strip -quality 85 \
-interlace JPEG -colorspace RGB -define jpeg:dct-method=float {} ::: *.jpg
© 2022 - 2024 — McMap. All rights reserved.