Python3解决使用Pillow(PIL)图片验证码时,报AttributeError: module 'string' has no attribute 'lowercase'的解决办法

详情报错内容如下

builtins.AttributeError
AttributeError: module 'string' has no attribute 'lowercase'

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 225, in generate_captcha
return self.captcha("")
File "/Users/zhangbin/Desktop/Python04/ihome_python04/ihome/utils/captcha/captcha.py", line 213, in captcha
name = "".join(random.sample(string.lowercase + string.uppercase + '3456789', 24))
AttributeError: module 'string' has no attribute 'lowercase'
image.png

问题原因

string.lowercase出现了问题。

        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))
        text = "".join(self._text)
        out = BytesIO()
        image.save(out, format=fmt)
        if path:
            image.save(os.path.join(path, name), fmt)
        return name, text, out.getvalue()

解决办法

先导入from io import StringIO,然后将string.lowercase改成string.ascii_lowercase

        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 = BytesIO()
        image.save(out, format=fmt)
        if path:
            image.save(os.path.join(path, name), fmt)
        return name, text, out.getvalue()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容