I have downloaded Python 3.7 and 3.9 (both 64 bit) as well as VS Code to replicate the following code: https://www.youtube.com/watch?v=WymCpVUPWQ4&t=1291s
However, when installing "pywin32" and then importing the packages to the code, I get "unresolved-import" on the three imports: win32gui, win32ui and win32con.
I have tried multiple installation methods, what is strange is that the code replicated from the link above works.
Tried installing via "pip" https://pypi.org/project/pywin32/
As well as downloading the executable program for installation from: https://github.com/mhammond/pywin32/releases
Including installing using the .whl files from: https://www.lfd.uci.edu/~gohlke/pythonlibs/ (with their respective Python version and bits) and then proceeding to the "post-installation" which was explained here: Not able to use the win32gui module from pywin32 in Pycharm
From source Forge https://sourceforge.net/projects/pywin32/
And still, the error persuades. I'm not quite sure what I'm doing wrong or if the versions used are currently outdated/not compatible.
I tried the 4 installations both in Python 3.7 and 3.9 with 64 bit executables/.whl files.
This is the code that I'm using:
As a side note, the code works just fine if the "hwnd" is set to "NONE" but if I comment this and use the line "hwnd = win32gui.FindWindow(None, 'Calculator')" it just shows a black screen even though the Calculator app is open.
I suppose it's because of the imports.
import win32gui
import win32ui
import win32con
import numpy as np
import PIL
import cv2 as cv
def screen():
w = 1920
h = 1080
#hwnd = win32gui.FindWindow(None, 'Calculator')
hwnd = None
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)
signedIntsArray = dataBitMap.GetBitmapBits(True)
#img = np.fromstring(signedIntsArray, dtype='uint8')
img = np.frombuffer(signedIntsArray, dtype='uint8')
img.shape = (h, w, 4)
# free resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
#img = img[...,:3]
#img = np.ascontiguousarray(img)
return img
while(True):
sc = screen()
resize = cv.resize(sc,(800,500))
cv.imshow('idk',resize)
if cv.waitKey(1) == ord('q'):
cv.destroyAllWindows()
break