Lijo Joseph
Lijo Joseph

Reputation: 139

How to Read 16Uint Images in Python

I am having a medical 16 unsigned jpeg image and I need to read it in python. these images can be read only with mathlab for my project I need to read its actual pixel values and need to do some mathematical operations in that values. you can find the image in the following link

https://drive.google.com/file/d/0B4l5GiM7kBXraDEyMXdseENfUlE/edit?usp=sharing

when i am ask to get the image info in mathlab the following description is coming.

i = imfinfo('di.jpg')

i = 

       Filename: [1x56 char]
    FileModDate: '14-Jul-2014 15:22:13'
       FileSize: 1044064
         Format: 'jpg'
  FormatVersion: ''
          Width: 1024
         Height: 1024
       BitDepth: 16
      ColorType: 'grayscale'
FormatSignature: ''
NumberOfSamples: 1
   CodingMethod: 'Huffman'
  CodingProcess: 'Lossless'
        Comment: {'Created by AccuSoft Corp.'}

I am already used matplotlib, opencv, scikitImage, scipy, medpy, PIL libraries for reading this image in python. I am not able to read this image in these types.

when I am using in python the following error is coming code: import Image img = Image.open('di.jpg') print (img)

raise IOError("cannot identify image file")
IOError: cannot identify image file

please help to solve my problem

Upvotes: 1

Views: 1336

Answers (1)

DrV
DrV

Reputation: 23480

This seems to be a duplicate of How we can read 16 un signed integer(16 uint) jpeg files in python

I had a look at your image at https://drive.google.com/file/d/0B4l5GiM7kBXraDEyMXdseENfUlE/edit?usp=sharing . What is clear is that it is a 16-bit grayscale lossless JPEG image with Huffman coding. However, for lossless coding there are at least:

  • JPEG lossless (rare, old)
  • JPEG-LS (quite rare, not so old)
  • JPEG2000 (newer)

For more information on these, please see: http://en.wikipedia.org/wiki/Lossless_JPEG

The GDCM library mentioned by cel in their comment uses the CharLS library to read JPEG-LS. This library indeed has Python bindings, but unfortunately refuses to load the image, because it is not a JPEG-LS file. (However, YMMV, see malat's comment below.)

I also tried to query ImageMagick's identify, and it gives a bit more information:

identify.im6: Unsupported JPEG process: SOF type 0xc3 `/tmp/di.jpg' @ error/jpeg.c/JPEGErrorHandler/316.

Now the SOF types are important in JPEG. The error message suggests that the file in question is a "JPEG lossless" file. (Which could have been deduced by the fact that it employs Huffman coding.) The nasty thing is that the encoding scheme used in this type of file is completely different from the standard JPEG, and the standard JPEG libraries do not support it.

There seems to be two at least two open source libraries which support the file format:

I tried the latter, and its command line tool indeed recognized the file and decoded it into something more useful (a grayscale PPM file).

So, this offers at least one route. You have to compile and install the library and then create the necessary Python bindings for it. Alternatively, you may use the command line tools as external conversion tools and run them with, e.g., subprocess. The best approach depends on your platform.

Upvotes: 2

Related Questions