python - 使用 Python 读取 16 位 PNG 图像文件

标签 python png python-imaging-library

我正在尝试读取以 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/

相关文章:

python - 获取指定文件夹的路径子集

python - defaultdict无法识别python中的2位数字

image - Chrome 中针对 png 图像的 MIME 类型警告

Android Large PNG 不显示问题

python - PIL : DLL load failed: specified procedure could not be found

python - 在 python/PIL 中执行与 ImageMagick 的 "-level"相同的操作?

python - 如何使用正则表达式查找带有句点分隔符的组合单词?

python - 在 python 中将标准输出设置为非阻塞

c++ - .png 图像信息

python - 在 Python 上使用 PIL 更改像素颜色