最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python實(shí)現(xiàn)獲取屏幕像素顏色值

 更新時(shí)間:2025年06月08日 11:35:21   作者:蠟筆小電芯  
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)獲取屏幕像素顏色值,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、一個(gè)小工具,按住F10鍵,顏色值會(huì)跟著顯示。

完整代碼

import tkinter as tk
import pyautogui
import keyboard
 
class ColorViewer:
    def __init__(self):
        self.root = tk.Tk()
        self.root.overrideredirect(True)  # 無(wú)邊框
        self.root.wm_attributes("-topmost", 1)  # 最前
        self.root.configure(bg="black")
        self.root.geometry("140x60")
 
        self.color_frame = tk.Frame(self.root, width=24, height=48, bg="white")
        self.color_frame.place(x=5, y=5)
 
        self.hex_label = tk.Label(self.root, text="#------", font=("Consolas", 13), bg="black", fg="white")
        self.hex_label.place(x=35, y=5)
 
        self.coord_label = tk.Label(self.root, text="(0000,0000)", font=("Consolas", 11), bg="black", fg="white")
        self.coord_label.place(x=35, y=30)
 
        self.update_loop()
 
        self.root.withdraw()  # 初始隱藏
        self.root.mainloop()
 
    def update_loop(self):
        if keyboard.is_pressed("F10"):
            x, y = pyautogui.position()
            r, g, b = pyautogui.screenshot(region=(x, y, 1, 1)).getpixel((0, 0))
            hex_color = "#{:02x}{:02x}{:02x}".format(r, g, b)
 
            self.color_frame.configure(bg=hex_color)
            self.hex_label.configure(text=hex_color)
            self.coord_label.configure(text=f"({x},{y})")
 
            # 自動(dòng)移動(dòng)窗口,避免遮擋鼠標(biāo)
            screen_w = self.root.winfo_screenwidth()
            screen_h = self.root.winfo_screenheight()
            win_w, win_h = 140, 60
            offset = 20
 
            pos_x = x + offset
            pos_y = y + offset
            if pos_x + win_w > screen_w:
                pos_x = x - win_w - offset
            if pos_y + win_h > screen_h:
                pos_y = y - win_h - offset
 
            self.root.geometry(f"{win_w}x{win_h}+{pos_x}+{pos_y}")
            self.root.deiconify()
        else:
            self.root.withdraw()
 
        self.root.after(30, self.update_loop)  # 循環(huán)檢查
 
if __name__ == "__main__":
    ColorViewer()

二、樣式示例

三、方法補(bǔ)充

python獲取像素顏色

使用image模塊中的getpixel函數(shù)獲得像素值。

GetPixel函數(shù)檢索指定坐標(biāo)點(diǎn)的像素的RGB顏色值。

函數(shù)原型:COLORREF GetPixel(HDC hdc, int nXPos, int nYPos)

參數(shù):

hdc:設(shè)備環(huán)境句柄。

nXPos:指定要檢查的像素點(diǎn)的邏輯X軸坐標(biāo)。

nYPos:指定要檢查的像素點(diǎn)的邏輯Y軸坐標(biāo)。

示例:

import Image
import sys
im = Image.open(sys.argv[1])
width = im.size[0]
height = im.size[1]
print "/* width:%d */"%(width)
print "/* height:%d */"%(height)
count = 0
for h in range(0, height):
for w in range(0, width):
pixel = im.getpixel((w, h))
for i in range(0,3):
count = (count+1)%16
if (count == 0):
print "0x%02x,/n"%(pixel[i]),
else:
print "0x%02x,"%(pixel[i]),

Python獲取屏幕指定坐標(biāo)處像素顏色

import ctypes
from ctypes import wintypes
from typing import Sequence, Generator
 
 
user32 = ctypes.windll.user32
gdi32 = ctypes.windll.gdi32
 
# 定義類型
HWND = wintypes.HWND
HDC = wintypes.HDC
HBITMAP = wintypes.HBITMAP
 
 
class BITMAPINFOHEADER(ctypes.Structure):
    _fields_ = [
        ("biSize", wintypes.DWORD),
        ("biWidth", wintypes.LONG),
        ("biHeight", wintypes.LONG),
        ("biPlanes", wintypes.WORD),
        ("biBitCount", wintypes.WORD),
        ("biCompression", wintypes.DWORD),
        ("biSizeImage", wintypes.DWORD),
        ("biXPelsPerMeter", wintypes.LONG),
        ("biYPelsPerMeter", wintypes.LONG),
        ("biClrUsed", wintypes.DWORD),
        ("biClrImportant", wintypes.DWORD)
    ]
 
class BITMAPINFO(ctypes.Structure):
    _fields_ = [
        ("bmiHeader", BITMAPINFOHEADER),
        ("bmiColors", wintypes.DWORD * 3)
    ]
 
 
def get_pixel_color(coords: Sequence[tuple[int, int]], hwnd: HWND) -> Generator[tuple[int, int, int], None, None]:
    rect = wintypes.RECT()
    user32.GetClientRect(hwnd, ctypes.byref(rect))
    width = rect.right - rect.left
    height = rect.bottom - rect.top
 
    # 創(chuàng)建內(nèi)存設(shè)備上下文
    hdc_src = user32.GetDC(hwnd)
    hdc_dst = gdi32.CreateCompatibleDC(hdc_src)
    bmp = gdi32.CreateCompatibleBitmap(hdc_src, width, height)
    gdi32.SelectObject(hdc_dst, bmp)
 
    # 使用 BitBlt 復(fù)制窗口內(nèi)容到內(nèi)存設(shè)備上下文
    gdi32.BitBlt(hdc_dst, 0, 0, width, height, hdc_src, 0, 0, 0x00CC0020)  # SRCCOPY
 
    # 獲取位圖信息
    bmi = BITMAPINFO()
    bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
    bmi.bmiHeader.biWidth = width
    bmi.bmiHeader.biHeight = -height  # 負(fù)值表示自底向上
    bmi.bmiHeader.biPlanes = 1
    bmi.bmiHeader.biBitCount = 32
    bmi.bmiHeader.biCompression = 0
 
    # 創(chuàng)建緩沖區(qū)并獲取位圖數(shù)據(jù)
    buffer = ctypes.create_string_buffer(width * height * 4)
    gdi32.GetDIBits(hdc_dst, bmp, 0, height, buffer, ctypes.byref(bmi), 0)
 
    # 釋放資源
    gdi32.DeleteObject(bmp)
    gdi32.DeleteDC(hdc_dst)
    user32.ReleaseDC(hwnd, hdc_src)
 
    # 遍歷指定坐標(biāo)并返回像素顏色
    for x, y in coords:
        if 0 <= x < width and 0 <= y < height:
            offset = (y * width + x) * 4
            color = buffer[offset:offset + 4]
            yield color[2], color[1], color[0]  # BGR -> RGB
        else:
            yield (0, 0, 0)

到此這篇關(guān)于使用Python實(shí)現(xiàn)獲取屏幕像素顏色值的文章就介紹到這了,更多相關(guān)Python獲取屏幕像素顏色值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python寫一個(gè)基于MD5的文件監(jiān)聽(tīng)程序

    Python寫一個(gè)基于MD5的文件監(jiān)聽(tīng)程序

    這篇文章主要給大家介紹了關(guān)于利用Python如何寫一個(gè)基于MD5的文件監(jiān)聽(tīng)程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python數(shù)據(jù)類型可變不可變知識(shí)點(diǎn)總結(jié)

    python數(shù)據(jù)類型可變不可變知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給各位整理的是關(guān)于python數(shù)據(jù)類型可變不可變知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • python使用Psutil模塊實(shí)現(xiàn)獲取計(jì)算機(jī)相關(guān)信息

    python使用Psutil模塊實(shí)現(xiàn)獲取計(jì)算機(jī)相關(guān)信息

    psutil 是一個(gè)跨平臺(tái)的庫(kù),用于獲取進(jìn)程和系統(tǒng)運(yùn)行狀態(tài)的信息,這篇文章主要為大家詳細(xì)介紹了python如何調(diào)用psutil模塊實(shí)現(xiàn)獲取計(jì)算機(jī)相關(guān)信息,有需要的小伙伴可以了解下
    2023-11-11
  • python3獲取控制臺(tái)輸入的數(shù)據(jù)的具體實(shí)例

    python3獲取控制臺(tái)輸入的數(shù)據(jù)的具體實(shí)例

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python3獲取控制臺(tái)輸入的數(shù)據(jù)的具體實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-08-08
  • python中and和or邏輯運(yùn)算符的用法示例

    python中and和or邏輯運(yùn)算符的用法示例

    python中的邏輯運(yùn)算符有兩種返回值,python運(yùn)算符除了能操作bool類型表達(dá)式,還能操作其他所有類型的表達(dá)式,這篇文章主要給大家介紹了關(guān)于python中and和or邏輯運(yùn)算符用法的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • pandas.DataFrame.agg()方法的使用

    pandas.DataFrame.agg()方法的使用

    agg函數(shù)是一個(gè)非常強(qiáng)大的工具,用于對(duì)數(shù)據(jù)進(jìn)行分組聚合操作,它可以沿指定軸(行或列)應(yīng)用一個(gè)或多個(gè)聚合函數(shù),常用于統(tǒng)計(jì)匯總分析,感興趣的可以了解一下
    2025-05-05
  • python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說(shuō)明

    python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說(shuō)明

    這篇文章主要介紹了python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 實(shí)例講解Python的函數(shù)閉包使用中應(yīng)注意的問(wèn)題

    實(shí)例講解Python的函數(shù)閉包使用中應(yīng)注意的問(wèn)題

    這里我們來(lái)以實(shí)例講解Python的函數(shù)閉包使用中應(yīng)注意的問(wèn)題,主要針對(duì)閉包后新生成的變量來(lái)不及初始化而導(dǎo)致找不到變量的錯(cuò)誤出現(xiàn),需要的朋友可以參考下
    2016-06-06
  • 詳解Python requests 超時(shí)和重試的方法

    詳解Python requests 超時(shí)和重試的方法

    這篇文章主要介紹了詳解Python requests 超時(shí)和重試的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • python pyinstaller打包exe報(bào)錯(cuò)的解決方法

    python pyinstaller打包exe報(bào)錯(cuò)的解決方法

    這篇文章主要給大家介紹了關(guān)于python pyinstaller打包exe報(bào)錯(cuò)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論

新乡市| 宜兰市| 平乐县| 班戈县| 仁怀市| 松江区| 随州市| 柳江县| 甘洛县| 句容市| 阿拉善右旗| 广昌县| 扎兰屯市| 称多县| 宾川县| 辰溪县| 崇礼县| 玉田县| 富宁县| 仁化县| 榆社县| 图木舒克市| 剑阁县| 芦溪县| 永川市| 鸡西市| 双牌县| 屏边| 海宁市| 方正县| 长顺县| 长汀县| 义乌市| 通州区| 克什克腾旗| 柞水县| 类乌齐县| 安平县| 商城县| 新邵县| 中方县|