实现对屏幕指定内容的操作

 

import time
import win32gui, win32ui, win32con, win32api

from PIL import Image
import pytesseract


# 将app窗口置为当前窗口,等效点,选3个,点击
time.sleep(10)
l = [(15, 81), (23, 120), (22, 126)]
for i in l:
x, y = i
win32api.SetCursorPos([x, y])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y)

#下拉内屏幕3次,鼠标位置相对win屏幕不变
dwData = -100
for i in range(20):
dwData -= 20
y -= (i+1)*120
win32api.SetCursorPos([x, y])
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, x, y, dwData, 0)
time.sleep(3)
# 鼠标位置保持不变
y += (i+1)*120

#筛选
#选取矩形区,进行字符串字符串识别
img_fn = 'my.screenshot.png'
hwnd = 0 # 窗口的编号,0号表示当前活跃窗口
# 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
hwndDC = win32gui.GetWindowDC(hwnd)
# 根据窗口的DC获取mfcDC
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
# mfcDC创建可兼容的DC
saveDC = mfcDC.CreateCompatibleDC()
# 创建bigmap准备保存图片
saveBitMap = win32ui.CreateBitmap()
# 获取监控器信息
MoniterDev = win32api.EnumDisplayMonitors(None, None)
w = MoniterDev[0][2][2]
h = MoniterDev[0][2][3]
# w,h=(150,30)
# print w,h   #图片大小
# 为bitmap开辟空间

saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
# 高度saveDC,将截图保存到saveBitmap中
saveDC.SelectObject(saveBitMap)
# 截取从左上角(0,0)长宽为(w,h)的图片
# saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
# 截取目标区域图片
cx, cy = (120, 277)
w, h = (300, 100)
saveDC.BitBlt((cx, cy), (w, h), mfcDC, (cx, cy), win32con.SRCCOPY)
saveBitMap.SaveBitmapFile(saveDC, img_fn)


im = Image.open(img_fn)
cropedIm = im.crop((cx, cy, cx + w, cy + h))
cropedIm.save('my.screenshot.crop.png')

# MoniterDev = win32api.EnumDisplayMonitors(None, None)


# import os.path
#
# # img_dir = 'C:\\Users\\sas\\PycharmProjects\\py_win_to_unix\\crontab_chk_url\\personas\\trunk\\plugins\\pyos\\'
# # img_n = 'en1.PNG'
# # img_n = 'cn1.PNG'
# # img_n = 'bd_res.PNG'
# # img = '{}{}'.format(img_dir, img_n)
# # chk_exist=os.path.isfile(img)
#
# # d=Image.open('pil0.PNG')
# # 上面都是导包,只需要下面这一行就能实现图片文字识别
# # text = pytesseract.image_to_string(Image.open(img), lang='chi_sim') #chi_sim eng
text = pytesseract.image_to_string(Image.open('my.screenshot.crop.png'), lang='chi_sim') # chi_sim eng
text_en = pytesseract.image_to_string(Image.open('my.screenshot.crop.png'), lang='eng') # chi_sim eng
#
#一旦点击区域文字和背景的对比度降低,识别为空字符串
print(text)
print(text_en)


#进入个人页

win32api.SetCursorPos([cx, cy+(i+1)*120])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, cx, cy+(i+1)*120)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, cx, cy+(i+1)*120)
time.sleep(0.05)


time.sleep(12)
bx,by=(43,91)

win32api.SetCursorPos([bx, by])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, bx, by)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, bx, by)
time.sleep(0.05)




import os
os._exit(2323)



posted @ 2018-02-02 11:53  papering  阅读(406)  评论(0编辑  收藏  举报