鱼C论坛

 找回密码
 立即注册
查看: 1617|回复: 1

[已解决]图片验证码问题

[复制链接]
发表于 2023-6-13 10:44:59 | 显示全部楼层 |阅读模式
5鱼币
from app.cs import check_code
from io import BytesIO
def yzm(request):
    img,code_staing = check_code()
    print(code_staing)

    stream = BytesIO()
    img.save(stream, 'png')
    stream.getvalue()
    return  HttpResponse(stream.getvalue())


这个代码 可以在前台 正常显示内容 不过是在内存里面的


如果我要把 code_staing  这个图片验证码 写在指的的文件里面要怎么做呢

不是cs.py文件里面 是调用cs 里面
最佳答案
2023-6-13 10:45:00
您的代码显示了如何生成一个验证码,并且在内存中创建了一个图像流,然后将这个图像流返回给HttpResponse。但是如果你想把验证码图片保存到一个特定的文件,你可以在Python的 PIL 库的 Image 对象上调用 save() 方法,像这样:
from app.cs import check_code
from io import BytesIO
def yzm(request):
    img,code_string = check_code()
    print(code_string)

    # Save the image to a file
    img.save('/path/to/your/file.png')

    stream = BytesIO()
    img.save(stream, 'png')
    stream.getvalue()
    return HttpResponse(stream.getvalue())

在上面的代码中, /path/to/your/file.png  是你想要保存图像的文件的路径。请确保你有写入该文件的权限,且文件路径的目录存在。你也可以将 code_string (验证码)一同写入文件,但它需要被写入一个文本文件,这将涉及到一个不同的处理方式。

如果你想将验证码一同写入,可以考虑以下代码:
from app.cs import check_code
from io import BytesIO
def yzm(request):
    img,code_string = check_code()
    print(code_string)

    # Save the image to a file
    img.save('/path/to/your/file.png')

    # Save the code_string to a text file
    with open('/path/to/your/textfile.txt', 'w') as f:
        f.write(code_string)

    stream = BytesIO()
    img.save(stream, 'png')
    stream.getvalue()
    return HttpResponse(stream.getvalue())

在这里, /path/to/your/textfile.txt  是你想要保存验证码的文本文件的路径。这段代码将把验证码写入一个文本文件中。

最佳答案

查看完整内容

您的代码显示了如何生成一个验证码,并且在内存中创建了一个图像流,然后将这个图像流返回给HttpResponse。但是如果你想把验证码图片保存到一个特定的文件,你可以在Python的 PIL 库的 Image 对象上调用 save() 方法,像这样: 在上面的代码中, /path/to/your/file.png 是你想要保存图像的文件的路径。请确保你有写入该文件的权限,且文件路径的目录存在。你也可以将 code_string (验证码)一同写入文件,但它需要被写入 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-13 10:45:00 | 显示全部楼层    本楼为最佳答案   
您的代码显示了如何生成一个验证码,并且在内存中创建了一个图像流,然后将这个图像流返回给HttpResponse。但是如果你想把验证码图片保存到一个特定的文件,你可以在Python的 PIL 库的 Image 对象上调用 save() 方法,像这样:
from app.cs import check_code
from io import BytesIO
def yzm(request):
    img,code_string = check_code()
    print(code_string)

    # Save the image to a file
    img.save('/path/to/your/file.png')

    stream = BytesIO()
    img.save(stream, 'png')
    stream.getvalue()
    return HttpResponse(stream.getvalue())

在上面的代码中, /path/to/your/file.png  是你想要保存图像的文件的路径。请确保你有写入该文件的权限,且文件路径的目录存在。你也可以将 code_string (验证码)一同写入文件,但它需要被写入一个文本文件,这将涉及到一个不同的处理方式。

如果你想将验证码一同写入,可以考虑以下代码:
from app.cs import check_code
from io import BytesIO
def yzm(request):
    img,code_string = check_code()
    print(code_string)

    # Save the image to a file
    img.save('/path/to/your/file.png')

    # Save the code_string to a text file
    with open('/path/to/your/textfile.txt', 'w') as f:
        f.write(code_string)

    stream = BytesIO()
    img.save(stream, 'png')
    stream.getvalue()
    return HttpResponse(stream.getvalue())

在这里, /path/to/your/textfile.txt  是你想要保存验证码的文本文件的路径。这段代码将把验证码写入一个文本文件中。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表