Excel為數據繪制拆線圖,并將均值線疊加在圖上,以及整個過程的區域錄屏python腳本
- 1.演示動畫
- A.視頻
- B.gif動畫
- 2.跟蹤鼠標區域的錄屏腳本
Excel中有一組數據,希望畫出曲線,并且能把均值線也繪制在圖上,以下動畫演示了整個過程,并且提供了區域錄屏腳本,原理如下:
為節約空間,避免剪輯,只記錄有效區域【僅記錄鼠標區域且圖像變化的圖片】
1.演示動畫
A.視頻
Excel為數據繪制拆線圖,并將均值線疊加在圖上
B.gif動畫
2.跟蹤鼠標區域的錄屏腳本
import cv2
import numpy as np
import mss
import time
import threading
import pyautogui
import datetime
from skimage.metrics import structural_similarity as ssim
from pynput import mousedef compute_ssim(imageA, imageB):"""計算兩幅圖像的結構相似性指數(SSIM)"""grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)(score, diff) = ssim(grayA, grayB, full=True)return scoredef resize_and_pad(image, size=(640, 640)):"""等比縮放并填充圖像"""h, w = image.shape[:2]scale = min(size[0] / w, size[1] / h)new_w, new_h = int(w * scale), int(h * scale)resized_image = cv2.resize(image, (new_w, new_h))# 創建黑色背景top = (size[1] - new_h) // 2bottom = size[1] - new_h - topleft = (size[0] - new_w) // 2right = size[0] - new_w - leftpadded_image = cv2.copyMakeBorder(resized_image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0, 0, 0])return padded_imageis_mouse_pressed=False
def on_click(x, y, button, pressed):global is_mouse_pressedis_mouse_pressed = presseddef capture_screen(stop_event):layout_w=720layout_h=1280mouse_listener = mouse.Listener(on_click=on_click)mouse_listener.start()with mss.mss() as sct:# 初始化第一幀monitor = sct.monitors[1]print(monitor)frame1 = Nonescreen_width = monitor["width"]screen_height = monitor["height"]fourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter('output.avi', fourcc, 3.0, (layout_w, layout_h))area=Nonewhile not stop_event.is_set():mouse_x, mouse_y = pyautogui.position() if area:if mouse_x<area['left'] or mouse_x>area['left']+layout_w or mouse_y<area['top'] or mouse_y>area['top']+layout_h:area=Noneif area is None and is_mouse_pressed: # 計算截取區域,以鼠標為中心640x640,同時進行邊界檢查left = max(0, min(screen_width - layout_w, mouse_x - layout_w // 2))top = max(0, min(screen_height - layout_h, mouse_y - layout_h // 2))area = {'top': top, 'left': left, 'width': layout_w, 'height': layout_h}if area:frame2 = np.array(sct.grab(area))frame2 = cv2.cvtColor(frame2, cv2.COLOR_BGRA2BGR)# 在 frame2 上繪制一個小圓點標記鼠標位置relative_mouse_x = mouse_x - area['left']relative_mouse_y = mouse_y - area['top']cv2.circle(frame2, (relative_mouse_x, relative_mouse_y), 5, (0, 0, 255), -1) # 紅色小圓點frame2=resize_and_pad(frame2,(layout_w,layout_h))if frame1 is None:frame1 = frame2.copy()continuescore = compute_ssim(frame1, frame2)if score<1.0:out.write(frame2)frame1 = frame2.copy()# 適量的延時,防止過高的CPU使用率time.sleep(0.1)out.release()if __name__ == '__main__':stop_event = threading.Event()# 啟動屏幕捕捉的線程capture_thread = threading.Thread(target=capture_screen, args=(stop_event,))capture_thread.start()# 等待用戶輸入'q'以停止捕捉while True:if input().strip().lower() == 'q':stop_event.set()break# 等待屏幕捕捉線程結束capture_thread.join()print("捕獲已結束并退出。")from moviepy.editor import VideoFileClip# 定義視頻文件路徑和輸出GIF文件路徑input_video_path = 'output.avi'output_gif_path = 'output.gif'# 加載視頻文件clip = VideoFileClip(input_video_path)clip = clip.subclip(3, -2)clip = clip.resize(0.4)# 將視頻剪輯轉換為GIFclip.write_gif(output_gif_path, fps=2)print(f"GIF文件保存到 {output_gif_path}")