直接打开/加载图像为 numpy ndarray [英] Open / load image as numpy ndarray directly
问题描述
我曾经使用 scipy,它会将文件中的图像直接加载到 ndarray 中.
I used to use scipy which would load an image from file straight into an ndarray.
from scipy import misc
img = misc.imread('./myimage.jpg')
type(img)
>>> numpy.ndarray
但现在它给了我一个 DeprecationWarning
和 文档说它将在 1.2.0 中删除. 我应该使用 imageio.imread 代替.但是:
But now it gives me a DeprecationWarning
and the docs say it will be removed in 1.2.0. and I should use imageio.imread instead. But:
import imageio
img = imageio.imread('./myimage.jpg')
type(img)
>>> imageio.core.util.Image
我可以通过这样做来转换它
I could convert it by doing
img = numpy.array(img)
但这似乎很糟糕.有没有什么方法可以像我之前使用 scipy 的 misc.imread
(除了使用 OpenCV)那样将图像直接加载到 numpy 数组中?
But this seems hacky. Is there any way to load an image straight into a numpy array as I was doing before with scipy's misc.imread
(other than using OpenCV)?
推荐答案
imageio.imread
的结果已经是一个NumPy数组;imageio.core.util.Image
是一个 ndarray 子类,主要存在以便数组可以具有保存图像元数据的 meta
属性.
The result of imageio.imread
is already a NumPy array; imageio.core.util.Image
is an ndarray subclass that exists primarily so the array can have a meta
attribute holding image metadata.
如果你想要一个完全numpy.ndarray
类型的对象,你可以使用asarray
:
If you want an object of type exactly numpy.ndarray
, you can use asarray
:
array = numpy.asarray(img)
与numpy.array(img)
不同,这不会复制img
的数据.
Unlike numpy.array(img)
, this will not copy img
's data.
这篇关于直接打开/加载图像为 numpy ndarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!