import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont # 视频分辨率 VIDEO_WIDTH = 1920 VIDEO_HEIGHT = 1080 # 文本属性 FONT_SIZE = int(VIDEO_HEIGHT * 0.75) FONT_COLOR = (255, 255, 255) # 背景属性 BACKGROUND_COLOR = (255, 0, 255) # 文本滚动速度 SCROLL_SPEED = int(VIDEO_WIDTH / 100) # 跑马灯文本 MARQUEE_TEXT = "Hello World! This is a marquee text." # 创建视频输出对象 fourcc = cv2.VideoWriter_fourcc(*'mp4v') video_writer = cv2.VideoWriter("E:/Template/word/marquee.mp4", fourcc, 25, (VIDEO_WIDTH, VIDEO_HEIGHT)) # 创建画布 canvas = Image.new('RGB', (VIDEO_WIDTH, VIDEO_HEIGHT), BACKGROUND_COLOR) draw = ImageDraw.Draw(canvas) # 加载字体 font = ImageFont.truetype("arial.ttf", FONT_SIZE) # 计算文本宽度 text_width, text_height = draw.textsize(MARQUEE_TEXT, font=font) # 文本起始位置 x = VIDEO_WIDTH # 循环生成帧并写入视频文件 while x > -text_width: # 添加文本 draw.text((x, (VIDEO_HEIGHT - text_height) / 2), MARQUEE_TEXT, font=font, fill=FONT_COLOR) # 转换图像格式 frame = np.array(canvas) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 写入视频文件 video_writer.write(frame) # 清空画布 draw.rectangle((0, 0, VIDEO_WIDTH, VIDEO_HEIGHT), fill=BACKGROUND_COLOR) # 更新文本位置 x -= SCROLL_SPEED # 释放资源 video_writer.release() 实现进度条加载。
时间: 2024-01-14 21:04:55 浏览: 137
好的,以下是Python代码实现进度条加载的功能:
```python
import cv2
import numpy as np
# 视频分辨率
VIDEO_WIDTH = 1920
VIDEO_HEIGHT = 1080
# 进度条属性
BAR_WIDTH = int(VIDEO_WIDTH * 0.8)
BAR_HEIGHT = int(VIDEO_HEIGHT * 0.1)
BAR_COLOR = (255, 255, 255)
BACKGROUND_COLOR = (0, 0, 0)
# 加载字体
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 2
font_thickness = 5
# 创建视频输出对象
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter("E:/Template/word/progress_bar.mp4", fourcc, 25, (VIDEO_WIDTH, VIDEO_HEIGHT))
# 循环生成帧并写入视频文件
for i in range(101):
# 创建画布
canvas = np.zeros((VIDEO_HEIGHT, VIDEO_WIDTH, 3), dtype=np.uint8)
canvas[:] = BACKGROUND_COLOR
# 添加进度条
x = int((VIDEO_WIDTH - BAR_WIDTH) / 2)
y = int((VIDEO_HEIGHT - BAR_HEIGHT) / 2)
progress = int(i * BAR_WIDTH / 100)
cv2.rectangle(canvas, (x, y), (x + progress, y + BAR_HEIGHT), BAR_COLOR, -1)
# 添加进度文本
text = "Progress: {}%".format(i)
text_size = cv2.getTextSize(text, font, font_scale, font_thickness)[0]
text_x = int((VIDEO_WIDTH - text_size[0]) / 2)
text_y = int(y - text_size[1])
cv2.putText(canvas, text, (text_x, text_y), font, font_scale, BAR_COLOR, font_thickness, cv2.LINE_AA)
# 写入视频文件
video_writer.write(canvas)
# 释放资源
video_writer.release()
```
该代码使用Python的OpenCV库,创建了一个黑色背景的画布,并在画布上添加了一个进度条和进度文本。然后它循环生成帧,并将每一帧写入视频文件中。在每一帧中,它会更新进度条的长度和进度文本的内容。最后,视频将保存在指定目录下。
阅读全文