Quantcast

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.

PYTHON:
  1. #!/usr/bin/python
  2.  
  3. “”
  4. mogrify -format jpg *.jp2 is taking 24.5 seconds/image.
  5. The same jp2->jpg process takes 4.5 seconds/image on the same box when using
  6. Kakadu, even when creating a temporary ppm file.
  7. You can get the free Kakadu binary here:
  8. http://www.kakadusoftware.com/
  9. This script operates on jp2 files in the current dir.
  10. “”
  11.  
  12. import commands
  13. import glob
  14.  
  15. files = glob.glob(“*.jp2″)
  16.  
  17. for file in files:
  18.     print “Processing “ + file
  19.    
  20.     cmd = “LD_LIBRARY_PATH=/usr/local/lib/kakadu /usr/local/bin/kdu_expand -i %s -o tmp.ppm” %(file)   
  21.     retval = commands.getstatusoutput(cmd)[0]
  22.     assert (0 == retval)
  23.  
  24.     cmd = “pnmtojpeg tmp.ppm> %s” % (file.replace(‘.jp2′, ‘.jpg’))
  25.     retval = commands.getstatusoutput(cmd)[0]
  26.     assert (0 == retval)
  27.  
  28. from os import unlink
  29. unlink(‘tmp.ppm’)