Python桌面录屏工具高清录制及光标透明化问题求助
Hey there! Let's fix up your screen recorder to get that crisp HD quality and the semi-transparent cursor circle you want. Here's how to tackle both issues:
1. Achieving HD Screen Recording
The main reason your recordings aren't high-def right now is because you're intentionally scaling down the screen size and using a low frame rate. Let's adjust that:
- Use your actual screen resolution: Instead of hardcoding a smaller size like
(960,540), grab your full display dimensions withpyautogui.size(). This ensures you capture every pixel of your screen. - Boost the frame rate: 12 FPS is too low for smooth video—bump it up to 25 or 30 (standard for most screen recordings).
- Remove the resize step: You're shrinking the captured frame before saving it, which directly reduces quality. Ditch that
cv2.resize()line entirely. - Optional: Use a better codec: While XVID works, if your OpenCV setup supports it, using
*'X264'will give you better compression with higher-quality output.
2. Making the Cursor Circle Transparent
The issue with cv2.circle() here is that your frame is a 3-channel RGB/BGR image—there's no alpha channel to handle transparency. To fix this, we'll draw the circle on a separate transparent overlay and blend it with the original frame:
- Create a 4-channel (RGBA) overlay that matches your screen size.
- Draw the circle on this overlay with an alpha value (0-255, where lower = more transparent).
- Convert the original frame to RGBA, then use
cv2.addWeighted()to merge the overlay and frame together.
Modified Full Code
import cv2 import numpy as np import pyautogui import datetime import win32api # Get current timestamp for filename date = datetime.datetime.now() # Use full screen resolution instead of scaling down SCREEN_SIZE = pyautogui.size() # Increase frame rate for smoother video framerate = 25 # Define codec - X264 offers better quality/compression if available; fallback to XVID try: fourcc = cv2.VideoWriter_fourcc(*'X264') except: fourcc = cv2.VideoWriter_fourcc(*'XVID') filename = f'E:/project/videos/rec_{date.year}{date.month}{date.day}{date.hour}{date.minute}{date.second}.avi' # Initialize VideoWriter with full screen size out = cv2.VideoWriter(filename, fourcc, framerate, SCREEN_SIZE) while True: # Capture full screen screenshot img = pyautogui.screenshot() frame = np.array(img) # Convert from BGR (pyautogui default) to RGB for OpenCV frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Get cursor position _xs, _ys = win32api.GetCursorPos() # Create a transparent overlay for the cursor circle overlay = np.zeros((SCREEN_SIZE[1], SCREEN_SIZE[0], 4), dtype=np.uint8) # Draw circle on overlay: (0,255,255) is yellow, alpha=80 (adjust for transparency) cv2.circle(overlay, (_xs, _ys), 20, (0, 255, 255, 80), -1) # Convert original frame to RGBA to match overlay frame_rgba = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA) # Blend overlay with frame: adjust alpha weights if needed frame_with_cursor = cv2.addWeighted(frame_rgba, 1.0, overlay, 0.7, 0) # Convert back to RGB for VideoWriter (since most codecs don't support RGBA) frame_final = cv2.cvtColor(frame_with_cursor, cv2.COLOR_RGBA2RGB) # Write the frame to video out.write(frame_final) # Show the preview window cv2.imshow('screenshot', frame_final) # Exit on pressing 'q' if cv2.waitKey(1) == ord("q"): break # Cleanup cv2.destroyAllWindows() out.release()
Quick Notes:
- For cursor transparency, tweak the alpha value in
(0,255,255, 80)—lower numbers make the circle more see-through. - If the
X264codec isn't available, the code falls back toXVIDautomatically, so no compatibility issues. - The preview window will show your full screen (you can resize it manually if needed, but the recorded video will stay full resolution).
内容的提问来源于stack exchange,提问作者arvind8




