'How do I get rid of black bar in python window?

I'm working on a Object Detection project for the game Cuphead using OpenCV and Python. Now I'm trying to capture objects in real time but when the detection window displays I get this rare black bar on the top and I don't know how to get rid of it, here's what I see, on the left my object detection window and in the right the Cuphead game window.

Here's the code for the class used for this:

import numpy as np
import win32gui, win32ui, win32con

class WindowCapture:

    # define monitor's width and height
    w = 0
    h = 0
    hwnd = None

    # constructor
    def __init__(self, window_name):
        
        if window_name is None: # if we don't pass any window names capture desktop
            self.hwnd = win32gui.GetDesktopWindow()
        else:
            # Find the game window
            self.hwnd = win32gui.FindWindow(None, window_name)
            if not self.hwnd:
                raise Exception("Window not founnd: {}".format(window_name))

            # define window's widht and height. the resolution we'll work with
            window_rect = win32gui.GetWindowRect(self.hwnd)
            self.w = window_rect[2] - window_rect[0]
            self.h = window_rect[3] - window_rect[1]

    def get_screenshot(self):
        

        # get the window image data
        wDC = win32gui.GetWindowDC(self.hwnd)
        dcObj = win32ui.CreateDCFromHandle(wDC)
        cDC = dcObj.CreateCompatibleDC()
        dataBitMap = win32ui.CreateBitmap()
        dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
        cDC.SelectObject(dataBitMap)
        cDC.BitBlt((0,0), (self.w, self.h), dcObj, (0,0), win32con.SRCCOPY)

        # create the screenshot image that we want to return to be processed
        signedIntsArray = dataBitMap.GetBitmapBits(True)
        img = np.fromstring(signedIntsArray, dtype='uint8')
        img.shape = (self.h, self.w, 4)

        # Free Resources
        dcObj.DeleteDC()
        cDC.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, wDC)
        win32gui.DeleteObject(dataBitMap.GetHandle())

        # get rid of the alpha channel in the img
        img = img[..., :3]
        img = np.ascontiguousarray(img)

        return img


Solution 1:[1]

It seems img.shape = (self.h, self.w, 4) causes the problem. As GetWindowRect and @IInspectable said,

In Windows Vista and later, the Window Rect now includes the area occupied by the drop shadow.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YangXiaoPo - MSFT