tensorflow读取图像出现错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

#!/usr/bin/python
# -*- coding: utf-8 -*- import matplotlib.pyplot as plt
import tensorflow as tf filename = "/home/zzz/1-Work/Documents/2-Codes/learning/tf/17flowers/jpg/0/image_0001.jpg"
image_raw_data = tf.gfile.FastGFile(filename, "r").read() # 问题出在这里,mode应该为“rb”而不是“r” with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
tmp = img_data.eval()
print(tmp)
plt.imshow(tmp)
plt.show()
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8)
encoded_img = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile("/home/zzz/tf_tmp", "wb") as f:
f.write(encoded_img.eval())

原因:在

image_raw_data = tf.gfile.FastGFile(filename, "r").read()

这一行,读取的时候读取方式应该是“rb”,在读取模式只使用“r”的时候,python试图将一个byte-array转成utf-8字符串,这样python就会遇到utf-8的非法字符: 0xff in position 0,遇到这种情况时,可以将读取方式改为“rb”,这样在读取数据的时候,会将数据按照二进制读取,就不会有上述的解码问题。

ps:引起这个问题的另一个原因可能是所要读取的数据是按照utf-16编码的,在这种情况下,可以加关键字参数,encoding=“utf-16”解决。

ref: https://stackoverflow.com/questions/42339876/error-unicodedecodeerror-utf-8-codec-cant-decode-byte-0xff-in-position-0-in

tensorflow UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte的更多相关文章

  1. TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

    我使用VS2013  Python3.5  TensorFlow 1.3  的开发环境 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...

  2. TensorFlow学习('utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

    使用语句: image_raw_data = tf.gfile.GFile("./picture.jpg", "r").read() 读取图像时报错如下: Un ...

  3. 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

    'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 觉得有用的话,欢迎一起讨论相互学习~Follow Me 今 ...

  4. Python:出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 0: invalid continuation byte问题

    我在导入一个csv文件的时候出现了一个问题 报错的内容是这样的: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in positio ...

  5. Pandas读取文件报错UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

    pandas读取文件时报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start by ...

  6. binlog2sql 解析日志失败 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte

    python35 ./binlog2sql.py -h... -P... -u... -p... -B --start-file="mysql-bin.091940" --star ...

  7. 第一篇:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

    需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 解决参考: 代码: def verification_ssh(host,username,password,port,roo ...

  8. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

    需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 解决参考: 代码: def verification_ssh(host,username,password,port,roo ...

  9. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

    用pandas打开csv文件可能会出现这种情况,原因可能是excel自己新建一个*.csv文件时候容易出错.进入文件另存为,然后选择csv文件即可.

随机推荐

  1. KindEditor 上传文件 在Asp.net中的使用

    以前一直用FCK编辑器,因为配置比较简单,但是发现Kindeditor这个编辑器更加好看,更加灵活,就用了下. 但是发现在上传文件的时候,出现了大问题,弄了我好久的时间,为了记录下,或许能帮助到您,共 ...

  2. PHP写的手机端网站,可以打包成app吗,怎么打包?

    8:13:36 沐歌-重庆 2018/1/19 8:13:36 PHP写的手机端网站,可以打包成app吗,怎么打包 风太大-淮安 2018/1/19 8:14:58 变色龙 沐歌-重庆 一般用什么打包 ...

  3. 使用 mysqldump 备份时的一些参数

    因为还没有用到 ThinkPHP 的迁移组件,暂时使用 mysqldump 来备份,并版本控制. 有几个参数需要用到. --skip-dump-date 不要完成时间. --skip-extended ...

  4. tomcat源码阅读之StandardContext

    Context实例表示一个具体的web应用程序,其中包含一个或者多个Wrapper实例,每个Wrapper表示一个具体的servlet定义.StandardContext类是Context接口的标准实 ...

  5. azkaban:java任务调度系统

    github:https://github.com/azkaban/azkaban 官方网站:https://azkaban.github.io/ 好的介绍文章:http://srcct.com/20 ...

  6. MyBatis持久层框架使用总结 转载

    MyBatis持久层框架使用总结   MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google co ...

  7. 使用scrapy框架爬取自己的博文(3)

    既然如此,何不再抓一抓网页的文字内容呢? 谷歌浏览器有个审查元素的功能,就是按树的结构查看html的组织形式,如图: 这样已经比较明显了,博客的正文内容主要在div 的class = cnblogs_ ...

  8. Python 中的sort()排序

    v = [1, 3, 5, 2, 4, 6] v.sort() print(v) # [1, 2, 3, 4, 5, 6] v2 = [(1, 2), (2, 2), (2, 3), (3, 1)] ...

  9. 如何判断事务是否完成,SqlTransaction

    SqlConnection sconn = null; SqlCommand scmd = null; SqlTransaction strans = null; try { string sqlIn ...

  10. spring eureka required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.

    spring在集成第三方过程很容易出现类名相同,且基本作用相同的类.这样给初学者带来一定的困惑. 导致用错类而出现以下问题. required a bean of type 'com.netflix. ...