我正在尝试读取以 16 位数据类型编写的 PNG 图像文件。数据应转换为 NumPy 数组。但我不知道如何读取“16 位”文件。我尝试使用 PIL 和 SciPy,但它们在加载时将 16 位数据转换为 8 位数据。谁能告诉我如何在不更改数据类型的情况下从 16 位 PNG 文件读取数据并将其转换为 NumPy 数组?
以下是我使用的脚本。
from scipy import misc
import numpy as np
from PIL import Image
#make a png file
a = np.zeros((1304,960), dtype=np.uint16)
a[:] = np.arange(960)
misc.imsave('16bit.png',a)
#read the png file using scipy
b = misc.imread('16bit.png')
print "scipy:" ,b.dtype
#read the png file using PIL
c = Image.open('16bit.png')
d = np.array(c)
print "PIL:", d.dtype
最佳答案
我建议使用 opencv:
pip install opencv-python
和
import cv2
image = cv2.imread('16bit.png', cv2.IMREAD_UNCHANGED)
- 对比OpenImageIO , opencv 可以从 pip 安装
- 读取单个 4000x4000 png 所需的时间与 PIL 大致相同,但 PIL 使用更多 CPU,并且需要更多时间将数据转换回
uint16
。
关于python - 使用 Python 读取 16 位 PNG 图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622658/