问题是,当你除以4时,你正在创建浮点值,这些值不想进入ints的数组中
如果要使用putmask,并避免试图转换为float的问题,那么可以使用floor division(//)将值更改为int:np.putmask(result, result>255, result//4)
>>> result
array([[[ 72, 88, 82],
[ 72, 206, 66],
[242, 70, 64]],
[[210, 97, 85],
[ 68, 113, 218],
[255, 87, 64]],
[[127, 85, 173],
[112, 98, 147],
[223, 228, 100]]])
备选方案1:
将result数组转换为浮点dtype,并使用原始的putmask:
^{pr2}$
如果需要,您甚至可以将其转换回int:result = result.astype(int)
array([[[ 72, 88, 82],
[ 72, 206, 66],
[242, 70, 64]],
[[210, 97, 85],
[ 68, 113, 218],
[255, 87, 64]],
[[127, 85, 173],
[112, 98, 147],
[223, 228, 100]]])
备选方案2:
完全取消putmask,您可以获得如下所需的结果:result[result > 255] = result[result > 255] / 4
>>> result
array([[[ 72, 88, 82],
[ 72, 206, 66],
[242, 70, 64]],
[[210, 97, 85],
[ 68, 113, 218],
[255, 87, 64]],
[[127, 85, 173],
[112, 98, 147],
[223, 228, 100]]])