Convert tiff (I;16) to JPG with PIL/pillow

11,831

It's a bug. Here is a workaround:

import Image
image = Image.open("Fredy1_002.tif")
image.mode = 'I'
image.point(lambda i:i*(1./256)).convert('L').save('my.jpeg')

See https://stackoverflow.com/a/7248480/839338

Share:
11,831
Dario Behringer
Author by

Dario Behringer

Updated on June 05, 2022

Comments

  • Dario Behringer
    Dario Behringer almost 2 years

    I have a problem converting a tiff image from a microscope to a jpeg, which should be shown within a web application. I tried the following:

    image = Image.open(file_name)
    image.convert(mode="RGB")
    image.save('my.jpeg')
    
    >>IOError: cannot write mode I;16 as JPEG
    

    Anybody has some experience in converting 16-bit TIFF files to jpegs... I have linked such a file below. Thanks for your help!

    https://drive.google.com/open?id=0B04N02JqhWJOWjBPY1RRZkIwbTg

  • Dario Behringer
    Dario Behringer almost 7 years
    Thanks a lot! Works perfect and saved my day :)
  • DarioB
    DarioB almost 5 years
    hey, I am having the same problem! if I plot the image with matplotlib using ax.imshow(image.point(lambda i:i*(1./256)).convert('L'), cmap=plt.cm.bone) I can see the image, but the file 'my.jpeg' it is a black image! can you please help?
  • DarioB
    DarioB almost 5 years
    I have created this new stackoverflow question with more details stackoverflow.com/questions/56956198/…
  • Salvatore
    Salvatore over 3 years
    I'm guessing this answer was copied from Mark Ransom's identical answer here: stackoverflow.com/questions/7247371/python-and-16-bit-tiff
  • Dan-Dev
    Dan-Dev over 3 years
    I have used Pillow for years and PIL before that. I have many code snippets in my code base which I often use to answer questions. I am uncertain of where these originated from as I don't keep track of the source unless it is licenced. I generally link to answers if I find them on the web but if I find a snippet in my code base I just post it, apologies to Mark Ransom if my snippet came from his post originally.