DEV Community

loading...
Cover image for How to quickly batch resize, compress, and convert images with a Bash one-liner

How to quickly batch resize, compress, and convert images with a Bash one-liner

victoria profile image Victoria Drake Originally published at victoria.dev on ・1 min read

Part of my Hugo site continuous deployment workflow is the processing of 210 images, at time of writing.

Here’s my one-liner:

find public/ -not -path "*/static/*" \( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) -print0 | xargs -0 -P8 -n2 mogrify -strip -thumbnail '1000>' -format jpg

I use find to target only certain image file formats in certain directories. With mogrify, part of ImageMagick, I resize only the images that are larger than a certain dimension, compress them, and strip the metadata. I tack on the format flag to create jpg copies of the images.

Here’s the one-liner again (broken up for better reading):

# Look in the public/ directory
find public/ \
# Ignore directories called "static" regardless of location
-not -path "*/static/*" \
# Print the file paths of all files ending with any of these extensions
\( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) -print0 \
# Pipe the file paths to xargs and use 8 parallel workers to process 2 arguments
| xargs -0 -P8 -n2 \
# Tell mogrify to strip metadata, and...
mogrify -strip \
# ...compress and resize any images larger than the target size (1000px in either dimension)
-thumbnail '1000>' \
# Convert the files to jpg format
-format jpg

That’s it. That’s the post.

Discussion (15)

pic
Editor guide
Collapse
booyaa profile image
Mark Sta Ana

You should write a blog post that does the same thing using sharp!

Collapse
stiles69 profile image
Brett Salemink

Great one liner. Was looking for something just like this. Thanks!

Collapse
victoria profile image
Victoria Drake Author

Sweet! You're welcome Brett!

Collapse
vistad profile image
vistad

Super cool! I also was looking for this kind of automation. Thank you!

Collapse
eaich profile image
Eddie

I actually needed something like this. Thank you so much!

Collapse
victoria profile image
Victoria Drake Author

Glad to hear it! You're welcome!

Collapse
zcabjro profile image
zcabjro

Thanks for sharing this.

Collapse
victoria profile image
Victoria Drake Author

You're welcome! Hope it was useful to you!

Collapse
navanshurastogi profile image
Navanshu Rastogi

Can you please explain more about parallel workers and why are you using 8 parallel workers?

Collapse
booyaa profile image
Mark Sta Ana • Edited

Having more workers allows you to process more data (in this case images) at the same time. The workers can process different chunks of the image. Number of workers will match the number of CPU cores you have on your computer. This is the same technique you would adopt when compiling code rather than wait for data to be processed sequentially.

Collapse
yujiri8 profile image
Ryan Westlund

And here I was foolish around using a Python library for this when there's a CLI tool :D

Collapse
victoria profile image
Victoria Drake Author

I think my work here is done. 😁

Collapse
joshcheek profile image
Josh Cheek

We need more posts like this.

Collapse
victoria profile image
Victoria Drake Author

I'll do my best! :)