यह पीआईएल का grabscreen स्रोत है, यह किसी भी पैरामीटर को स्वीकार नहीं करता है, और यह पूरी स्क्रीन पकड़ लेता है और इसे बिटमैप में परिवर्तित करता है।
PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
{
int width, height;
HBITMAP bitmap;
BITMAPCOREHEADER core;
HDC screen, screen_copy;
PyObject* buffer;
/* step 1: create a memory DC large enough to hold the
entire screen */
screen = CreateDC(";DISPLAY", NULL, NULL, NULL);
screen_copy = CreateCompatibleDC(screen);
width = GetDeviceCaps(screen, HORZRES);
height = GetDeviceCaps(screen, VERTRES);
bitmap = CreateCompatibleBitmap(screen, width, height);
if (!bitmap)
goto error;
if (!SelectObject(screen_copy, bitmap))
goto error;
/* step 2: copy bits into memory DC bitmap */
if (!BitBlt(screen_copy, 0, 0, width, height, screen, 0, 0, SRCCOPY))
goto error;
/* step 3: extract bits from bitmap */
buffer = PyString_FromStringAndSize(NULL, height * ((width*3 + 3) & -4));
if (!buffer)
return NULL;
core.bcSize = sizeof(core);
core.bcWidth = width;
core.bcHeight = height;
core.bcPlanes = 1;
core.bcBitCount = 24;
if (!GetDIBits(screen_copy, bitmap, 0, height, PyString_AS_STRING(buffer),
(BITMAPINFO*) &core, DIB_RGB_COLORS))
goto error;
DeleteObject(bitmap);
DeleteDC(screen_copy);
DeleteDC(screen);
return Py_BuildValue("(ii)N", width, height, buffer);
error:
PyErr_SetString(PyExc_IOError, "screen grab failed");
DeleteDC(screen_copy);
DeleteDC(screen);
return NULL;
}
तो, जब मैं सिर्फ एक छोटे से गहरी जाओ, पाया सी दृष्टिकोण अच्छा
http://msdn.microsoft.com/en-us/library/dd144909(VS.85).aspx
है और अजगर ctypes है, तो यहाँ ctypes
>>> from ctypes import *
>>> user= windll.LoadLibrary("c:\\winnt\\system32\\user32.dll") #I am in windows 2000, may be yours will be windows
>>> h = user.GetDC(0)
>>> gdi= windll.LoadLibrary("c:\\winnt\\system32\\gdi32.dll")
>>> gdi.GetPixel(h,1023,767)
16777215 #I believe its white color of RGB or BGR value, #FFFFFF (according to msdn it should be RGB)
>>> gdi.GetPixel(h,1024,767)
-1 #because my screen is only 1024x768
उपयोग करते हुए मेरे दृष्टिकोण है आप फ़ंक्शन GetPixel के लिए एक रैपर लिख सकते हैं जैसे
from ctypes import windll
dc= windll.user32.GetDC(0)
def getpixel(x,y):
return windll.gdi32.GetPixel(dc,x,y)
तो फिर तुम getpixel(0,0)
, getpixel(100,0)
, आदि जैसे उपयोग कर सकते हैं ...
पुनश्च: मेरा Windows 2000 है, इसलिए मैं रास्ते में winnt
शब्दों में कहें, आप windows
करने के लिए इसे बदलने की जरूरत है सकते हैं या आप पूरी तरह से हटाने के chould पथ, बस user32.dll
और gdi32.dll
का उपयोग करना भी काम करना चाहिए। S.Mark के समाधान के
धन्यवाद एक गुच्छा। यह स्क्रीन को नोटिसबैली पढ़ने के लिए प्रेरित किया गया। मैं जितनी तेजी से पढ़ सकता हूं उतना तेज़ी से पढ़ सकता हूं। ;) – ThantiK
मैंने सीटीपीएस के लिए उपरोक्त वाक्यविन्यास की कोशिश की और लोड लोडर के साथ काम नहीं कर सका। रैपर फ़ंक्शन कोड ठीक वही है जो मैं ढूंढ रहा था। – Octipi
मुझे विश्वास नहीं है कि यह वास्तव में दूसरे के लिए कम से कम 30 बार समय के साथ काम करता है। मैंने गेट पिक्सेल की कोशिश की है और इसमें समृद्ध समय लगेगा। –