4

enter image description hereHello 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.

P.S.: 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?

Ain Britain
  • 370
  • 4
  • 15
  • 2
    What version of PIL are you using? PIL should have support for 16-bit grayscale images (since 1.1.4). – Gabe Apr 24 '11 at 06:05
  • Personally I would look at SciPy, with scipy.misc.imread() and the labelled array functions, which seem to be perfect for what you are doing. – Benjamin Apr 24 '11 at 12:52
  • I have been using PIL version 1.1.7, so maybe it's not the bit-depth? I'm stumped! Is there some other odd formatting the camera software could be doing to the .tiff's? – Ain Britain Apr 25 '11 at 07:19
  • 1
    On a related but separate note, you might want to look at [CellProfiler](http://cellprofiler.org). It has an example pipeline for colony counting, and deals with 16-bit images in multiple formats. –  Apr 25 '11 at 21:13
  • Sorry, dumb question: can I upload my example image on this site? It seems that stackoverflow converted it to a png. – Ain Britain Apr 26 '11 at 00:43
  • So we can conclude that your images have problems. 1) Find the manual or documentation for the camera. Scientific equipment usually has one with detailed specs. 2) Determine the output being generated and if there are any options OR 3) try other tools Photoshop, Imagemagick, etc. This is why science research is hard - the experiment can be easy while the data analysis is a pain. At this point you should accept thouis's answer and ask a new question specifically about your image format. This is no longer a python question. – WombatPM Apr 28 '11 at 12:45

3 Answers3

2

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

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

2

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.

WombatPM
  • 2,561
  • 2
  • 22
  • 22
  • I can't even Image.open() these tiff's, it gives me "IOError: cannot identify image file", so the example in the first link won't work. I know the files are fine because they open in Photoshop. Could it be something other than the bit-depth? – Ain Britain Apr 25 '11 at 06:09
  • If you open in photoshop and re-save as tiff does it work? It could be a file header issue or raw data versus compressed. – WombatPM Apr 25 '11 at 13:53
  • I can open them in and convert them with Photoshop; I've been testing my code with .bmp's I made. According to right-clicking and looking at "Properties", there is no compression in the originals. I don't know what a file header is or how to look at it, how can I see if anything's abnormal? – Ain Britain Apr 26 '11 at 00:52
  • But you are changing formats. If you open one of the images, and save as with a new name but keep the file type as TIFF and data as 16 bits what happens. Photoshop will silently fix things in the background and when you do a SaveAs fix any header issues. My point is try to determine if a known good TIFF file works. If so, you can add in a routine (ImageMagick can be called from within Python) or you can find out what is wrong with these files (maybe they are not really TIFFs) but first you need to isolate the problem. – WombatPM Apr 26 '11 at 11:34
  • @cgohlke @WombatPM 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? – Ain Britain Apr 26 '11 at 18:38
1

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.

whatnick
  • 5,400
  • 3
  • 19
  • 35