Raf*_*ael 6 python numpy python-3.x
我正在使用Python 3.4(Numpy 1.9.2和PIL.Image 1.1.7)开发Ubuntu 14.04.这是我做的:
>>> from PIL import Image
>>> import numpy as np
>>> img = Image.open("./tifs/18015.pdf_001.tif")
>>> arr = np.asarray(img)
>>> np.shape(arr)
(5847, 4133)
>>> arr.dtype
dtype('bool')
# all of the following four cases where I incrementally increase
# the number of rows to 700 are done instantly
>>> v = arr[1:100,1:100].sum(axis=0)
>>> v = arr[1:500,1:100].sum(axis=0)
>>> v = arr[1:600,1:100].sum(axis=0)
>>> v = arr[1:700,1:100].sum(axis=0)
# but suddenly this line makes Python crash
>>> v = arr[1:800,1:100].sum(axis=0)
fish: Job 1, “python3” terminated by signal SIGSEGV (Address boundary error)
Run Code Online (Sandbox Code Playgroud)
对我而言,就像Python突然耗尽内存一样.如果是这种情况 - 如何为Python分配更多内存?正如我从htop看到的那样,我的32GB内存容量甚至没有被远程耗尽.
您可以在此处下载TIFF图像.
如果我创建一个空的布尔数组,明确设置像素然后应用求和 - 然后它工作:
>>> arr = np.empty((h,w), dtype=bool)
>>> arr.setflags(write=True)
>>> for r in range(h):
>>> for c in range(w):
>>> arr.itemset((r,c), img.getpixel((c,r)))
>>> v=arr.sum(axis=0)
>>> v.mean()
5726.8618436970719
>>> arr.shape
(5847, 4133)
Run Code Online (Sandbox Code Playgroud)
但是这种"解决方法"并不十分令人满意,因为复制每个像素需要太长时间 - 也许有更快的方法?
我可以使用从 Ubuntu 存储库安装的 numpy v1.8.2/PIL v1.1.7 重现您的段错误。
如果我使用 pip 在 virtualenv 中安装 numpy 1.8.2(仍然使用 Ubuntu 存储库中的 PIL v1.7.1),那么我将不再看到段错误。
如果我执行相反的操作(使用 pip 安装 PIL v1.1.7,并使用 Ubuntu 存储库中的 numpy v1.8.2),我仍然会遇到段错误。
这让我相信这是由 numpy 中的一个旧错误引起的。我无法在 numpy 的问题跟踪器中找到一个好的候选者,但我怀疑更新 numpy (例如从当前源或通过 pip)可能会解决该问题。
一种解决方法是在创建数组之前将图像模式转换为"P"(无符号 8 位整数),然后将其转换回布尔值:
arr2 = np.asarray(img.convert("P")).astype(np.bool)
v = arr2[1:800,1:100].sum(axis=0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
794 次 |
| 最近记录: |