2016-04-01 8 views
1

: Python 2'de denemek AncakKod Çözme Tabanı64 Python 2'de Dize & PIL'ye Geçir? Şu anda bir base64 dize deşifre ve resim gibi PIL onu geçmek için Python 3'te aşağıdaki satırı kullanıyorum

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

alıyorum:

TypeError: character mapping must return integer, None or unicode 

Python2'de bunu nasıl çözebilir ve PIL'e geçirebilirim?

Fazla Bilgi:

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

Tam geri izleme ekleyebilir misiniz? –

+0

Elbette. Sadece cevabımı güncelledim. – KingPolygon

+0

'imageData' muhtemelen 'unicode' şeklindedir. http://stackoverflow.com/questions/10367302/what-is-producing-typeerror-character-mapping-must-return-integer-in-this-p –

cevap

2

Sizin imageData muhtemelen bir noktada unicode dönüştürüldü. urlsafe_b64decodestr değerlerini (aka bytes) altchars olarak geçirir ve b64decode'daki çeviriler giriş olarak str verilerini bekler.

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, '-_') 

sizin imageData yeniden kodlamak veya unicode kodunu çözmek değil kaynağını saptamak.

b64decode ve urlsafe_b64decode'un unicode numaralarını almada nasıl farklı davrandığını gösteren bir örnek.

>>> 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