Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16-bit TIFF files truncated to 0..255 before conversion to float #274

Closed
tdsmith opened this issue Jul 8, 2013 · 2 comments

Comments

Projects
None yet
3 participants
@tdsmith
Copy link

commented Jul 8, 2013

Example data file: https://dl.dropboxusercontent.com/u/3720/junk/12bit.tif (a 16-bit TIFF container with pixel values spanning a 12-bit range, which is common in scientific imaging)

Opening the file with Image.open gives the expected result:

>>> im, im.mode
(<PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1344x1024 at 0x1074A2518>, 'I;16')
>>> im.getpixel((5,5))
447

But converting to a float type clips all pixel values above 255 to 255:

>>> im.convert('F').getpixel((5,5))
255.0

Conversion to float should not clip 16-bit integer values.

@wiredfool

This comment has been minimized.

Copy link
Member

commented Jul 9, 2013

It appears (from Convert.c) that there's no direct conversion from I;16 to F. There are I to F, and I;16 to I, but nothing that works in one step. [edit, though, I'm not actually seeing how it should work at all, since it should return a conversion not supported error. edit 2, this https://github.com/python-imaging/Pillow/blob/master/PIL/Image.py#L703 is the truncation. ]

def _test_float_conversion(im):
    orig = im.getpixel((5,5))
    converted = im.convert('F').getpixel((5,5))
    assert_equal(orig, converted)

#fails
def test_12bit():
    im = Image.open('Tests/images/12bit.tif')
    _test_float_conversion(im)

#succeeds
def test_12bit_workaround():
    im = Image.open('Tests/images/12bit.tif')
    _test_float_conversion(im.convert('I'))

It looks like a conversion for the I;16 modes -> F should be straight forward.

@bailey-989

This comment has been minimized.

Copy link

commented Jul 15, 2013

In case anyone is as retarded as me, and completely didn't twig from the above answer, just use

im.convert('I').convert('F').

This is what Wiredfool suggested, so credit to him.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.