图片添加动画后转成视频的操作时,可以实现,但是到处的时候图片颜色发生了改变,好像转成了对比色,应该时进行色彩空间BGR和RGB部分转化时出现了问题。,请大佬帮我看一看,谢谢!!
import cv2
import numpy as np
from moviepy.editor import ImageSequenceClip
from PIL import Image, ImageDraw, ImageFont
def create_animation(image, frames, duration, fps):
"""创建旋转动画效果."""
for t in np.linspace(0, 1, int(fps * duration)):
angle = t * 360 # 从0到360度旋转
M = cv2.getRotationMatrix2D(
(image.shape[1] // 2, image.shape[0] // 2), angle, 1)
rotated = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
frames.append(rotated)
def load_image(image_path):
"""加载图片并返回其路径和调整后的图片."""
image = cv2.imread(image_path)
return image_path.split("/")[-1].split(".")[0], image
def resize_image(image, target_width, target_height):
"""根据目标宽高比调整图片大小,并保持原始比例."""
height, width, _ = image.shape
aspect_ratio = width / height
if aspect_ratio > (target_width / target_height):
new_width = target_width
new_height = int(target_width / aspect_ratio)
else:
new_height = target_height
new_width = int(target_height * aspect_ratio)
resized_image = cv2.resize(image, (new_width, new_height))
# 创建背景并将图片居中放置
background = np.zeros((target_height, target_width, 3), dtype=np.uint8)
y_offset = (target_height - new_height) // 2
x_offset = (target_width - new_width) // 2
background[y_offset:y_offset + new_height,
x_offset:x_offset + new_width] = resized_image
return background, new_width, new_height
def generate_video(frames, output_path, target_width, target_height, fps):
"""生成视频并输出."""
clip = ImageSequenceClip(frames, fps=fps)
clip = clip.resize(newsize=(target_width, target_height))
clip.write_videofile(output_path, codec="libx264", fps=fps)
def add_text_with_pil(frame, text, position, font_path, font_size, color):
"""使用 PIL 在帧上添加中文文本."""
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(pil_image)
font = ImageFont.truetype(font_path, font_size)
draw.text(position, text, font=font, fill=color)
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
def main(image_path):
# 加载原始图片
file_name, image = load_image(image_path)
# 设定目标视频的分辨率
target_width = 1080
target_height = 2060
# 调整图片大小并居中
image_resized, new_width, new_height = resize_image(
image, target_width, target_height)
# 创建视频帧列表
frames = []
# 设置文本参数
font_path = "SourceHanSerifCN-Heavy.otf" # 确保这个路径是正确的
font_size_file = 50 # 文件名字体大小
font_size_text = 50 # 测试test字体大小
color_file = (45, 204, 159) # 文件名颜色
color_text = (45, 204, 159) # 测试test颜色
# 计算文件名文本位置
text_size_file = cv2.getTextSize(
file_name, cv2.FONT_HERSHEY_SIMPLEX, 3, 5)[0]
text_x_file = (target_width - text_size_file[0]) // 2
text_y_file = target_height // 3 # 上方1/3位置
# 计算“测试test”文本位置(左下角)
text = "测试test"
text_x_text = 10 # 左边距
text_y_text = target_height - 60 # 底边距,留出一些空间
# 添加原图的前半秒并写上文件名
for _ in range(int(0.5 * 30)): # 30 FPS
frame = image_resized.copy()
frame = add_text_with_pil(
frame, file_name, (text_x_file, text_y_file), font_path, font_size_file, color_file)
frames.append(frame)
# 添加动画效果
create_animation(image_resized, frames, duration=2, fps=30) # 2秒的旋转动画
# 添加原图的最后两秒
for _ in range(int(2 * 30)):
frames.append(image_resized)
# 在每一帧中添加“测试test”文本
for frame in frames:
frame = add_text_with_pil(
frame, text, (text_x_text, text_y_text), font_path, font_size_text, color_text)
# 构建输出视频路径
output_path = f"{file_name}.mp4"
# 输出视频
generate_video(frames, output_path, target_width, target_height, fps=30)
print("视频已生成:", output_path)
if __name__ == "__main__":
image_path = "test001.jpg" # 替换为你的图片路径
main(image_path)