python - 請問png讀取出來到編碼是怎樣的?
問題描述
想了解一下圖像實際儲存的代碼形式是怎樣的?試著暴力讀取一下:
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請輸入代碼
這是為啥呢?請問除了用別的庫,有什么方法解決么?
問題解答
回答1:不要用文本文件格式打開非文本的文件!
PNG這種文件應該用binary格式的文件來讀?。?/p>
with open(’#filename#.png’,’rb’) as f: print(f.read())回答2:
試下用 ’rb’ 模式打開
with open(’/usr/src/pycharm-2017.1/bin/pycharm.png’,’rb’) as f: print(f.read())
相關文章:
1. css - 關于偽類背景問題2. html - 移動端radio無法選中3. apache - 怎么給localhost后面默認加上8080端口4. html5 - 如何實現圖中的刻度漸變效果?5. python - 用scrapy-splash爬取網站 為啥iframe下的內容沒有被返回6. mysql - 數據庫建字段,默認值空和empty string有什么區別 1107. python - django的模板預加載8. 關于Navicat連接到mysql,我改了root的密碼后,Navicat連接報錯1862?9. mysql起不來了,為什么?10. mysql里show full processlist,進程很少
