生成验证码 图片 ~(BytesIO,image file, base64), 文字
pip install simpel_captcha
from simpel_captcha import captcha
code = captcha()
from simpel_captcha import img_captcha
img, code = img_captcha()
print(f'图片对象: {image}')
print(f'验证码: {text}')
output:
图片对象: <PIL.Image.Image image mode=RGB size=100x30 at 0x1C7A0CAA7C0>
验证码: TjXP
from simpel_captcha import img_captcha
img, code = img_captcha(byte_stream=True)
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from simpel_captcha import img_captcha
app = FastAPI()
@app.get("/captcha", summary='图片验证码')
def image_captcha():
image, text = img_captcha(byte_stream=True)
# todo 将验证码缓存到Redis中
return StreamingResponse(content=image, media_type='image/jpeg')
from simpel_captcha import b64_captcha
obj, code = b64_captcha()
print(f'base64图片: {image}')
print(f'验证码: {text}')
output:
# 直接将base64字符串 复制到浏览器
base64图片: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AJYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxE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6ZNXF7YXUCubp2jBR2kYkMUwG+ZmOQBtDjIwC1bMdxpaQhbzwtLFeAYaGLTvNUt/suoKke5I96vaHp1yL+91rUIzFd3hCrAHyIol+6pxwW6kntnjvUyV9VuDVVrovx/yL/8Abek/9BOy/wDAhP8AGj+2dObiK6S4b+5bZmYD1ITJx79KwU0y48MTquk7PssrBI4Lid1gJJ4QkBvKbJyHVcSElXBcq56mGRpYI5HieFnUMY3ILISOh2kjI6cEj3NOLuCVVr4l93/BM661a2NpMPLvOUbrZTen+7WT4P1KCHwlpsbR3RZYcEpaSsOp6EKQa6l1V0ZGGVYYI9qgsrK306yitLWPy4Il2om4nA+p5pOL51Ly/wAiHTq86ldbdv8AgkP9oTnlNKvWQ/dbMS5HrhnBH0IB9qntriSfd5lnNb4xjzSh3fTax/Wp6Ks0jCSd3Jv7v8hocGQphsgA/dOOc9+nb/OaKdRQaBRRRQAUUUUANkjWWJ43GUcFWHqDWOvhPRkUKtvMqjgAXUoA/wDHq2GjR2RmRWKHchIztOCMj04JH4mnVEqcZfEkzOdKE9ZxTMu28PabaXCTwxSiRDlSbiRh+RbFaThyPkZQfcZ/rTqKcYxjpFWHGnCKtFW9NCL9+vGI398lf05o3zD70II/2Hyf1xUtFWHK+jZF5r/8+8n5r/jR9oT+7J/36b/CpaKA5ZLZ/wBfgVbn7Jd2k1rckeVMjRujkoWUjB9D07im2jTmIxSXUM0iMNso5LpxyyjADdRxxnnAztFymuiSDDqrDrgjNKyvcXLK9x1FQm0tmKk28RKnIJQcHp/Wl+zp/ek/7+t/jQVeXb8f+AS0VF5LDhZpFHpwf1IJqWm0TCopNrsFFFFI0P/Z
验证码: Q2of
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from simpel_captcha import b64_captcha
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/captcha", summary='图片验证码')
def image_captcha():
image, text = b64_captcha()
# todo 将验证码缓存到Redis中
return {"captcha_img": image, "captcha_code": text}
<!-- vue3 -->
<template>
<img :src="captcha" />
</template>
<script setup>
import { ref } from "vue";
import axios from "axios"
const captcha = ref("")
axios({
method: 'get',
url: 'https://127.0.0.1:8000/captcha'
}).then(res => captcha.value = res.data.captcha_img)
</script>
MIT License
Copyright (c) 2022 柒意
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.