简体   繁体   中英

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

在此输入图像描述 Hello all,

I am working on a program which determines the average colony size of yeast from a photograph, and it is working fine with the .bmp images I tested it on. The program uses pygame, and might use PIL later.

However, the camera/software combo we use in my lab will only save 16-bit grayscale tiff's, and pygame does not seem to be able to recognize 16-bit tiff's, only 8-bit. I have been reading up for the last few hours on easy ways around this, but even the Python Imaging Library does not seem to be able to work with 16-bit .tiff's, I've tried and I get "IOError: cannot identify image file".

import Image

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

My ultimate goal is to have this program be user-friendly and easy to install, so I'm trying to avoid adding additional modules or requiring people to install ImageMagick or something.

Does anyone know a simple workaround to this problem using freeware or pure python? I don't know too much about images: bit-depth manipulation is out of my scope. But I am fairly sure that I don't need all 16 bits, and that probably only around 8 actually have real data anyway. In fact, I once used ImageMagick to try to convert them, and this resulted in an all-white image: I've since read that I should use the command "-auto-levels" because the data does not actually encompass the 16-bit range.

I greatly appreciate your help, and apologize for my 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 bundle it with Python and pygame so it's only one install? Can this be done for both Windows and Mac? Thank you.

EDIT: I tried to open it in GIMP, and got 3 errors:

1) Incorrect count for field "DateTime" (27, expecting 20); tag trimmed 2) Sorry, can not handle images with 12-bit samples 3) Unsupported layout, no RGBA loader

What does this mean and how do I fit it?

I would look at pylibtiff , which has a pure python tiff reader.

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

py2exe is the way to go for packaging up your application if you are on a windows system.

Regarding the 16bit tiff issue:

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

Now for the unasked portion question: When doing image analysis, you want to maintain the highest dynamic range possible for as long as possible in your image manipulations - you lose less information that way. As you may or may not be aware, PIL provides you with many filters/transforms that would allow you enhance the contrast of an image, even out light levels, or perform edge detection. A future direction you might want to consider is displaying the original image (scaled to 8 bit of course) along side a scaled image that has been processed for edge detection.

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

This is actually a 2 part question:

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

2) Release engineering Python apps can get messy. Depending on how complex your app is you can get away with py2deb , py2app and py2exe . Learning distutils will help too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM