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:
-
#!/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 |
0 Comments
Tagged: commandline , ImageMagick , jpeg , jpeg2000 , kakadu , mogrify
