tensorflow 读取图片错误(error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0 )的解析

在用tensorflow 自带的读取文件gfile模块中,调用API,如:

filename = directory + DIRECTORY_IMAGES + name + '.jpg'
image_data = tf.gfile.FastGFile(filename, 'rb').read()

常出现:error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0错误。

根本原因为:python3中的读取模式‘r’,'rb'存在较大差异,采用'r'模式文件python 解释器认为该文件内容为UTF-8的文本格式,但错误提示则提示该文件为二进制格式(通常图片所采用的格式),因此修改打开模式为‘rb’即可解决,如下:

image_data = tf.gfile.FastGFile(filename, 'rb').read()

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值