我可以使用"normal"将numpy数组另存为16位图像吗? (想)python吗? [英] Can I save a numpy array as a 16-bit image using "normal" (Enthought) python?
问题描述
是否可以使用任何常用的python软件包将numpy数组另存为16位图像(tif,png)? 这是过去是我上班的唯一方法,但是我需要安装FreeImage软件包,这有点烦人.
Is there any way to save a numpy array as a 16 bit image (tif, png) using any of the commonly available python packages? This is the only way that I could get to work in the past, but I needed to install the FreeImage package, which is a little annoying.
这似乎是一项非常基本的任务,所以我希望它应该被scipy覆盖,但是scipy.misc.imsave仅适用8位.
This seems like a pretty basic task, so I would expect that it should be covered by scipy, but scipy.misc.imsave only does 8-bits.
有什么想法吗?
推荐答案
一种替代方法是使用 pypng 一个>.您仍然必须安装另一个软件包,但这是纯Python,因此应该很容易. (在pypng源文件中实际上有一个Cython文件,但是它的使用是可选的.)
One alternative is to use pypng. You'll still have to install another package, but it is pure Python, so that should be easy. (There is actually a Cython file in the pypng source, but its use is optional.)
以下是使用pypng将numpy数组写入PNG的示例:
Here's an example of using pypng to write numpy arrays to 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)
# 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)
这是颜色输出:
这是灰度输出:
更新:我最近为名为 numpngw
提供了将numpy数组写入PNG文件的功能.该存储库具有一个setup.py
文件,用于将其作为软件包安装,但是基本代码位于单个文件numpngw.py
中,可以将其复制到任何方便的位置. numpngw
的唯一依赖项是numpy.
Update: I recently created a github repository for a module called numpngw
that provides a function for writing a numpy array to a PNG file. The repository has a setup.py
file for installing it as a package, but the essential code is in a single file, numpngw.py
, that could be copied to any convenient location. The only dependency of numpngw
is numpy.
这是一个脚本,可以生成与上面显示的图像相同的16位图像:
Here's a script that generates the same 16 bit images as those shown above:
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 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 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)
这篇关于我可以使用"normal"将numpy数组另存为16位图像吗? (想)python吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!