np arrays being immutable - “assignment destination is read-only”

前端 未结 4 2118
礼貌的吻别
礼貌的吻别 2020-11-30 04:30

FD** - I am a Python newb as well as a stack overflow newb as you can tell. I have edited the question based on comments.

My goal is to read a set of PNG files, crea

4条回答
  •  悲&欢浪女
    2020-11-30 04:58

    Since numpy version 1.16.0 the following doesn't work anymore:

    img = np.asarray(Image.open(filename))
    img.setflags(write=1)
    

    The problem is that now OWNDATA is set to False and you can't set WRITEABLE flag to True. Therefore you should simply do the following:

    img = np.array(Image.open(filename))
    

    This will make a copy of array when casting it from Pillow object to numpy array. However I tested time performance in numpy 1.16.0 and haven't found any noticable difference between both methods.

提交回复
热议问题