python - scipy.misc.imread 返回没有大小/形状的对象

标签 python scipy python-imaging-library

我有一个类似于 this one 的问题,虽然不相同。我的目标是使用 scipy.misc.imread 读取 TIFF 图像,然后使用函数返回的灰度值数组。当我对一张尺寸为 1280x960 像素的图像执行此操作时,我得到了我需要的东西。但是,当我尝试对更大的图像(6272x897 px)执行相同操作时,imread 返回以下对象:

<PIL.TiffImagePlugin.TiffImageFile image mode=LA size=6272x897 at 0x3906B48>

我想从该对象中提取数据,例如使用.getdata(),但该对象本身似乎没有任何形状或大小,就像在我上面链接的问题。 Ben 随后提出了以下解决方案:

pip uninstall PIL
brew install libjpeg
pip install PIL

但是,我正在使用 TIFF 文件,而不是 JPEG(libtiff 似乎已安装)。此外,读取较小的图像效果很好,而读取大图像会突然出现问题。有人知道发生了什么事吗?

最佳答案

Scipy 正在调用 np.array,但当它无法看到 __array__ 时,则会创建一个对象数组。您应该使用原始 PIL 并从那里读取信息,SO 中有很多关于如何将 PIL 对象转换为 Numpy 数组的问题。

之所以能与较小的一起工作,可能是因为尺寸太大,使得 PIL 对内存要小心,但我不确定。

另一种可行的方法是使用 Pillow(PIL 的一个分支),或 matplotlib

关于python - scipy.misc.imread 返回没有大小/形状的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675619/

相关文章:

python - 沿着 3D NumPy 数组的后 2 个维度移动窗口以获得 3D block

python - 使用线性方程创建径向插值圆

python-3.x - Ubuntu Tkinter 安装不包括 PyImagingPhoto

python - libjpeg.so.62 : cannot open shared object file: No such file or directory

python-imaging-library - PIL Python中的仿射变换

python - 将一个 pandas 数据帧分配为另一个 pandas 数据帧的子帧

python - Django 使用 .extra() 仅查询模型的一个字段,而不使用 .defer() 或 .only()

python - 使用 tf.GradientTape() 训练逻辑回归无法收敛

python - 将 B 样条写为分段三次

python - 矢量化 : Limit the increase in value between two subsequent numbers in Series (or list/array/whatever)