-1

I'm using Python 3.8. I want to get image byte data into a JSON object. So I tried this

with open(os.path.join(dir_path, "../image_data", "myimg.jpg"), mode='rb') as img_file:
    image_data = img_file.read().decode("utf-16")
    my_json_data = {
        "image_data": image_data
        ...
    }

but the image_data = line is giving this error:

UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 0-1: illegal UTF-16 surrogate

What's the proper way to load data for inclusion into a JSON object?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

decode and encode work on character data. You use them to covert between character encoding formats, such as utf-8 and ASCII. It doesn't make sense to take pure binary data -- such as an image -- and try to convert it to characters. Not every binary value can be converted to character; most character formats leave a few values as reserved or unused.

What you need is a simple raw byte format. Read the file as a sequence of bytes; this preserves the binary form, making it easy for your eventual JSON consumer to utilize the information.

Prune
  • 76,765
  • 14
  • 60
  • 81