admin管理员组

文章数量:1635997

参考:http://stackoverflow/questions/13572448/change-values-in-a-numpy-array

问题:img为numpy.ndarray,在使用img[img<0]=0 语句时,报错ValueError:assignment destination is read-only

(同一代码在win、ubuntu下没问题,而在centos7下报的错)

解决方案:修改ndarray的属性:

img.flags.writeable = True
补充:查看ndarray的属性
import numpy as np
help(np.ndarray.flags)
flags属性是关于数组内存布局的信息。其中,flags属性可以以字典形式修改,比如:
a.flags['WRITEABLE'] = True
也可以使用小写字母属性名:
a.flags.writeable = True
而缩写词('C' / 'F' 等)仅适用于字典形式

 

仅有属性UPDATEIFCOPY,WRITEABLE以及ALIGNED属性可以被使用者修改,有3种修改方式:

1.直接赋值:

a.flags.writeable = True

2.字典输入:

a.flags['WRITEABLE'] = True

3.使用函数ndarray.setflags:

help(np.ndarray.setflags)
 

 

 

 

本文标签: ndarraynumpyPythonValueErrorread