我目前正在使用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))
答案 0 :(得分:2)
您的imageData
可能已在某个时候转换为unicode。 urlsafe_b64decode
将str
值(又名bytes
)传递为 altchars ,因此b64decode
中的翻译需要str
个数据作为输入。< / p>
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
。
以下是b64decode
和urlsafe_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