Crap Nullpointer Exceptions

This is the blog of John Dulaney, a hacker of Fedora, SCAdian, player of Music, blacksmith, sailor, and consumer of Bacon.

Batch convert .jp2 files to png from the command line with GIMP

I have a bunch of Sanborn maps in JPEG 2000 (.jp2) format that are quite large.  The problem is that the the proprietary CAD software I use does not support this particular file format.  Therefore, I need to convert to something it does support; I chose png.  To convert all the files in a directory at once, I cd into that directory and run this:

jdulaney@gefjon:~/$ gimp -n -i -b – <<EOF
(let* ( (file’s (cadr (file-glob “*.jp2” 1))) (filename “”) (image 0) (layer 0) )
(while (pair? file’s)
(set! image (car (gimp-file-load RUN-NONINTERACTIVE (car file’s) (car file’s))))
(set! layer (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))
(set! filename (string-append (substring (car file’s) 0 (- (string-length (car file’s)) 4)) “.png”))
(gimp-file-save RUN-NONINTERACTIVE image layer filename filename)
(gimp-image-delete image)
(set! file’s (cdr file’s))
(gimp-quit 0)


2 responses to “Batch convert .jp2 files to png from the command line with GIMP

  1. Arthur Buliva January 6, 2015 at 9:31 am

    imageMagick is simpler. Here is an example usage bash line:

    sh-4.3$ convert -verbose Flag_of_Madagascar.svg Flag_of_Madagascar.jp2

    As neat as that. convert .

    For more usage examples, man convert

  2. JRM January 7, 2015 at 9:05 am

    If the jp2 files are georeferenced and if you want to keep it then using GDAL (gdal_translate) to convert to png/jpeg/tiff

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: