Reputation: 31
I'm trying to convert some matlab code to python and the im2double
function is giving me trouble. It takes an image and returns a matrix with the pixels using doubles instead of ints.
Currently I'm manipulating my images with PIL. It has a convert
method that can take 'F'
as parameter, but all it does is convert the integer value 255
to 255.0
. Useless as far as I can tell.
The problem I have is that I'm doing some image manipulation and then have to save them. I can normalize my values so that they fall into the 0-255 range, but I lose some precision. It's small enough that it shouldn't normally matter, but here, it does.
I've tried using the 'tiff' file format and that didn't work out well. Though I can write/read to it, the results I get are not the right ones, which I can only get at the moment converting the pixels to 255 which results in a loss of precision, as I said previously. I also tried this 'SPIDER'
file format thing I found on google previously that PIL supports though I couldn't open the image on an editor to check how it was doing.
Upvotes: 3
Views: 3455
Reputation: 5470
The way to do this properly in Python will to use Numpy. You can read images via PIL into numpy arrays. At this point a wide range of Matlab like matrix operations become available to you via numpy/scipy. Changing the precision of the array is simply a matter of switching the arrays datatype via numpy. Recent releases of PIL include the patch from Travis Oliphant to allow you to do this without extra hackery.
Saving the data to a more commonly readable image format can be achieved by using a floating point TIFF without loss of precision. I use the GDAL library to interface to multiple image format writers/readers. If you want lossless compression TIFF can compress using zlib as well.
Upvotes: 1