Simple way to resize large number of image files

256 views Asked by At

I have a folder which contains about 45000 jpeg images. Most of them are from 10KB - 20Kb. Now I want to write a script to resize all of them to fixed size 256x256. I wonder if there is any simple way to do that like: for a in *.jpg do .... I am using 8-core CPU with 8GB of RAM machine running Ubuntu 14.04, so it is fine if the process requires many resources

2

There are 2 answers

0
Mark Setchell On BEST ANSWER

I would use GNU Parallel, like this to make the most of all those cores:

find . -name \*.jpg | parallel -j 16 convert {} -resize 256x256 {}

If you had fewer files, you could do it like this, but the commandline would be too long for 45,000 files:

parallel -j 16 convert {} -resize 256x256 {} ::: *.jpg

Also, note that if you want the files to become EXACTLY 256x256 regardless of input dimensions and aspect ratio, you must add ! after the -resize like this -resize 256x256!

As Tom says, make a backup first!

Here is a little benchmark...

# Create 1,000 files of noisy junk @1024x1024 pixels
seq 1 1000 | parallel convert -size 1024x1024 xc:gray +noise random {}.jpg

# Resize all 1,000 files using mogrify
time mogrify -resize 256x256 *.jpg

real    1m23.324s

 # Create all 1,000 input files afresh
seq 1 1000 | parallel convert -size 1024x1024 xc:gray +noise random {}.jpg

# Resize all 1,000 files using GNU Parallel
time parallel convert -resize 256x256 {} {} ::: *.jpg

real    0m22.541s

You can see that GNU Parallel is considerably faster for this example. To be fair though, it is also wasteful of resources though because a new process has to be created for each input file, whereas mogrify just uses one process that does all the files. If you knew that the files were named in a particular fashion, you may be able to optimise things better...

Finally, you may find xargs and mogrify in concert work well for you, like this:

time find . -name \*.jpg -print0 | xargs -0 -I {} -n 100 -P 8 mogrify -resize 256x256 {}

real    0m20.840s

which allows up to 8 mogrify processes to run in parallel (-P 8), and each one processes up to 100 input images (-n 100) thereby amortizing the cost of starting a process over a larger number of files.

0
Tom Fenech On

You could use the mogrify tool provided by ImageMagick

mogrify -resize 256x256 *.jpg

This modifies all files in place, resizing them to 256x256px. Make sure to take a backup of your originals before using this command.