屏幕截图 从安卓模拟器中识别出屏幕文字

 

Python实现屏幕截图的两种方式 - weiyinfu - 博客园 https://www.cnblogs.com/weidiao/p/8051280.html

 

def window_capture(filename):
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]
# 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)
saveBitMap.SaveBitmapFile(saveDC, filename)




 https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370(v=vs.85).aspx

 

BOOL BitBlt(
  _In_ HDC   hdcDest,
  _In_ int   nXDest,
  _In_ int   nYDest,
  _In_ int   nWidth,
  _In_ int   nHeight,
  _In_ HDC   hdcSrc,
  _In_ int   nXSrc,
  _In_ int   nYSrc,
  _In_ DWORD dwRop
);

 

Parameters

hdcDest [in]

A handle to the destination device context.

nXDest [in]

The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.

nYDest [in]

The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.

nWidth [in]

The width, in logical units, of the source and destination rectangles.

nHeight [in]

The height, in logical units, of the source and the destination rectangles.

hdcSrc [in]

A handle to the source device context.

nXSrc [in]

The x-coordinate, in logical units, of the upper-left corner of the source rectangle.

nYSrc [in]

The y-coordinate, in logical units, of the upper-left corner of the source rectangle.

dwRop [in]

A raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.

 

import time
import win32gui, win32ui, win32con, win32api

# 将app窗口置为当前窗口
time.sleep(3)
l = [(262, 95), (250, 507)]
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)

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)
# 截取目标区域图片
x, y = (16, 444)
# w,h=(150,30)
saveDC.BitBlt((x, y), (200, 30), mfcDC, (x, y), win32con.SRCCOPY)
saveBitMap.SaveBitmapFile(saveDC, img_fn)

MoniterDev = win32api.EnumDisplayMonitors(None, None)

import os

os._exit(1213)


# 当前活跃窗口的全部
def window_capture(filename):
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]
# 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)
saveBitMap.SaveBitmapFile(saveDC, filename)


# beg = time.time()
# for i in range(10):
# window_capture("haha.jpg")
# end = time.time()
# print(end - beg)

import time

import numpy as np
from PIL import ImageGrab, Image

# 每抓取一次屏幕需要的时间约为1s,如果图像尺寸小一些效率就会高一些
# beg = time.time()
# debug = False
# for i in range(10):
# img = ImageGrab.grab(bbox=(250, 161, 1141, 610))
# img = np.array(img.getdata(), np.uint8).reshape(img.size[1], img.size[0], 3)
#
# ImageGrab.grabclipboard()
#
# # img_fn='{}{}{}'.format('img',i,'.PNG')
# # # Image.open(img_fn,'w')
# # Image.new('RGB',)
# # with open(img_fn,'wb') as fo:
# # fo.write(img)
# end = time.time()
# print(end - beg)


 

 

 

 

 

 

import time
import win32gui, win32ui, win32con, win32api

# 将app窗口置为当前窗口
time.sleep(3)
l = [(262, 95), (250, 507)]
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)

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)
# 截取目标区域图片
x, y = (16, 444)
# w,h=(150,30)
saveDC.BitBlt((x, y), (200, 30), mfcDC, (x, y), win32con.SRCCOPY)
saveBitMap.SaveBitmapFile(saveDC, img_fn)

from PIL import Image
im = Image.open(img_fn)
cropedIm = im.crop((x, y, 216, 474))
cropedIm.save('my.screenshot.crop.png')


MoniterDev = win32api.EnumDisplayMonitors(None, None)

from PIL import Image
import pytesseract
# 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='eng') #chi_sim eng
#
print(text)


 

 百度域名

 

 

 

 

 

 

posted @ 2018-01-30 14:30  papering  阅读(976)  评论(0编辑  收藏  举报