python抠图

窗口命令pip install PIL

from PIL import Image

from selenium import webdriver
d=webdriver.Firefox()
d.get('http://www.baidu.com')
# 窗口最大化
d.maximize_window()
time.sleep(1)
# 进行全屏截图
d.get_screenshot_as_file('c:\mark666.png')
# 定位需要截图的元素位置
baidu=d.find_element('id','su')
# 获取element的顶点坐标
left=baidu.location['x']
top=baidu.location['y']
# 获取element的宽、高
elementwidth=baidu.location['x']+baidu.size['width']
elementheight=baidu.location['y']+baidu.size['height']
# 打开截图
p=Image.open('c:\mark666.png')
# 使用Image的crop函数,从截图中再次截取我们需要的区域,进行图片剪切
p=p.crop((left,top,elementwidth,elementheight))
# 保存我们需要的的图片
p.save(r'c:\mark6.png')

猜你喜欢

转载自www.cnblogs.com/it-xian/p/10176125.html
今日推荐