pythonjpegcompressionpython-imageiorawimage

JPEG-Decompression on RAW image in python


When I try to JPEG-Decompress (old-style JPEG compression, not JPEG-LS and not JPEG2000) the RAW data, I get following error:

Traceback (most recent call last):
  File "raw-reader.py", line 766, in <module>
    raw_image_data = imageio.imread(io.BytesIO(raw_packed_image_data))
  File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 206, in imread
    reader = read(uri, format, 'i', **kwargs)
  File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 129, in get_reader
    return format.get_reader(request)
  File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/format.py", line 168, in get_reader
    return self.Reader(self, request)
  File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/format.py", line 217, in __init__
    self._open(**self.request.kwargs.copy())
  File "/home/ian/.local/lib/python3.6/site-packages/imageio/plugins/pillow.py", line 398, in _open
    pilmode=pilmode, as_gray=as_gray)
  File "/home/ian/.local/lib/python3.6/site-packages/imageio/plugins/pillow.py", line 122, in _open
    self._im = factory(self._fp, '')
  File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 780, in jpeg_factory
    im = JpegImageFile(fp, filename)
  File "/home/ian/.local/lib/python3.6/site-packages/PIL/ImageFile.py", line 102, in __init__
    self._open()
  File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 339, in _open
    handler(self, i)
  File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 166, in SOF
    raise SyntaxError("cannot handle %d-bit layers" % self.bits)
SyntaxError: cannot handle 14-bit layers

The RAW data in the image is 14-bit JPEG data, and imageio isn't able to read it. When I tried using pillow, it didn't even recognize the data as JPEG. My question now is: How can I decompress the data without writing my own JPEG decompressor, while keeping in mind that the data is 14 bits?

My code:

import io
import imageio

allbytes = open("raw_data.dat", "rb").read()

raw_packed_image_data = allbytes
raw_image_data = imageio.imread(io.BytesIO(raw_packed_image_data))

The file raw_data.dat is a file containing purely the RAW-Image data compressed with JPEG. Link to raw_data.dat


Solution

  • raw_data.dat is a JPEG Lossless, Nonhierarchical file with 2 frames and a precision > 8-bit, a very rare format.

    The imagecodecs package can read the file (assuming that the _imagecodecs Cython extension is present):

    >>> from imagecodecs import jpegsof3_decode
    >>> data = open('raw_data.dat', 'rb').read()
    >>> image = jpegsof3_decode(data)
    >>> image.shape
    (3528, 2640, 2)
    >>> image.dtype
    dtype('uint16')
    

    The LEADTOOLS SDK should also be able to read the file (not tested).