python - 請問png讀取出來到編碼是怎樣的?
問題描述
想了解一下圖像實際儲存的代碼形式是怎樣的?試著暴力讀取一下:
with open(’/usr/src/pycharm-2017.1/bin/pycharm.png’,’r’) as f: print(f.read())
結果出現(xiàn)了錯誤
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格式的文件來讀取:
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. javascript - react+百度地圖2. html5 - iOS的webview加載出來的H5網頁,怎么修改html標簽select的樣式字體?3. vue.js - vue+webpack+vue-router 部署到nginx服務器下,非根目錄,前后端怎樣配置文件?4. javascript - 為什么當index等于5的時候,不在當前頁面跳轉到百度?不跳轉的代碼在倒數(shù)第五行5. angular.js - 關于angular react vue 我們在什么實際的開發(fā)項目中使用?如何選擇?6. 手動啟動mysql服務出錯,1067錯誤,如何解決呢?7. index.php錯誤,求指點8. 這是什么情況???9. 請教一條mysql的sql語句寫法;10. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯誤
