您可以通过创建第三个 dtype uint8 数组和一个 bool 数组(它们一起比一个 uint16 数组更节省内存)。
np.putmask 对于避免临时数组很有用。
a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
c = 255 - b # a temp uint8 array here
np.putmask(a, c < a, c) # a temp bool array here
a += b
但是,正如@moarningsun 正确指出的那样,bool 数组与 uint8 数组占用的内存量相同,因此这不一定有用。可以通过避免在任何给定时间拥有多个临时数组来解决此问题:
a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
b = 255 - b # old b is gone shortly after new array is created
np.putmask(a, b < a, b) # a temp bool array here, then it's gone
a += 255 - b # a temp array here, then it's gone
这种方法用内存消耗换取 CPU。
另一种方法是预先计算所有可能的结果,即 O(1) 额外内存(即与数组大小无关):
c = np.clip(np.arange(256) + np.arange(256)[..., np.newaxis], 0, 255).astype(np.uint8)
c
=> array([[ 0, 1, 2, ..., 253, 254, 255],
[ 1, 2, 3, ..., 254, 255, 255],
[ 2, 3, 4, ..., 255, 255, 255],
...,
[253, 254, 255, ..., 255, 255, 255],
[254, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
c[a,b]
=> array([150, 250, 255], dtype=uint8)
如果您的数组非常大,这种方法是最节省内存的。同样,它的处理时间很昂贵,因为它用较慢的 2dim 数组索引取代了超快速的整数加法。
解释它是如何工作的
上面c 数组的构造使用了一个numpy 广播技巧。添加形状为(N,) 的数组和形状为(1,N) 的数组都广播为(N,N)-like,因此结果是所有可能和的NxN 数组。然后,我们剪辑它。我们得到一个满足:c[i,j]=min(i+j,255) 对于每个 i,j 的 2dim 数组。
剩下的就是使用花哨的索引来抓取正确的值。使用您提供的输入,我们访问:
c[( [100, 200, 250] , [50, 50, 50] )]
第一个索引数组是指第一个昏暗,第二个是第二个昏暗。因此,结果是一个与索引数组 ((N,)) 形状相同的数组,由值 [ c[100,50] , c[200,50] , c[250,50] ] 组成。