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

前端 未结 2 643
一向
一向 2021-01-18 01:41

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

相关标签:
2条回答
  • 2021-01-18 02:16

    ImageJ opens float Tiff images.

    0 讨论(0)
  • 2021-01-18 02:20

    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

    0 讨论(0)
提交回复
热议问题