Package: bettercam
📸BetterCam🚀🌟 World's Fastest Python Screenshot Library for Windows 🐍importbettercamcamera=bettercam.create()camera.grab()🌈 IntroductionBetterCam is the World's 🌏 Fastest Publicly available Python screenshot library for Windows, boasting 240Hz+ capturing using the Desktop Duplication API 🖥️💨. Born fromDXCam, it shines in deep learning pipelines for FPS games, outpacing other Python solutions likepython-mssandD3DShot.BetterCam's superpowers include:🚅 Insanely fast screen capturing (> 240Hz)🎮 Capture from Direct3D exclusive full-screen apps without interruption, even during alt+tab.🔧 Auto-adjusts to scaled / stretched resolutions.🎯 Precise FPS targeting for Video output.👌 Smooth NumPy, OpenCV, PyTorch integration, etc.💞 Community contributions warmly invited!🛠️ InstallationFrom PyPI:pipinstallgit+https://github.com/RootKit-Org/BetterCamNote:🧩 OpenCV is needed by BetterCam for color space conversion. Install it withpip install opencv-pythonif not yet available.📚 UsageEach monitor is paired with aBetterCaminstance.
To get started:importbettercamcamera=bettercam.create()# Primary monitor's BetterCam instance📷 ScreenshotFor a quick snap, call.grab:frame=camera.grab()frameis anumpy.ndarrayin the(Height, Width, 3[RGB])format by default. Note:.grabmay returnNoneif there's no update since the last.grab.To display your screenshot:fromPILimportImageImage.fromarray(frame).show()For a specific region, provide theregionparameter with a tuple for the bounding box coordinates:left,top=(1920-640)//2,(1080-640)//2right,bottom=left+640,top+640region=(left,top,right,bottom)frame=camera.grab(region=region)# A 640x640x3 numpy ndarray snapshot📹 Screen CaptureStart and stop screen capture with.startand.stop:camera.start(region=(left,top,right,bottom))# Capture a region (optional)camera.is_capturing# True# ... Your Codecamera.stop()camera.is_capturing# False🔄 Retrieving Captured DataWhen capturing, grab the latest frame with.get_latest_frame:camera.start()foriinrange(1000):image=camera.get_latest_frame()# Waits for a new framecamera.stop()⚙️ Advanced Usage & Notes🖥️ Multiple Monitors / GPUscam1,cam2,cam3=[bettercam.create(device_idx=d,output_idx=o)ford,oin[(0,0),(0,1),(1,1)]]img1,img2,img3=[cam.grab()forcamin(cam1,cam2,cam3)]To list devices and outputs:>>>importbettercam>>>bettercam.device_info()>>>bettercam.output_info()🎨 Output FormatSelect your color mode when creating a BetterCam instance:bettercam.create(output_idx=0,output_color="BGRA")We support "RGB", "RGBA", "BGR", "BGRA", "GRAY" (for grayscale). Right now onlynumpy.ndarrayshapes are supported:(Height, Width, Channels).🔄 Video BufferFrames go into a fixed-size ring buffer. Customize its max length withmax_buffer_lenon creation:camera=bettercam.create(max_buffer_len=512)🎥 Target FPSFor precise FPS targeting, we use the high-resolutionCREATE_WAITABLE_TIMER_HIGH_RESOLUTION:camera.start(target_fps=120)# Ideally, not beyond 240Hz.🔄 Video ModeFor constant framerate video recording, usevideo_mode=Trueduring.start:# Example: Record a 5-second, 120Hz videocamera.start(target_fps=target_fps,video_mode=True)# ... Video writing code goes here🛠️ Resource ManagementCall.releaseto stop captures and free resources. Manual deletion also possible:delcamera📊 BenchmarksMax FPS Achievement:cam=bettercam.create()# ... Benchmarking code...BetterCam Nvidia GPU :checkered_flag:BetterCam :checkered_flag:DXCampython-mssD3DShotAvg FPS111.667123.6673934.667N/AStd Dev0.8891.7781.3332.222N/AFPS Targeting:# ... Sample code to test target FPS ...Target/ResultBetterCam Nvidia GPU :checkered_flag:BetterCam :checkered_flag:DXCampython-mssD3DShot120fps111.667, 0.88988.333, 2.44436.667, 0.889N/AN/A60fps60, 060, 035, 5.3N/AN/A📝 Referenced WorkDXCam: Our origin story.D3DShot: Provided foundational ctypes.OBS Studio: A treasure trove of knowledge.[^1]:Preemption (computing)[^2]:Time.sleep precision improvement