ra1nty / DXcam

A Python high-performance screen capture library for Windows using Desktop Duplication API
MIT License
457 stars 67 forks source link

numpy processor #94

Open ZCban opened 2 months ago

ZCban commented 2 months ago

def process(self, rect, width, height, region, rotation_angle):

pitch = int(rect.Pitch)
if rotation_angle in (0, 180):
    offset = (region[1] if rotation_angle == 0 else height - region[3]) * pitch
    height = region[3] - region[1]
else:
    offset = (region[0] if rotation_angle == 270 else width - region[2]) * pitch
    width = region[2] - region[0]

buffer = (ctypes.c_char * (pitch * height)).from_address(ctypes.addressof(rect.pBits.contents) + offset)
pitch = pitch // 4
if rotation_angle in (0, 180):
    image = np.ndarray((height, pitch, 4), dtype=np.uint8, buffer=buffer)
elif rotation_angle in (90, 270):
    image = np.ndarray((width, pitch, 4), dtype=np.uint8, buffer=buffer)

if rotation_angle == 90:
    image = np.rot90(image, axes=(1, 0))
elif rotation_angle == 180:
    image = np.rot90(image, k=2, axes=(0, 1))
elif rotation_angle == 270:
    image = np.rot90(image, axes=(0, 1))

if rotation_angle in (0, 180):
    image = image[region[1]:region[3], :width, :]
else:
    image = image[:height, region[0]:region[2], :]

# Convert the color of the cropped image only # reduce unnecessary CPU usage
if self.color_mode is not None:
    image = self.process_cvtcolor(image)

return image