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
Simple way to resize large number of image files
256 views Asked by DucCuong At
2
I would use GNU Parallel, like this to make the most of all those cores:
If you had fewer files, you could do it like this, but the commandline would be too long for 45,000 files:
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...
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
andmogrify
in concert work well for you, like this: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.