Home > Blockchain >  Convert win32ui DataBitmap to array in python
Convert win32ui DataBitmap to array in python

Time:02-16

i want to take a screen shoot than transfroming it to an array without saving the file as image in a path and loading it again from the path to convert it :

what i want is directly convert the data to an array :

        w = 1920
        h = 1080
        bmpfilenamename = r"path"
        hwnd = win32gui.FindWindow(None, "my_window")
        wDC = win32gui.GetWindowDC(hwnd)
        dcObj=win32ui.CreateDCFromHandle(wDC)
        cDC=dcObj.CreateCompatibleDC()
        dataBitMap = win32ui.CreateBitmap()
        dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
        cDC.SelectObject(dataBitMap)
        cDC.BitBlt((0, 0),(w, h) , dcObj, (0, 0), win32con.SRCCOPY)
        dataBitMap.SaveBitmapFile(cDC, bmpfilenamename) #i want to ignor this phase and directly convert the data to array
        My_array = np.array(#myimg , dtype='float')
        print(array)
        print(array.shape)

My final goal is to feed a neural network predection model with fast stream of screenshots

CodePudding user response:

You may use PIL for converting dataBitMap to PIL Image object as demonstrated here.
Use array = np.asarray(im) for converting im to NumPy array.

The pixel format is supposed to be BGRA.

Here is a code sample:

import win32gui
import win32ui
import win32con
import numpy as np
from PIL import Image
import cv2  # Used for showing the NumPy array as image

w = 1920
h = 1080
hwnd = win32gui.FindWindow(None, "my_window")
wDC = win32gui.GetWindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (w, h), dcObj, (0, 0), win32con.SRCCOPY)

# https://stackoverflow.com/questions/6951557/pil-and-bitmap-from-winapi
bmpinfo = dataBitMap.GetInfo()
bmpstr = dataBitMap.GetBitmapBits(True)
im = Image.frombuffer('RGBA', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'RGBA', 0, 1)

array = np.asarray(im) # Convet to NumPy array

# Show image for testing
cv2.imshow('array', array)
cv2.waitKey()
cv2.destroyAllWindows()
  • Related