Selenium + PhantomJS + python图片全屏截取+定位坐标+抠图+图片识别
1.原图片(全屏截图)
#设置浏览器参数,伪装成浏览器
dcap = dict(DesiredCapabilities.PHANTOMJS) #设置userAgent
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 ")
#打开浏览器
wb = webdriver.PhantomJS(desired_capabilities=dcap)
url = "http://m.maoyan.com/shows/149?_v_=yes"
wb.maximize_window()
wb.get(url)
imgelement = wb.find_element_by_xpath('//div[@class="showtime-list"]/table/tbody/tr[1]/td[3]/span/span')
#图片坐标
locations = imgelement.location
print(locations)
#图片大小
sizes = imgelement.size
print(sizes)
# 构造指数的位置
rangle = (int(locations['x']),int(locations['y']),int(locations['x'] + sizes['width']),int(locations['y'] + sizes['height']))
print rangle
# 截取当前浏览器
path1 = "/home/bingcheng/maoyan_shotImage/maoyan_PSeat_image/" + str(2)
path2 = "/home/bingcheng/maoyan_shotImage/maoyan_seat_image/" + str(2)
wb.save_screenshot(str(path1) + ".png")
# 打开截图切割
img = Image.open(str(path1) + ".png")
jpg = img.crop(rangle)
jpg.save(str(path2) + ".png")
print "图片截取成功!"
#图像识别
image = Image.open(str(path2) + ".png")
image.load()
code = pytesseract.image_to_string(image)
print "图片内容识别为:"
print code
#关闭浏览器
wb.close()
print "结束"
2.定位坐标截图后的图片
3.识别后
64
交流群:453908562