2

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

Akali DZ
  • 47
  • 6

1 Answers1

2

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()
Rotem
  • 30,366
  • 4
  • 32
  • 65
  • than i can convert it to 3d shape like this: img = cv2.cvtColor(array, cv2.COLOR_RGBA2RGB) and resized to the size my neural is traind with : im = cv2.resize(img, (300, 300), interpolation = cv2.INTER_AREA) – Akali DZ Feb 15 '22 at 11:27
  • Yes, I think it should be `cv2.COLOR_BGRA2RGB` (unless the input to the neural is BGR as OpenCV convention). – Rotem Feb 15 '22 at 11:35