最近少帅下飞机的视频也是火遍全网啦,有的大神用CAD画
有的用Solidworks画
那怎么用python画呢?
其实就是一个二值化加上像素化的过程,直接上源码
(喜欢的关注公众号Pythonicave,各种各样有趣python源码分享)
import cv2
import numpy as np
def process_video(input_path, output_path, pixel_size=10):
# 打开视频文件
cap = cv2.VideoCapture(input_path)
# 获取视频的帧率、宽度和高度
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 原始帧宽度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 原始帧高度
# 定义视频编解码器并创建 VideoWriter 对象
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 使用 mp4 编码
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height), isColor=False)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 转换为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 二值化处理(阈值可根据需要调整)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 像素化处理
if pixel_size > 1:
temp_height = height // pixel_size
temp_width = width // pixel_size
# 缩小图像
small = cv2.resize(binary, (temp_width, temp_height), interpolation=cv2.INTER_LINEAR)
# 放大图像回原始尺寸
pixelated = cv2.resize(small, (width, height), interpolation=cv2.INTER_NEAREST)
else:
pixelated = binary
# 将处理后的帧写入输出视频
out.write(pixelated)
# (可选)显示处理进度
# cv2.imshow('frame', pixelated)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
# 使用函数处理视频
input_video = "D:\迅雷下载\下载.mp4" # 输入视频路径
output_video = "D:\迅雷下载\下载71.mp4" # 输出视频路径
process_video(input_video, output_video, pixel_size=10)