def check_code(request): img, code = rd_check_code() stream = BytesIO() img.save(stream, 'png') request.session['code'] = code return HttpResponse(stream.getvalue())
def check_code(request): # 读取硬盘中的文件,在页面显示 # f = open("staticc/imgs/md.jpg","rb") # data = f.read() # f.close() # return HttpResponse(data) # from PIL import Image # f = open("code.png","wb") # img = Image.new(mode="RGB",size=(120,30),color=(255,255,255)) # img.save(f,"png") # f.close() # f = open("code.png","rb") # data = f.read() # f.close() # from PIL import Image,ImageDraw,ImageFont # from io import BytesIO # 在内存中开辟一块空间 # f = BytesIO() # 写到内存,在内存中开辟一块空间 # img = Image.new(mode="RGB",size=(120,30),color=(255,255,255)) # 图片对象 # draw = ImageDraw.Draw(img,mode="RGB") # 创建画笔对象 # draw.point([10,10],fill="red") # 红色 # draw.point([10,10],fill=(255,255,255)) # RGB颜色 # draw.line((15,10,50,30),fill="red") # 画线 # draw.line((45,20,100,100),fill=(255,0,255)) # 画线 # # draw.arc((0,0,30,30),0,90,fill="red") # 画圆,0-90度 # # # draw.text([0,0],"python","red") # # # font = ImageFont.truetype("kumo.ttf",28) # # draw.text([0,0],"python",(0,255,0),font=font) # import random # # # char_list = [] # # for i in range(5): # # char = chr(random.randint(65,90)) # # chhar_list.append(char) # # "".join(char_list) # # # v = "".join[chr(random.randint(65,90)) for i in range(5)] # # char_list = [] # for i in range(5): # char = chr(random.randint(65,90)) # char_list.append(char) # font = ImageFont.truetype("static/myfont/kumo.ttf",28) # draw.text([i*24, 0], char, (random.randint(0,255), random.randint(0,255), random.randint(0,255)), font=font) # img.save(f,"png") # 写到内存 # data = f.getvalue() # # code="".join(char_list) # request.session["code"] = code from io import BytesIO # 在内存中开辟一块空间 from utils.random_check_code import rd_check_code img,code = rd_check_code() stream = BytesIO() img.save(stream,"png") # 图片以png的格式存储到内存stream中 request.session["code"] = code # 验证码存储在session中 return HttpResponse(stream.getvalue())
def codecheck(request): from io import BytesIO # 在内存中开辟一块空间 from utils.random_check_code import rd_check_code img, code = rd_check_code() stream = BytesIO() img.save(stream, "png") # 图片以png的格式存储到内存stream中 request.session["code"] = code # 验证码存储在session中 print(request.session.get("code")) return HttpResponse(stream.getvalue())
def check_code(request): """ 验证码生成 :param request: :return: """ img, code = rd_check_code() stream = BytesIO() img.save(stream, "png") request.session["code"] = code return HttpResponse(stream.getvalue())
def check_code(request): from io import BytesIO from utils.random_check_code import rd_check_code img,code = rd_check_code() stream = BytesIO()#开辟一个内存空间,类似于文件句柄 print(stream)#io空间,<_io.BytesIO object at 0x06D6EAE0> print(img)#pillow生成的图片对象 img.save(stream,'png') print(stream) print(stream.getvalue())#bytes类型的图片信息,返回前端生成图片 print(img) request.session['code'] = code#将生成的随机字符串存到session用于验证 return HttpResponse(stream.getvalue())
def check_code(request): """ 生成随机验证码 :param request: :return: """ from io import BytesIO from utils.random_check_code import rd_check_code img, code = rd_check_code() stream = BytesIO() img.save(stream, 'png') request.session['code'] = code return HttpResponse(stream.getvalue())
def produce_code(request): img, code = rd_check_code() mem_file_handle = BytesIO() img.save(mem_file_handle, 'png') request.session['code'] = code return HttpResponse(mem_file_handle.getvalue())
def check_code(request): #读取硬盘中的文件在页面上显示 # f=open('static/imgs/yingjie.jpg','rb') # data=f.read() # f.close() # return HttpResponse(data) #创建文件并保存 #pip3 install pillow # from PIL import Image # f=open('code.png','wb') #创建一个白色的图片 # img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # img.save(f,'png') # f.close() # return HttpResponse("...") # from PIL import Image,ImageDraw,ImageFont # from io import BytesIO # f=BytesIO() # img = Image.new(mode='RGB', size=(120, 30), color=(187, 255, 255)) # #创建画笔,用于在图片上画任意内容 # draw = ImageDraw.Draw(img, mode='RGB') # #画点 # # 第一个参数:表示坐标 # # 第二个参数:表示颜色 # draw.point([10,10],fill="red") # draw.point([80,10],fill=(255,255,255)) # #画线 # # 第一个参数:表示起始坐标和结束坐标 # # 第二个参数:表示颜色 # draw.line((15,10,50,50),fill=(0,255,0)) # #画圈 # # 第一个参数:表示起始坐标和结束坐标(圆要画在其中间) # # 第二个参数:表示开始角度 # # 第三个参数:表示结束角度 # # 第四个参数:表示颜色 # draw.arc((0, 0, 30, 30), 0, 360, fill="red") # #选择字体,写字。前面是字体,后面是字体大小 # # font = ImageFont.truetype("kumo.ttf", 28) # # 第一个参数:表示起始坐标 # # 第二个参数:表示写入内容 # # 第三个参数:表示颜色 # # draw.text([0, 0], 'python', "red",font=font) # # #生成随机字符 # import random # # char_list=[] # # for i in range(5): # # char=chr(random.randint(65,90)) # # char_list.append(char) # # ''.join(char_list) # # # v=''.join([ chr(random.randint(65,90)) for i in range(5) ]) # # char_list=[] # for i in range(5): # char=chr(random.randint(65,90)) # font = ImageFont.truetype("kumo.ttf", 28) # draw.text([i*24, 0], char, (random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font) # char_list.append(char) # code=''.join(char_list) # # #保存图片 # img.save(f, 'png') # #获取生成的图片 # data = f.getvalue() # #保存在session # request.session['code']=code from io import BytesIO from utils.random_check_code import rd_check_code stream = BytesIO() img, code = rd_check_code() img.save(stream, 'png') request.session['code'] = code return HttpResponse(stream.getvalue())
def check_code(request): # 读取硬盘中的文件,在页面显示 # f = open('static/imgs/yj.png','rb') # data = f.read() # f.close() # return HttpResponse(data) # pip3 install pillow # from PIL import Image # f = open('code.png','wb') # img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # img.save(f,'png') # f.close() # # f = open('code.png','rb') # data = f.read() # f.close() # from PIL import Image,ImageDraw,ImageFont # from io import BytesIO """ f = BytesIO() img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) draw = ImageDraw.Draw(img, mode='RGB') draw.point([10, 10], fill="red") draw.point([20, 10], fill=(255, 255, 255)) draw.line((15,10,50,50), fill='red') draw.line((45,20,100,100), fill=(0, 255, 0)) draw.arc((0,0,30,30),0,360,fill="red") # draw.text([0,0],'python',"red") # font = ImageFont.truetype("kumo.ttf", 28) # draw.text([0,0],'python',(0, 255, 0),font=font) import random # char_list = [] # for i in range(5): # char = chr(random.randint(65,90)) # char_list.append(char) # ''.join(char_list) # v = ''.join([ chr(random.randint(65,90)) for i in range(5)]) char_list = [] for i in range(5): char = chr(random.randint(65,90)) char_list.append(char) font = ImageFont.truetype("kumo.ttf", 28) draw.text([i*24,0],char,(random.randint(0,255), random.randint(0,255), random.randint(0,255)),font=font) img.save(f,'png') data = f.getvalue() code = ''.join(char_list) request.session['code'] = code """ from io import BytesIO from utils.random_check_code import rd_check_code img, code = rd_check_code() stream = BytesIO() img.save(stream, 'png') request.session['code'] = code return HttpResponse(stream.getvalue())