4

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
  • 287
  • 2
  • 3
  • 10

1 Answers1

7

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

Dan-Dev
  • 8,957
  • 3
  • 38
  • 55
  • Thanks a lot! Works perfect and saved my day :) – Dario Behringer May 15 '17 at 13:22
  • 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 Jul 09 '19 at 15:26
  • I have created this new stackoverflow question with more details https://stackoverflow.com/questions/56956198/pil-converting-from-i16-to-jpeg-produce-white-image – DarioB Jul 09 '19 at 15:41
  • I'm guessing this answer was copied from Mark Ransom's identical answer here: https://stackoverflow.com/questions/7247371/python-and-16-bit-tiff – Salvatore Oct 12 '20 at 22:09
  • 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. – Dan-Dev Oct 13 '20 at 00:13