Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:02:33+00:00 2026-05-13T09:02:33+00:00

Currently I’m using a pixel reader via AutoItv3 to perform some actions in a

  • 0

Currently I’m using a pixel reader via AutoItv3 to perform some actions in a program that is running direct X; A game. Right now the program works fine but as an exercise I’ve been rewriting it in python. Right now I can do:

import ImageGrab  # Part of PIL
    image = ImageGrab.grab() #Define an area to capture.
    rgb = image.getpixel((1, 90)) #What pixel do we want?

And that grabs the pixel info I want just fine, but I’m doing this quite rapidly (needs to be done 3x a second or faster), but the result is that it majorly affects the framerate of this DirectX-based game.

Is there a faster way in Python to read a specific screen pixel? Even limiting this one to running every 0.3 seconds is causing more strain than it really should (I actually figured python would be faster than AutoIt for this particular purpose, hence the reason I’m trying it)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:02:34+00:00Added an answer on May 13, 2026 at 9:02 am

    This is the PIL’s grabscreen source, Its does not accept any parameters, and Its grab the whole screen and convert it to bitmap.

    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;
    }
    

    So, when I just go a little deep, found C approach is good

    http://msdn.microsoft.com/en-us/library/dd144909(VS.85).aspx

    And Python has ctypes, so here is my approach using ctypes (in Windows 10, winnt has been replaced with Windows):

    >>> 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
    

    You could write a wrapper for function GetPixel like this

    from ctypes import windll
    dc= windll.user32.GetDC(0)
    
    def getpixel(x,y):
        return windll.gdi32.GetPixel(dc,x,y)
    

    Then you can use like getpixel(0,0), getpixel(100,0), etc…

    PS: Mine is Windows 2000, so I put winnt in the path, you may need to change it to windows or you chould completely remove path, just using user32.dll and gdi32.dll should work too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.