解码 Python 中的 Base64 字符串并传递给 PIL?

Decoding Base64 String in Python 2 & pass to PIL?

我目前正在使用 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