简体   繁体   English

Python PIL将我的16位灰度图像截断为8位

[英]Python PIL cut off my 16-bit grayscale image at 8-bit

I'm working on an python program to display images of stars. 我正在使用python程序来显示星星图像。 The images are 16-bit grayscale tiffs. 图像是16位灰度tiff。 If I try to display them in an extern program, eg ImageMagick they are correct but if I load them in python and then use 'show()' or implement them in a canvas in Tkinter they are, unless a few pixel, totally white. 如果我尝试在extern程序(例如ImageMagick)中显示它们,则它们是正确的,但是如果我将它们加载到python中,然后使用“ show()”或在Tkinter的画布中实现它们,则除非有几个像素,否则它们将是完全白色的。 So I estimate python sets every pixel above 255 to white but I don't know why. 因此,我估计python将255以上的每个像素都设置为白色,但是我不知道为什么。 If I load the image and then save it as tiff again, ImageMagick can show it correct. 如果我加载图像,然后再次将其另存为tiff,ImageMagick可以正确显示它。 Thanks for help. 感谢帮助。

Try to convert the image to a numpy array and display that: 尝试将图像转换为numpy数组并显示:

import Image
import matplotlib.pyplot as plt
import numpy as np

img = Image.open('image.tiff')
arr = np.asarray(img.getdata()).reshape(img.size[1], img.size[0])
plt.imshow(arr)
plt.show()

You can change the color mapping too: 您也可以更改颜色映射:

from matplotlib import cm
plt.imshow(arr, cmap=cm.gray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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