Deutsch   English   Français   Italiano  
<tkinter-20240909172337@ram.dialup.fu-berlin.de>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: POTD: Getting the pixels from tkinter
Date: 9 Sep 2024 16:36:31 GMT
Organization: Stefan Ram
Lines: 46
Expires: 1 Jul 2025 11:59:58 GMT
Message-ID: <tkinter-20240909172337@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de u7pHk+aahguJo6qg38kZtQjdB6T2skiud4exdkcNQEkeCR
Cancel-Lock: sha1:mtAzkOQeEspjNjeBopSWhy2pyi0= sha256:s7uza7kiENhMlhm4chJ8VAvOCBBrHAeZaozxrnMtvys=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
	Distribution through any means other than regular usenet
	channels is forbidden. It is forbidden to publish this
	article in the Web, to change URIs of this article into links,
        and to transfer the body without this notice, but quotations
        of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
	services to mirror the article in the web. But the article may
	be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 2857

  It's pretty much impossible to grab the color of pixels from
  tkinter using just the standard libraries. But seriously,
  who wants to mess around with extension libraries, right?

  Here's a thought that might actually let you read the color of a
  pixel from the tkinter GUI using only Python's standard libraries!
  The trick is to split the work: I'll show you how to do it on
  Windows, and then you can whip up the equivalent code for Linux!

  The following POTD (Program of the Day) illustrates how to read a
  pixel from the screen using only standard Python on Windows. You can
  set up any tkinter interface you want and then use this technique
  to pull the color of individual pixels from that interface!

import ctypes

# Constants for color retrieval
GDI32 = ctypes.WinDLL('gdi32')
USER32 = ctypes.WinDLL('user32')

# Function to get the pixel color
def get_pixel_color(x, y):
    # Get the device context for the entire screen
    hdc = USER32.GetDC(0)
    
    # Get the color of the pixel at (x, y)
    pixel = GDI32.GetPixel(hdc, x, y)
    
    # Release the device context
    USER32.ReleaseDC(0, hdc)
    
    # Extract RGB values
    blue = pixel & 0xFF
    green = (pixel >> 8) & 0xFF
    red = (pixel >> 16) & 0xFF
    
    return (red, green, blue)

for col in range( 36 ):
    for row in range( 72 ):
        r, g, b = get_pixel_color( row, col )
        a = (r+g+b)/3
        print( end = '#' if a < 128 else ' ' )
    print()