Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save a mode 'F' image? (Python/PIL)

Tags:

python

image

I have a ndarray with floats in it I want to save. I would like to keep the values as float though. The only format I found that accepts saving float data is tiff. Doesn't show the actual image however.

from Image import *
from numpy import *

img = random.random((300, 300)) #float numbers, i have actual data in my image though
img = fromarray(img)
img.save('test.tiff')
like image 237
jBorys Avatar asked Mar 24 '11 03:03

jBorys


People also ask

How do I save an image in Python PIL?

save() Saves this image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible. Keyword options can be used to provide additional instructions to the writer.

What is mode in PIL image?

Modes. The mode of an image is a string which defines the type and depth of a pixel in the image. Each pixel uses the full range of the bit depth. So a 1-bit pixel has a range of 0-1, an 8-bit pixel has a range of 0-255 and so on.


2 Answers

Your example is saving a floating-point TIFF file. I've confirmed by examining the TIFF header, noting that the samples per pixel tag 0x153 has a value of 3 (floating point data). Using your example:

import Image
from numpy import *

data = random.random((2, 2))
img1 = Image.fromarray(data)
img1.save('test.tiff')
img2 = Image.open('test.tiff')

f1 = list(img1.getdata())
f2 = list(img2.getdata())
print f1 == f2
print f1

Output:

True
[0.27724304795265198, 0.12728925049304962, 0.4138914942741394, 0.57919681072235107]

Details on the TIFF6 file format

Updated: Example 64x64 image viewed on Mac desktop: enter image description here

like image 111
samplebias Avatar answered Nov 17 '22 11:11

samplebias


ImageJ opens float Tiff images.

like image 43
Lcharleux Avatar answered Nov 17 '22 11:11

Lcharleux