gpt4 book ai didi

python - 在Python 2中解码Base64字符串并传递给PIL?

转载 作者:行者123 更新时间:2023-12-01 04:01:50 28 4
gpt4 key购买 nike

我目前正在 Python 3 中使用以下行来解码 Base64 字符串并将其作为图像传递给 PIL:

img = Image.open(BytesIO(base64.urlsafe_b64decode(imageData)))    

但是,当我在 Python 2 中尝试时,我得到:

TypeError: character mapping must return integer, None or unicode

如何解码它并将其传递给 python2 中的 PIL?

更多信息:

Traceback (most recent call last)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Me/Desktop/Project/app.py", line 43, in predict
image = decodeImageFromBase64String(imageData)
File "/Users/Me/Desktop/Project/app.py", line 84, in decodeImageFromBase64String
img = Image.open(BytesIO(base64.urlsafe_b64decode(imageData)))
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 112, in urlsafe_b64decode
"""
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 71, in b64decode
if altchars is not None:
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 36, in _translate
return s.translate(''.join(translation))

最佳答案

您的imageData可能已在某个时刻转换为unicode。 urlsafe_b64decodestr 值(又名 bytes)作为 altchars 传递,因此 b64decode 中的翻译> 期望 str 数据作为输入。

def urlsafe_b64decode(s):
"""Decode a string encoded with the standard Base64 alphabet.

s is the string to decode. The decoded string is returned. A TypeError
is raised if the string is incorrectly padded or if there are non-alphabet
characters present in the string.

The alphabet uses '-' instead of '+' and '_' instead of '/'.
"""
return b64decode(s, '-_')

重新编码您的 imageData 或修复源,使其不将其解码为 unicode

以下示例说明了 b64decodeurlsafe_b64decode 在接收 unicode 时的不同行为。

>>> base64.b64encode('tervehdys')
'dGVydmVoZHlz'
>>> s = unicode(base64.b64encode('tervehdys'))
>>> base64.b64decode(s)
'tervehdys'
>>> base64.urlsafe_b64decode(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "base64.py", line 112, in urlsafe_b64decode
return b64decode(s, '-_')
File "base64.py", line 71, in b64decode
s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
File "base64.py", line 36, in _translate
return s.translate(''.join(translation))
TypeError: character mapping must return integer, None or unicode

关于python - 在Python 2中解码Base64字符串并传递给PIL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36348865/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com