我有一个jpg文件在tmp
文件夹下。
upload_path = /tmp/resized-test.jpg
我一直在使用以下代码:
方法1
with open(upload_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
方法二
def imgToHex(file):
string = ''
with open(file, 'rb') as f:
binValue = f.read(1)
while len(binValue) != 0:
hexVal = hex(ord(binValue))
string += '\\' + hexVal
binValue = f.read(1)
string = re.sub('0x', 'x', string) # Replace '0x' with 'x' for your needs
return string
imgToHex(upload_path)
但是它们都不能按照我的要求工作。
"raise TypeError(repr(o) + \" is not JSON serializable\")"
我的返回代码如下:return {'header': { 'Content-Type': content_type }, 'body': encoded_string }
- Onur Degerli