Hi,
This is my first time on daniweb, so if I've posted in the wrong section or there's already a similarly-themed thread or I've done something wrong I apologise. Anyway, I'm hoping someone can prod me in the right direction with a problem I'm having right now.
I'm currently working on a project which aims to form a sort of public desktop network (i.e. the screens of connected computers can be viewed remotely - think Remote Assistance but more public and less confined to LANs or small-medium businesses). Nothing too flash or high-quality - just for my own interest at this stage.
I'm writing the application in Python 2.6.1, and am implementing it using the standard Tkinter module interface. For image handling I am also using the Python Imaging Library (PIL).
Anyway, I'm not quite sure how to approach a problem I've already encountered. I want to regularly capture the contents of the screen, which in itself is thankfully made incredibly trivial by PIL (doing the same in C++ in the past was a pain).
However, the program will be displaying the contents of other screens and of its own screen, and therefore if it includes itself in the screenshot then when the screens are viewed within the program a sort of recursive image situation emerges (if that makes sense) - I hope you can picture that.
So, ideally, I need a way for the screen capture to somehow ignore the window of my application (I've said active window in the thread title to save space but obviously it may or may not be the active window) and store the remainder of the screen (including what is hidden behind my window).
Right now, I'm using ImageGrab.grab()
to capture the screen, converting the returned result into a format Tkinter can natively handle using ImageTk.PhotoImage()
, then storing it in a label (just to test the screen capture process, not part of broader program). I am repeating this process at regular, quick intervals (right now every 200ms, but this is just an arbitrarily selected interval - although it will obviously need to be pretty small for my purposes).
Here's the rough code to give you an idea (in the capture_screen function, a method of my application class - some commented lines explained below):
def capture_screen(self):
#temporarily hide window so recursive screenshot is avoided
#self.master.withdraw()
screen_contents = ImageGrab.grab()
#self.master.deiconify()
size = win_width, win_height
screen_contents = screen_contents.resize(size, Image.ANTIALIAS)
screen_image = ImageTk.PhotoImage(screen_contents)
self.screen_displayer.config(image = screen_image)
self.screen_displayer.image = screen_image
self.screen_displayer.grid(row = 0, column = 0)
#update display
self.master.after(200, self.capture_screen)
You might notice the commented out self.master.withdraw()
and self.master.deiconify()
- my first idea was to for the briefest of instants hide the application so that at the moment the screenshot was taken the application would be invisible. I hoped that this would happen so quickly (in three code lines, though this I guess means little next to the function of the lines) that the visibility toggling would be unseen by the user, not even a flicker. Unsurprisingly, it was false hope and the result was worse than a small flicker, rendering the program pretty unusable.
Another idea that I have vaguely is the option of attempting it in C++ - which from my experience would give me lower-level access to all the mechanisms needed and thus the control to implement the selective screen capture, and then interfacing this C++ with my Python application. However, to be honest, although C++ can in its own way be fun, the sheer effort that always seems to accompany learning how to do something new in the language puts me quite off this option. The less micromanaging nature of Python is what attracted me to it in the first place, and I'd rather do it solely in Python if at all possible (at the moment at least, until future speed requirements may need to be met).
So anyway, sorry for the long post and the long ramble. I don't usually approach other programmers for help - solving my problems is usually an independent process and usually eventually works out. But I just thought it makes more sense to try and find someone who may have tried something similar before and could save me lots of internet trawling (not that I haven't already tried a bit of hunting).
If anyone could help it'd be greatly appreciated.
Thanks in advance,
mrpoate