python - 我可以使用 "normal"(Enthought) python 将 numpy 数组保存为 16 位图像吗?

标签 python numpy

有什么方法可以使用任何常用的 python 包将 numpy 数组保存为 16 位图像(tif、png)? This这是我过去唯一可以开始工作的方法,但我需要安装 FreeImage 包,这有点烦人。

这似乎是一个非常基本的任务,所以我希望它应该被 scipy 涵盖,但 scipy.misc.imsave 只做 8 位。

有什么想法吗?

最佳答案

一种替代方法是使用 pypng .你仍然需要安装另一个包,但它是纯 Python 的,所以这应该很容易。 (在pypng源码中实际上有一个Cython文件,但它的使用是可选的。)

下面是一个使用pypng将numpy数组写入PNG的例子:

import png

import numpy as np

# The following import is just for creating an interesting array
# of data.  It is not necessary for writing a PNG file with PyPNG.
from scipy.ndimage import gaussian_filter


# Make an image in a numpy array for this demonstration.
nrows = 240
ncols = 320
np.random.seed(12345)
x = np.random.randn(nrows, ncols, 3)

# y is our floating point demonstration data.
y = gaussian_filter(x, (16, 16, 0))

# Convert y to 16 bit unsigned integers.
z = (65535*((y - y.min())/y.ptp())).astype(np.uint16)

# Use pypng to write z as a color PNG.
with open('foo_color.png', 'wb') as f:
    writer = png.Writer(width=z.shape[1], height=z.shape[0], bitdepth=16,
                        greyscale=False)
    # Convert z to the Python list of lists expected by
    # the png writer.
    z2list = z.reshape(-1, z.shape[1]*z.shape[2]).tolist()
    writer.write(f, z2list)

# Here's a grayscale example.
zgray = z[:, :, 0]

# Use pypng to write zgray as a grayscale PNG.
with open('foo_gray.png', 'wb') as f:
    writer = png.Writer(width=z.shape[1], height=z.shape[0], bitdepth=16,
                        greyscale=True)
    zgray2list = zgray.tolist()
    writer.write(f, zgray2list)

这是颜色输出:

foo_color.png

这是灰度输出:

foo_gray.png


更新:我创建了一个名为 numpngw 的库(在 PyPIgithub 上可用),它提供了将 numpy 数组写入 PNG 文件的功能。存储库有一个 setup.py 文件,用于将其作为一个包安装,但基本代码位于单个文件 numpngw.py 中,可以将其复制到任何方便的位置地点。 numpngw 的唯一依赖项是 numpy。

这是一个生成与上面所示相同的 16 位图像的脚本:

import numpy as np
import numpngw

# The following import is just for creating an interesting array
# of data.  It is not necessary for writing a PNG file.
from scipy.ndimage import gaussian_filter


# Make an image in a numpy array for this demonstration.
nrows = 240
ncols = 320
np.random.seed(12345)
x = np.random.randn(nrows, ncols, 3)

# y is our floating point demonstration data.
y = gaussian_filter(x, (16, 16, 0))

# Convert y to 16 bit unsigned integers.
z = (65535*((y - y.min())/y.ptp())).astype(np.uint16)

# Use numpngw to write z as a color PNG.
numpngw.write_png('foo_color.png', z)

# Here's a grayscale example.
zgray = z[:, :, 0]

# Use numpngw to write zgray as a grayscale PNG.
numpngw.write_png('foo_gray.png', zgray)

关于python - 我可以使用 "normal"(Enthought) python 将 numpy 数组保存为 16 位图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25696615/

相关文章:

python - 在图像上叠加 matplotlib quiver

python - 将 ifort 与 f2py 结合使用时禁用 fp-model strict

python - 如何在不使用任何内置函数且不使用循环的情况下生成类似转置的矩阵?

python - Cython 中用于循环调用的快速基本线性代数

python - Pandas:距某个日期还剩多少天

python - 用于 Transformer DNN 模型的基于时间序列的数据的位置编码

python - 只读具有值的 Excel 单元格 python win32com

python - 使用 matplotlib 和 np.linalg 绘制协方差矩阵的特征向量

python - 循环继承?什么?

使用 cv2.imread() 时出现 Python openCV 错误