class MyView(View):
def get(self, request):
# 创建一个画布
img = Image.new('RGB', (200, 100), (0, 50, 200))
# 创建一个画笔,并绑定画布
img_draw = ImageDraw.Draw(img, 'RGB')
# 创建一个字体,需要指定STATICFILES_DIRS = [ os.path.join(BASE_DIR,'statics')]
font = ImageFont.truetype('statics/gb.ttf', size=50, encoding='utf-8')
# 画什么东西,fill填充字体的颜色
img_draw.text((15, 25), "hello world", fill=(100, 100, 100), font=font)
for i in range(200):
img_draw.point((random.randint(0,200), random.randint(0,100)),fill=(random.randint(0,255), random.randint(0,255), random.randint(0,255)))
# 写入内容带内存缓冲区中,buffer相当于一个文件对象
from io import BytesIO
buffer = BytesIO()
img.save(buffer, 'png')
content = buffer.getvalue()
return HttpResponse(content, content_type='image/png')
python生成随机的图片验证码
最新推荐文章于 2024-09-13 15:02:21 发布