Capturing/Recognizing Text In Another Window

  • Thread starter Scott Brady Drummonds
  • Start date
S

Scott Brady Drummonds

Hello, everyone,

I'd like to write a Python program that could capture the contents of
another Windows program and analyze the resultant bitmap. First, I have no
idea how to capture another window. Second, I'm not even certain if this
graphical problem is a Python issue or a Windows issue.

Can anyone offer me some guidance on this?

Thanks,
Scott
 
T

Tim Roberts

Scott Brady Drummonds said:
I'd like to write a Python program that could capture the contents of
another Windows program and analyze the resultant bitmap. First, I have no
idea how to capture another window. Second, I'm not even certain if this
graphical problem is a Python issue or a Windows issue.

Can anyone offer me some guidance on this?

If the thing you are trying to read is a text control, such as what Notepad
presents, you don't have to analyze a bitmap: you can fetch the text as a
string. You'll have to use the Windows API to do it, so you'll want to
read up on the win32gui and win32ui modules.

First, use a tool like spyxx to examine the app you want to scrape. Find
the ID of the control you want.

Now, fromm Python, you can use FindWindow or EnumWindows to find the
top-level app you want to manipulate. Then, you can use GetDlgItem to
fetch the window handle of the control with your desired ID. Then, use
win32gui.GetWindowText to fetch the text inside it.
 
J

JD

I too am interested in this if anyone can shed some light. If I am
understanding Scott's question, he is not refering to text that can be
captured by GetWindowText or equivalent. For example, if the other
program was a checkers game, how might one capture the screen so as to
know where the checker pieces are?
 
M

Miki Tebeka

Hello Scott,
I'd like to write a Python program that could capture the contents of
another Windows program and analyze the resultant bitmap. First, I have no
idea how to capture another window. Second, I'm not even certain if this
graphical problem is a Python issue or a Windows issue.

Can anyone offer me some guidance on this?

I'd use an automation program such as AutoIt
(http://www.hiddensoft.com/AutoIt)
to capture the scree usign ALT+PRTSC (there is an example in the help
on how to do this). Then use MS Paint or whatever to save the image to
the disc from the clipboard.

From there on you can open the bitmap and do whatever you want (try
PIL).

HTH.
Miki
 
J

JD

To answer my own question for the benefit of others who are
interested, do it like this:

import win32gui
import win32ui

DC = win32ui.GetActiveWindow().GetDC()
MemDC = DC.CreateCompatibleDC( None )
BitMap = win32ui.CreateBitmap()
BitMap.CreateCompatibleBitmap(DC, 400, 400)
MemDC.SelectObject( BitMap )
MemDC.BitBlt((0,0), (400, 400), DC, (0,0), 13369376)
BitMap.SaveBitmapFile(MemDC, "c:\mybitmap.bmp")
 
B

Bengt Richter

To answer my own question for the benefit of others who are
interested, do it like this:

import win32gui
import win32ui

DC = win32ui.GetActiveWindow().GetDC()
MemDC = DC.CreateCompatibleDC( None )
BitMap = win32ui.CreateBitmap()
BitMap.CreateCompatibleBitmap(DC, 400, 400)
MemDC.SelectObject( BitMap )
MemDC.BitBlt((0,0), (400, 400), DC, (0,0), 13369376)
BitMap.SaveBitmapFile(MemDC, "c:\mybitmap.bmp")

Just wondering what the BitBlt is doing with '0xcc0020'

Regards,
Bengt Richter
 
M

Michael Geary

MemDC.BitBlt((0,0), (400, 400), DC, (0,0), 13369376)
Just wondering what the BitBlt is doing with
'0xcc0020'

You can do a Google search on either the decimal or hex number to find out.
But I'll tell you anyway... :)

BitBlt is a multipurpose function that takes a ROP (Raster OPeration) code
that controls exactly what it does. 0xCC0020 is the ROP code SRCCOPY, i.e.
do a simple copy from the source to the destination.

-Mike
 
B

Bengt Richter

You can do a Google search on either the decimal or hex number to find out.
But I'll tell you anyway... :)

BitBlt is a multipurpose function that takes a ROP (Raster OPeration) code
that controls exactly what it does. 0xCC0020 is the ROP code SRCCOPY, i.e.
do a simple copy from the source to the destination.
D'oh. I knew that. I need to sleep better...

Regards,
Bengt Richter
 
N

News M Claveau /Hamster-P

Hi !

I obtain a traceback in line 4 : "win32ui : No windows is active." ???

@-salutations
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,921
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top