Python: Manipulating a 16-bit .tiff image in PIL & / or pygame: somehow convert to 8-bit?

enter image description here Hello to all,

I am working on a program that determines the average size of a yeast colony in a photograph, and it works great with .bmp images that I tested them on. The program uses pygame and can use PIL later.

However, the camera / software combination that we use in my laboratory will only save 16-bit shades of gray, and pygame does not seem to be able to recognize the 16-bit tiff, only 8 bits. I read the last few hours along simple paths around this, but even the Python Imaging Library does not seem to work with the 16-bit .tiff, I tried, and I get "IOError: cannot detect image file".

import Image img = Image.open("01 WT mm.tif") 

My ultimate goal is to make this program convenient and easy to install, so I try not to add additional modules or require people to install ImageMagick or something like that.

Does anyone know a simple workaround for this problem with free or pure python? I don’t know too much about images: manipulating depth to depth is not suitable. But I'm sure I don't need all 16 bits, and probably only about 8 really have real data. In fact, I once used ImageMagick to try to convert them, and this resulted in an all-white image: since then I read that I have to use the "-auto-levels" command because the data does not actually cover 16-bit.

I really appreciate your help and apologize for the lack of knowledge.

PS: Does anyone have any tips on how to make my Python program easy for non-programmers to install? Is there a way, for example, to somehow associate it with Python and pygame, so that is only one installation? Can this be done for both Windows and Mac? Thanks.

EDIT: I tried opening it in GIMP and got 3 errors:

1) Wrong counter for the "DateTime" field (27, expect 20); cropped tag 2) Sorry, cannot process images with 12-bit samples 3) Unsupported layout, no RGBA downloader

What does it mean and how do I fit it?

+4
source share
3 answers

py2exe is the way to package your application if you are on a Windows system.

Regarding the problem with 16-bit typhoid:

This example http://ubuntuforums.org/showthread.php?t=1483265 shows how to convert to display using PIL.

Now for the question about the unsolicited part: when analyzing images you want to maintain the maximum possible dynamic range as long as possible when manipulating images - you lose less information in this way. As you may or may not know, PIL provides you with a lot of filters / conversions that will allow you to increase the contrast of the image, even reduce the light level or perform edge detection. A future direction you might want to consider is to display the original image (scaled to 8 bits, of course) along the side of the scaled image that was processed to detect the edge.

Check out http://code.google.com/p/pyimp/wiki/screenshots for more examples and sample code.

+2
source

I would take a look at pylibtiff , which has a clean tiff reader for python.

For picking, your best bet is probably py2exe and py2app.

+2
source

This is actually a two-part question:

1) 16-bit image data conversion for Python - I usually use GDAL + Numpy. This may be too much for your requirements, you can use PIL + Numpy instead.

2) The technology for releasing Python applications can become messy. Depending on how complex your application is, you may want to get away with py2deb , py2app, and py2exe . Learning distutils will also help.

+1
source

All Articles