import os
from PIL import Image
import io
import pytesseract
import requests
from fake_useragent import UserAgent
class Tool:
@classmethod
def get_tool(cls):
return cls()
def get_user_agent(self,headers):
ua = UserAgent()
headers['User-Agent'] = ua.random
return headers
def get_cookie(self,cookie_str):
c_b = cookie_str.split(',')
cookies = {}
for cookie in c_b:
k,v = cookie.split('=')
cookies[k] = v
return cookies
def tessseract_img(self,img_url):
# 识别图片文字数字
img_body = requests.get(img_url).content
image_stream = Image.open(io.BytesIO(img_body))
image_stream = image_stream.convert('L')
if os.path.exists('a.jpg'):
os.remove('a.jpg')
image_stream.save('a.jpg')
Image.open(r'a.jpg')
return pytesseract.image_to_string(image_stream,lang='eng')