Python OCR extracts numbers from ordinary digital graphics validation

Source: Internet
Author: User

To intercept the graphics verification Code:

1 #-*-coding:utf-8-*-2 " "3 Created on July 4, 20164 5 @author: Xuxianglin6 " "7 ImportOS8 ImportTempfile9 ImportShutilTen  One  fromPILImportImage A  -PATH =LambdaP:os.path.abspath (P) -Temp_file = PATH (Tempfile.gettempdir () +"/temp_screen.png") the  - classAppium_extend (object): -     def __init__(self, driver): -Self.driver =Driver +  -     defget_screenshot_by_element (self, Element): +         #capture the entire screen first and store it in the system temp directory A self.driver.get_screenshot_as_file (temp_file) at  -         #get element bounds -Location =element.location -Size =element.size -box = (location["x"], location["y"], location["x"] + size["width"], location["y"] + size["Height"]) -  in         #Capturing Pictures -Image =Image.open (temp_file) toNewImage =image.crop (Box) + Newimage.save (temp_file) -  the         return Self *  $     defget_screenshot_by_custom_size (self, start_x, start_y, end_x, end_y):Panax Notoginseng         #Custom Intercept Range - self.driver.get_screenshot_as_file (temp_file) thebox =(start_x, start_y, end_x, end_y) +  AImage =Image.open (temp_file) theNewImage =image.crop (Box) + Newimage.save (temp_file) -  $         return Self $  -     defWrite_to_file (self, dirpath, imageName, form ="PNG"): -         #Copy the screenshot file to the specified directory the         if  notOs.path.isdir (dirpath): - os.makedirs (Dirpath)WuyiShutil.copyfile (Temp_file, PATH (Dirpath +"/"+ ImageName +"."+form)) the  -     defload_image (Self, image_path): Wu         #Load target image for comparison -         ifOs.path.isfile (image_path): AboutLoad =Image.open (image_path) $             returnLoad -         Else: -             RaiseException ("%s is not exist"%image_path) -  A     defsame_as (self, Load_image, percent): +         #compare picture, the percent value is set to 0, then 100% returns True when similar, the larger the value, the greater the difference the         ImportMath -         Importoperator $  theImage1 =Image.open (temp_file) theImage2 =Load_image the  theHISTOGRAM1 =Image1.histogram () -HISTOGRAM2 =Image2.histogram () in  thediffer = MATH.SQRT (reduce (Operator.add, list (map) (LambdaA, B: (a) **2,  theHISTOGRAM1, histogram2))/Len (histogram1)) About         ifDiffer <=percent: the             returnTrue the         Else: the             returnFalse

To invoke the change method in the anchor element:

1VCODE=SELF.DRIVER.FIND_ELEMENT_BY_ID ("com.ajb.sp:id/changepsw_code_img")2Self.extend.get_screenshot_by_element (Vcode). Write_to_file (".. \\resource\\vcode\\","Image")3Self.asserttrue (Os.path.isfile ('.. \\resource\\vcode\\image.png'))4Image=image.open ('.. \\resource\\vcode\\image.png')5Vcode_number=pytesseract.image_to_string (image)6   PrintU"the intercepted Captcha is:%s"%vcode_number

Python OCR extracts numbers from ordinary digital graphics validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.