How to quickly convert a bunch of JPEG2000s to JPEGs
Let’s say you have a directory full of jp2 files, and you want to convert them to jpg. The usual way of doing something like this is to use ImageMagick’s mogrify command:
mogrify -format jpg *.jp2
ImageMagick uses JasPer to decompress the jp2, and it is unbelievably slow. If you want to do the same conversion, but five times faster, use the free Kakadu software instead.
Here is a script that will quickly convert the jp2s in the current directory to jpegs. You will have to edit the paths to the Kakadu binary and library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/bin/python """ mogrify -format jpg *.jp2 is taking 24.5 seconds/image. The same jp2->jpg process takes 4.5 seconds/image on the same box when using Kakadu, even when creating a temporary ppm file. You can get the free Kakadu binary here: http://www.kakadusoftware.com/ This script operates on jp2 files in the current dir. """ import commands import glob files = glob.glob("*.jp2") for file in files: print "Processing " + file cmd = "LD_LIBRARY_PATH=/usr/local/lib/kakadu /usr/local/bin/kdu_expand -i %s -o tmp.ppm" %(file) retval = commands.getstatusoutput(cmd)[0] assert (0 == retval) cmd = "pnmtojpeg tmp.ppm > %s" % (file.replace('.jp2', '.jpg')) retval = commands.getstatusoutput(cmd)[0] assert (0 == retval) from os import unlink unlink('tmp.ppm') |
Filed under: code code |
Tagged: commandline , ImageMagick , jpeg , jpeg2000 , kakadu , mogrify
