The problem is that you have bytes
JSON format only supports Unicode strings.
As mention bye @volcano63 you overwrite format,but also
Since Base64 encodes bytes to ASCII-only bytes,you can use that codec to decode the data so it can be dumped to JSON.
b'something'
in dictionary data.JSON format only supports Unicode strings.
As mention bye @volcano63 you overwrite format,but also
bytes
is a used name bye Python.>>> format <built-in function format> >>> bytes <class 'bytes'> >>> # A name that not used give NameError >>> foo Traceback (most recent call last): File "<string>", line 428, in runcode File "<interactive input>", line 1, in <module> NameError: name 'foo' is not definedYou should also show what you call function with,now do i see that in error message that it's png image.
Since Base64 encodes bytes to ASCII-only bytes,you can use that codec to decode the data so it can be dumped to JSON.
import base64 import json import subprocess def output_image(name, file_format, img_bytes): image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8" image_end = "END_IMAGE_0238jfw08fjsiufhw8frs" data = {} data['name'] = name data['format'] = file_format encoded = data['bytes'] = base64.encodestring(img_bytes) # Fix data['bytes'] = encoded.decode('ascii') print(image_start + json.dumps(data) + image_end) output_image('logo.png', 'png', open("logo.png", "rb").read())