![]() |
|
[Python] Interact with program on background - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Interact with program on background (/showthread.php?tid=5853005) |
Interact with program on background - Mohinder - Jun 23, 2018 Ola fellow-coders, I want to use pytesseract to read text out of an screen once in a while. The screen I want to extract information out can be on the background, would it be possible to interact with it (leaving it in the background)? Thanks in advance, Mo RE: Interact with program on background - hunter24957 - Jun 23, 2018 When you say "screen" you mean a "window", right? Assuming you are using Windows and that pytesseract will need an image for character recognition, we'd need to take a snapshot of the background window, more specifically the area of the window where the text lies and then feed that to pytesseract. Looking at the MSDN Windows API documentation we can see a function called PrintWindow. The PrintWindow function is defined as follows: PHP Code This function will allow us to take a snapshot of the given background window and save the resulting bitmap to a buffer. With a little bit of research we can find out how to call this function in Python: http://www.upgradeyourtechlife.com/blog/?p=82 Ok, so now we have a way of obtaining a snapshot of the Window area of a background window, now we have to crop the snapshot to the area of the window containing the text. Since the documentation I have linked you uses Pillow to cast the buffer, we can simply use the Image.crop() method that takes a rectangle of the region to crop to. It is defined below: Code Image.crop(box=None)Now we can successfully grab an image of the text on a background window without having to move the window to the front. I have included some code I have written for this down below, enjoy! 👍 PHP Code RE: Interact with program on background - Mohinder - Jun 24, 2018 Thanks for this very detailed information. :) RE: Interact with program on background - hunter24957 - Jun 24, 2018 (Jun 24, 2018 02:03 AM)Mohinder Wrote: Thanks for this very detailed information. :) No problem man, enjoy! 🤝 |