问题
I'm trying to manipulate images but I can't get rid of that error :
fichier=open("photo.jpg","r")
lignes=fichier.readlines()
Traceback (most recent call last):
File "<ipython-input-32-87422df77ac2>", line 1, in <module>
lignes=fichier.readlines()
File "C:\Winpython\python-3.5.4.amd64\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 207: character maps to <undefined>
I've seen forums where people say to add encoding='utf-8' in the "open ..." but that won't work
回答1:
Your problem is with the open()
command. Your jpeg image is binary and you should use open('photo.jpg', 'rb')
.
Also do not use readlines()
for this file; this function should be used for character inputs.
This is an example...
import struct
with open('photo.jpg', 'rb') as fh:
raw = fh.read()
for ii in range(0, len(raw), 4):
bytes = struct.unpack('i', raw[ii:ii+4])
# do something here with your data
来源:https://stackoverflow.com/questions/56099686/python-encodedecode-error-unicodedecodeerror-charmap-codec-cant-decode-byte