I want to get the photo(thumbnail) of a window which I already have it's handle (hwnd) using python in windows os.
Asked
Active
Viewed 1,134 times
2
-
Have you looked at http://stackoverflow.com/a/2447245/530160 ? – Nick ODell Dec 19 '12 at 02:16
-
Try searching. You can start here - http://stackoverflow.com/questions/3260559/how-to-get-a-window-or-fullscreen-screenshot-in-python-3k-without-pil – Aesthete Dec 19 '12 at 02:17
-
1@NickODell - OP wants to use python. – Aesthete Dec 19 '12 at 02:18
-
What do you intend to do with the image once you have it? – Mark Ransom Dec 19 '12 at 02:18
-
@Aesthete I know. I don't think there's a python library for it - he'll have to call out to the windows API. – Nick ODell Dec 19 '12 at 02:19
-
@Aesthete, you can make the same calls from Python using `windll` from `ctypes`, e.g. http://mail.python.org/pipermail/python-win32/2007-December/006555.html Unfortunately there's a lot of fiddly detail required that prevents me from leaving a detailed answer. – Mark Ransom Dec 19 '12 at 04:31
-
@MarkRansom - Yep. He'll have to deal with the win32 API. Luckily python has abstracted alot of the DLL handling boilerplate into modules like `win32gui`. I got a solution working so I posted in the answers below. – Aesthete Dec 19 '12 at 05:31
-
Sorry for late response, I had an exam. I want to implement the Alt+Tab windows like functionality. – K.H.A. Dec 31 '12 at 15:47
-
@Mark Ransom I wanted to write a plugin for [link](http://launchy.net/) to switch between different open windows using their title and showing small thumbnail for them. – K.H.A. Dec 31 '12 at 15:55
1 Answers
2
From the link i posted in your question comments, I was able to get a sample working that thumbnails my python interpreter window.
from PIL import ImageGrab, Image
import win32gui
hwnd = 2622054 # My python intepreter window
thumbnailsize = 128, 128
# Set the current window to your window
win32gui.SetForegroundWindow(hwnd)
# Get the size of the window rect.
bbox = win32gui.GetWindowRect(hwnd)
# Grab the image using PIL and thumbnail.
img = ImageGrab.grab(bbox)
img.thumbnail(thumbnailsize, Image.ANTIALIAS)
# Save.
img.save('c:/test/test.png')