1

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 Answers1

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
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • Thanks, I've tried it but it doesn't work neither : Traceback (most recent call last): File "", line 7, in bytes = struct.unpack('i', raw[ii:ii+4]) error: unpack requires a bytes object of length 4 Moreover, I don't want to use other libraries, we learnt in class to process images only using numpy and it worked fine until now :( – FrançoisBlas May 12 '19 at 14:43
  • OH NO okay, i was using the method to read text files for images it actually works with nympy and mathplotlib ! – FrançoisBlas May 12 '19 at 14:54