Skip to content

Opening 12-bit tiff files #414

Description

@kadrach

Hi,

I am trying to open 12bit image files with PIL/Pillow. This currently seems to be unsupported.

For a sample image, the debug mode spits out the following:

format key: ('II', 1, 1, 1, (12,), ())

Now I thought I'd cheat and just insert a key into the OPEN_key dict in TiffImagePlugin.py

(II, 1, 1, 1, (12,), ()): ("I;16", "I;16")

This now obviously messes up the image, and my tiff file ends seemingly prematurely.

Am I missing something? If it is possible to read 12bit tiffs easily, please let me know. If it is as of yet unsupported, I am happy to help, although I am afraid I will need some pointers :)

Here are two samples: Single frame, small stack (500mb!).

Activity

  1. wiredfool commented on Nov 19, 2013

    @wiredfool
    Member

    Even if you can't pull out a single page, let me take a look at it and see what I can do.

  2. wiredfool commented on Nov 19, 2013

    @wiredfool
    Member

    ok. downloading. wasn't expecting a cd sized image...

  3. kadrach commented on Nov 19, 2013

    @kadrach
    Author

    And that's a small one. Didn't want to upload the full sized ones ;)

  4. wiredfool commented on Nov 19, 2013

    @wiredfool
    Member

    Ok. Imagemagick helpfully converted that into 1000 separate tiff files, each ~400k, 1024x304, 12bit gray with no compression. (Apparently it can convert all of them, but not one of them. Yay.)

    I see the 12bit packing there.

  5. kadrach commented on Nov 20, 2013

    @kadrach
    Author

    I hadn't thought of using imagemagick. I have amended the original post to include both samples.

  6. wiredfool commented on Nov 20, 2013

    @wiredfool
    Member

    Imagemagick is a good swiss army knife for pulling apart images to see what's going on. On the other hand, it doesn't always give you what you want. I'm currently trying to get a good 12 bit test image with known values (e.g. a ramp from 0x000 to 0x0ff, or at least known integer values for some pixels) so that I can ensure that I'm not getting bits badly twiddled. But changing the depth of an 8 bit image normalizes the values so that they go full range, and I haven't figured out how to turn that off, at least in a way that I know what the values are supposed to be.

    So, current progress is that I've got I12 files unpacking to I16, but I'm stuck on the verification. Do you happen to have a way to dump one frame to raw 16bit values (with known endianess)?

  7. kadrach commented on Nov 20, 2013

    @kadrach
    Author

    This is the exact same image, saved from the camera in 12bit (actual chip bit depth), 16bit padded left and 16bit padded right. The camera offers the option of selecting "high" or "low" bits when saving at 16bit, I read this as padding left/right, but that might be wrong.

  8. wiredfool commented on Nov 20, 2013

    @wiredfool
    Member

    I'm getting the padded left and padded right ones being exactly the same:

    md5sum *
    7cd4d8c50dbb93ef9783a4962afe79a0  12bit.tif
    2cc0d9a86f09a9cc2561d4419ddb8888  16bit-left.tif
    2cc0d9a86f09a9cc2561d4419ddb8888  16bit-right.tif
    

    Imagemagick is reporting that the max value in the 16 bit ones is 255,255,255, which would correspond to an 8 bit image padded left. It's also reporting that it's an MSB oriented image, which is... not in agreement with the II* prefix.

    Like other swiss army knives, sometimes it's not really the right tool for the job.

  9. added a commit that references this issue on Nov 21, 2013
    cc5e8f0
  10. wiredfool commented on Nov 21, 2013

    @wiredfool
    Member

    Ok, I managed to get imagemagick to give me two equivalent files, one was cropped from one frame of your original, one was converted to be the same data in a 16bit form, and I was able to get the images to match.

    Try out cc5e8f0 on your machine and see if that does it for you. I still need test on a bigendian machine, but I should have access to one of them around here somewhere.

  11. kadrach commented on Nov 21, 2013

    @kadrach
    Author

    Yes that works, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions