详情报错内容如下
builtins.TypeError
TypeError: string argument expected, got 'bytes'
Traceback (most recent call last)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/zhangbin/Desktop/Python04/ihome_python04/ihome/api_1_0/verify_code.py", line 20, in get_image_code
name, text, image_data = captcha.generate_captcha()
File "/Users/zhangbin/Desktop/Python04/ihome_python04/ihome/utils/captcha/captcha.py", line 223, in generate_captcha
return self.captcha("")
File "/Users/zhangbin/Desktop/Python04/ihome_python04/ihome/utils/captcha/captcha.py", line 216, in captcha
image.save(out, format=fmt)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/PIL/Image.py", line 1994, in save
save_handler(self, fp, filename)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 761, in _save
ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/PIL/ImageFile.py", line 502, in _save
fp.write(d)
TypeError: string argument expected, got 'bytes'

image.png
问题原因
out = StringIO()该行代码出现了问题。
def captcha(self, path=None, fmt='JPEG'):
image = Image.new('RGB', (self.width, self.height), (255, 255, 255))
image = self.background(image)
image = self.text(image, self.fonts, drawings=['warp', 'rotate', 'offset'])
image = self.curve(image)
image = self.noise(image)
image = self.smooth(image)
# name = "".join(random.sample(string.lowercase + string.uppercase + '3456789', 24))
name = "".join(random.sample(string.ascii_lowercase + string.ascii_lowercase + '3456789', 24))
text = "".join(self._text)
out = StringIO()
image.save(out, format=fmt)
if path:
image.save(os.path.join(path, name), fmt)
return name, text, out.getvalue()
解决办法
先导入from io import BytesIO,然后将out = StringIO()改成out = BytesIO()
def captcha(self, path=None, fmt='JPEG'):
image = Image.new('RGB', (self.width, self.height), (255, 255, 255))
image = self.background(image)
image = self.text(image, self.fonts, drawings=['warp', 'rotate', 'offset'])
image = self.curve(image)
image = self.noise(image)
image = self.smooth(image)
# name = "".join(random.sample(string.lowercase + string.uppercase + '3456789', 24))
name = "".join(random.sample(string.ascii_lowercase + string.ascii_lowercase + '3456789', 24))
text = "".join(self._text)
# out = StringIO()
out = BytesIO()
image.save(out, format=fmt)
if path:
image.save(os.path.join(path, name), fmt)
return name, text, out.getvalue()