fromPILImportImage fromPILImportImagedraw, ImagefontImportRandomdefget_valid_img (Request):defGet_random_color ():return(Random.randint (0, 255), Random.randint (0,255), Random.randint (0,255)) Image= Image.new ("RGB", (250, 40), Get_random_color ())#generate five random charactersDraw =imagedraw.draw (image) font= Imagefont.truetype ("Static/blog/font/kumo.ttf", size=32) Temp= [] forIinchRange (5): Random_num= str (random.randint (0, 9)) Random_low_alpha= Chr (Random.randint (97, 122))#Get A~zRandom_upper_alpha = Chr (Random.randint (65, 90))#Get A~zRandom_char =Random.choice ([Random_num, Random_low_alpha, Random_upper_alpha]) Draw.text ((24+i*36, 0), Random_char, Get_random_color (), font=font)#Save Random characterstemp.append (Random_char)#Noise Noise Line #width=250 #height=40 #For i in range: #x1=random.randint (0,width) #x2=random.randint (0,width) #y1=random.randint (0,height) #y2=random.randint (0,height) #Draw.line ((x1,y1,x2,y2), Fill=get_random_color ()) # #For i in range: #Draw.point ([Random.randint (0, width), random.randint (0, height)], Fill=get_random_color ()) #x = Random.randint (0, width) #y = random.randint (0, height) #Draw.arc ((x, y, x + 4, y + 4), 0, Fill=get_random_color ()) #generating pictures in memory fromIoImportBytesio F=Bytesio () Image.Save (F,"PNG") Data=F.getvalue () f.close () Valid_str="". Join (temp)Print("Valid_str", Valid_str)#Set Sessionrequest.session["Valid_str"] =Valid_strreturnData
Custom Verification Code