Skip to content

How To Encode JP2s

Jeremy Echols edited this page Oct 9, 2018 · 1 revision

How to encode JP2s

The Historic Oregon Newspapers images JP2s are encoded in a way that makes them very friendly to pan-and-zoom systems. They are encoded as multi-resolution (think "zoom levels"), tiled images.

JP2s that aren't encoded like this will not be nearly as memory- and CPU-efficient. We'd recommend tiling JP2s at a size of around 1024x1024.

Open JPEG Tools

The openjpeg tools make this simple:

/bin/opj2_compress -i input.tiff -o output.jp2 -t 1024,1024 -r 20.250 -n 6

Some notes:

  • A rate [-r] of 20.250 is equivalent to a graphicsmagick "quality" of 70, which in JP2-land is about the same as a JPEG of quality 90-95 (very good).
  • -n 6 specifies that there are six resolution levels. This can be optimized based on the image's size if desired, but 6 is the default. Typically 6 will be fine, but a decent guideline is to start at 6 levels for a 16-megapixel image and add a resolution level each time the number of megapixels quadruples. e.g., 16mp = -n 6, 64mp = -n 7, 256mp = -n 8, etc.
  • You may have to build openjpeg tools manually to get support for converting some image formats, including TIFF and PNG

Graphics Magick

If using graphics magick, encoding is still fairly easy:

gm convert input.tiff -flatten -quality 70 \
    -define jp2:prg=rlcp \
    -define jp2:numrlvls=7 \
    -define jp2:tilewidth=1024 \
    -define jp2:tileheight=1024 output.jp2

Note that as mentioned above, the GraphicsMagick quality value doesn't directly correlate to the rate used by opj2_compress. Also, as mentioned above, the number of resolution levels can depend a lot on the size of the source image.

Grayscale vs. color

Grayscale images will require one-third the memory and processing power when compared to color images. If your sources are grayscale, but you scan in color for better preservation, consider building grayscale derivatives for web display.