python - 请问png读取出来到编码是怎样的?
天蓬老师
天蓬老师 2017-04-18 10:33:25
0
2
477

想了解一下图像实际储存的代码形式是怎样的?
试着暴力读取一下:

with open('/usr/src/pycharm-2017.1/bin/pycharm.png','r') as f:
    print(f.read())

结果出现了错误

Traceback (most recent call last):
  File "/home/noodle/PycharmProjects/untitled/test/picture_test.py", line 3, in <module>
    print(f.read())
  File "/usr/local/python34/lib/python3.4/codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
请输入代码

这是为啥呢?请问除了用别的库,有什么方法解决么?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(2)
左手右手慢动作

不要用文本文件格式打开非文本的文件!

PNG这种文件应该用binary格式的文件来读取:

with open('#filename#.png','rb') as f:
    print(f.read())
Peter_Zhu

试下用 'rb' 模式打开

with open('/usr/src/pycharm-2017.1/bin/pycharm.png','rb') as f:
    print(f.read())
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!