【场景】
测试使用图片验证码登录
接口返回的是图片的base64编码,我们需要
1、将base64编码转为图片
2、去识别图片中的验证码,然后再拿验证码去做后续的图片验证码登录测试
【参考链接】
https://blog.51cto.com/u_16213316/10678659
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=python%E8%A7%A3%E6%9E%90base64%E7%9A%84%E5%9B%BE%E7%89%87%E4%B8%AD%E7%9A%84%E6%96%87%E5%AD%97&fenlei=256&oq=python%25E8%25A7%25A3%25E6%259E%2590base64%25E5%259B%25BE%25E7%2589%2587%25E4%25B8%25AD%25E7%259A%2584%25E5%2586%2585%25E5%25AE%25B9&rsv_pq=f0b3d25e00b50591&rsv_t=9318eS%2FzPfSIiP0S3HbskcjMS8Xj3wmHhHpLInUt%2Fc7L6rmeZfnK%2FkaE4YQ&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_btype=t&inputT=6010&rsv_sug3=85&rsv_sug2=0&rsv_sug4=7622
https://blog.csdn.net/u014642921/article/details/136093403
【准备】
参考链接,需要先下载一个识别图片的软件,再配置环境变量,再配置什么cmd参数,还有pip一些包
【代码】
1 import base64 2 from PIL import Image 3 import io 4 import pytesseract 5 pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe' 6 7 8 picture64="/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAoAHgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM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TIpY7GBHR7uNWVhGoIIJ4INbI8XeGf+hi0j/wNj/8AiqKKAHjxf4Y/6GPSP/A6P/4qnjxh4XIwfEekf+B0X/xVFFAFbT9a8D6Y0j2esaDC8rFpHS7iBcnqSc98ZrQHjLwt/wBDLo//AIHRf/FUUUAYfiafwV4nSAXPibRlaJXUN9tiPDLj+9k844JwRnjOCNTRfEHhHR9JtrCPxLoO2BNgMd3Ei4zngbzj86KKALCeIfAy3BuBrfh7ziCPMN3Du568578Z+gq4njLwin3fEuhr9L6If+zUUUAY3iWb4c+LbVYNW13Q5Cn3JV1CIOnrg7qpeGNN+F/hWYT2WvaLJcjpNNqMTMOCDj5uM7j+npRRQB2I8ceEf+hp0T/wYRf/ABVSDxz4R/6GrQ//AAYRf/FUUUAY3jLxn4WuvAviC3t/EujTTy6bcpHHHfRMzsYmAAAbJJPGKKKKAP/Z" 9 10 # 解码Base64字符串 11 image_data = base64.b64decode(picture64) 12 13 # 将字节流转换为PIL图片 14 image = Image.open(io.BytesIO(image_data)) 15 16 # 使用Tesseract进行OCR识别 17 picNumber = pytesseract.image_to_string(image) 18 19 #不知道为啥转成数字之后,后面有两个\n换行符,所以改为取前四个数字 20 picNumber=picNumber[:4] 21 22 print(picNumber)View Code
写法2
标签:python,image,base64,接口,pytesseract,rsv,识别,picNumber,图片 From: https://www.cnblogs.com/hushuer/p/18325913