Python OCR提取普通數字圖形驗證中的數字

來源:互聯網
上載者:User

標籤:sam   reduce   截取   定位   load   operator   ted   new   art   

截取圖形驗證碼:

 1 # -*- coding: UTF-8 -*-  2 ‘‘‘ 3 Created on 2016年7月4日 4  5 @author: xuxianglin 6 ‘‘‘ 7 import os 8 import tempfile 9 import shutil10 11 from PIL import Image12 13 PATH = lambda p: os.path.abspath(p)14 TEMP_FILE = PATH(tempfile.gettempdir() + "/temp_screen.png")15 16 class Appium_Extend(object):17     def __init__(self, driver):18         self.driver = driver19 20     def get_screenshot_by_element(self, element):21         #先截取整個螢幕,儲存至系統臨時目錄下22         self.driver.get_screenshot_as_file(TEMP_FILE)23 24         #擷取元素bounds25         location = element.location26         size = element.size27         box = (location["x"], location["y"], location["x"] + size["width"], location["y"] + size["height"])28 29         #截取圖片30         image = Image.open(TEMP_FILE)31         newImage = image.crop(box)32         newImage.save(TEMP_FILE)33 34         return self35 36     def get_screenshot_by_custom_size(self, start_x, start_y, end_x, end_y):37         #自訂截取範圍38         self.driver.get_screenshot_as_file(TEMP_FILE)39         box = (start_x, start_y, end_x, end_y)40 41         image = Image.open(TEMP_FILE)42         newImage = image.crop(box)43         newImage.save(TEMP_FILE)44 45         return self46 47     def write_to_file( self, dirPath, imageName, form = "png"):48         #將截屏檔案複製到指定目錄下49         if not os.path.isdir(dirPath):50             os.makedirs(dirPath)51         shutil.copyfile(TEMP_FILE, PATH(dirPath + "/" + imageName + "." + form))52 53     def load_image(self, image_path):54         #載入靶心圖表片供對比用55         if os.path.isfile(image_path):56             load = Image.open(image_path)57             return load58         else:59             raise Exception("%s is not exist" %image_path)60 61     def same_as(self, load_image, percent):62         #對比圖片,percent值設為0,則100%相似時返回True,設定的值越大,相差越大63         import math64         import operator65 66         image1 = Image.open(TEMP_FILE)67         image2 = load_image68 69         histogram1 = image1.histogram()70         histogram2 = image2.histogram()71 72         differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, 73                                                          histogram1, histogram2)))/len(histogram1))74         if differ <= percent:75             return True76         else:77             return False

在定位元素中調用改方法:

1   vcode=self.driver.find_element_by_id("com.ajb.sp:id/changepsw_code_img")2   self.extend.get_screenshot_by_element(vcode).write_to_file("..\\resource\\vcode\\","image")3   self.assertTrue(os.path.isfile(‘..\\resource\\vcode\\image.png‘))4   image=Image.open(‘..\\resource\\vcode\\image.png‘)5   vcode_number=pytesseract.image_to_string(image)6   print u"截取到的驗證碼為:%s"%vcode_number

 

Python OCR提取普通數字圖形驗證中的數字

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.