Imagemagick uses the
convert shell command to convert various file formats back and forth. When it is used to convert
pdf to png/jpg, it delegates this task to ghostscript (
gs command on shell) to do the conversion.
One might think they both should take the same time to complete task but imagemagick limits user in the settings that
can be passed to
gs and it does extra work while passing the jobs to ghostscript.
ImageMagick loads the entire pdf into the memory before process it. On the other hand ghostscript has the capability of processing 1 page at time which reduces the load on hardware by a lot. Here is a great article about that i used as a reference in this testing.
I must mention few background info on the specs:
- ImageMagick version: ImageMagick 6.7.7-10 2013-02-25 Q16
- GhostScript version: 9.07
- Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz and 8GB ram
I used a sample PDF file with following meta info:
convert to convert from PDF to PNG, with 300 density:
it took 426 seconds and 51mb
gs to convert from PDF to PNG, with 300 density:
it took 135 seconds and 39mb disk space
Also I have achieved further time improvement by using jpeg,
gs to convert from PDF to JPEG with 300 density and 100% quality:
it took 32 seconds and 153mb disk space
These are the shell scripts I have used for my benchmarking: For PDF to PNG
For PDF to JPEG
For PDF to PNG
Although I must mention that I couldn’t get multi-threading working for ghostscript, I left the multi-threaded option in the code for those who might have ghostscript installed properly and has the multi-threading enabled.