我的下面有一个jpg文件tmp
folder.
upload_path = /tmp/resized-test.jpg
我一直在使用下面的代码:
Method 1
with open(upload_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
Method 2
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)
但他们都没有按照我想要的方式工作。
您可以使用 binascii 包来实现此目的。它将把它转换成十六进制字符串。
import binascii
filename = 'test.png'
with open(filename, 'rb') as f:
content = f.read()
print(binascii.hexlify(content))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)