Keras.preprocessing.image中的img_to_array执行的具体操作

顾名思义:img_to_array就是讲图片转化成数组,约等于numpy.asarray。

以下用一个小例子分享:

from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import preprocess_input
from PIL import Image
import numpy as np
image =  Image.open("./lena.jpg")
target = (512,512)

 

image = image.resize(target)
image = img_to_array(image)

得到结果:

 

...
    img_to_array(image)
改为
    np.asarray(image)
 
得到结果:
...
如果不修改继续进行预处理:
image = np.expand_dims(image, axis=0)  #拓展维度
image=preprocess_input(image)          #预处理
 
就会报错:

 

TypeError: Cannot cast ufunc subtract output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'
 
 
所以要将图片转化为浮点型数组: image = np.asarray(image,'f')
 

 

 
如果出现报错:
 

ValueError: output array is read-only

这是只需要在加上:
image.flags.writeable = True

就可以啦。

image = image.resize(target)
image = np.asarray(image,'f')
#image.flags.writeable = True
image = np.expand_dims(image, axis=0)  #拓展维度
image=preprocess_input(image)          #预处理

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值