韦奇_图片叠加图片,图片添加文字,图片增加音频变为视频

from PIL import Image, ImageDraw, ImageFont
import moviepy.editor as mpy
def overlay_images( overlay_image_path,  overlay_position=(0, 0)):
    """
    将一张图片叠加到另一张图片上。

    参数:
    base_image_path (str):底图的路径。
    overlay_image_path (str):要叠加的图片路径。
    save_path (str):保存结果图片的路径。
    overlay_position (tuple):叠加图片在底图上的位置,默认为(0, 0)。

    返回:
    None
    """

    base_image_path = 'D:\\rj\\python_code\\seer_utils\\Datas\\template\\black.jpg'
    save_path = 'D:\\rj\\python_code\\seer_utils\\Tmp\\images\\result.jpg'
    # 打开底图
    base_image = Image.open(base_image_path)
    base_width, base_height = base_image.size

    # 打开要叠加的图片
    overlay_image = Image.open(overlay_image_path)

    # 计算适应宽度后的高度,保持宽高比
    overlay_ratio = overlay_image.width / overlay_image.height
    new_height = int(base_width / overlay_ratio)
    resized_overlay = overlay_image.resize((base_width, new_height))

    # 将调整后的叠加图片放置到底图上指定位置
    base_image.paste(resized_overlay, overlay_position)

    # 保存结果
    base_image.save(save_path)


def draw_text_on_image_word(text,int_num,font_txt=100,first_line_size = -80):
    """
    在指定图片上添加文本,若文本过长则自动折行显示。
    参数:
    target_image(str):目标图片的路径。
    """

    # font_txt = 100
    # first_line_size = -80  # 字体大小
    first_line_color = (247, 143, 63)  #
    target_image = 'D:\\rj\\python_code\\seer_utils\\Tmp\\images\\result.jpg'

    try:
        # 打开图片并转换为 RGB 模式
        img = Image.open(target_image).convert("RGB")
        draw = ImageDraw.Draw(img)
        try:
            # 尝试加载特定字体,如果加载失败则使用默认字体
            big_font = ImageFont.truetype("simsun.ttc", font_txt)
        except OSError as e:
            print(f"字体加载失败:{e}。使用默认字体。")
            big_font = ImageFont.load_default()

        first_line_y_pos = img.height * int_num
        first_line_start_x = img.width // 30

        current_line_text = ""
        char_width = 0
        for char in text:
            try:
                temp_img = Image.new('RGB', (100, 100))
                temp_draw = ImageDraw.Draw(temp_img)
                single_char_font = ImageFont.truetype("simsun.ttc", font_txt)
                temp_draw.text((0, 0), char, fill=(255, 255, 255), font=single_char_font)
                temp_char_width = temp_img.width

            except OSError as e:
                print(f"字体加载失败:{e}。使用默认字体。")
                single_char_font = ImageFont.load_default()
                temp_img = Image.new('RGB', (100, 100))
                temp_draw = ImageDraw.Draw(temp_img)
                temp_draw.text((0, 0), char, fill=(255, 255, 255), font=single_char_font)
                temp_char_width = temp_img.width

            if char_width + temp_char_width > img.width - first_line_start_x:
                draw.text((first_line_start_x, first_line_y_pos), current_line_text, fill=first_line_color, font=big_font)
                first_line_y_pos += first_line_size + 200  # 增加一些行间距,避免重叠
                current_line_text = char
                char_width = temp_char_width
            else:
                current_line_text += char
                char_width += temp_char_width

        draw.text((first_line_start_x, first_line_y_pos), current_line_text, fill=first_line_color, font=big_font)

        img.save('D:\\rj\python_code\seer_utils\Tmp\\images\\result.jpg', format='JPEG')
    except Exception as e:
        print(f"处理图片时出现错误:{e}")



def create_video_from_audio_and_image(  output_path):
    image_path = 'D:\\rj\python_code\seer_utils\Tmp\\images\\result.jpg'
    audio_path = 'D:\\rj\python_code\seer_utils\Datas\\template\\fly_cloud.MP3'
    """
    根据音频和图片创建视频。

    参数:
    audio_path(str):音频文件路径。
    image_path(str):图片文件路径。
    output_path(str):输出视频文件路径。
    """
    audio = mpy.AudioFileClip(audio_path)
    image = mpy.ImageClip(image_path)
    image = image.set_duration(audio.duration)
    video = image.set_audio(audio)
    video.write_videofile(output_path, fps=1)

if __name__ == '__main__':
    parameter_image = 'tmp.jpg'
    # result_image = 'result.jpg'
    # overlay_images( parameter_image, result_image, (0, 750))
    # text_1 = '朝鲜突然宣布:“永久切断并封锁”与韩国交界的南部国境'
    # draw_text_on_image_word(text_1,result_image,0.2)
    # text_2 = '关注我,持续为您服务'
    # draw_text_on_image_word(text_2,result_image,0.8)
    #
    # audio_path = 'D:\\rj\python_code\seer_utils\Datas\\template\\fly_cloud.MP3'
    # output_path = 'output_video.mp4'
    # create_video_from_audio_and_image(audio_path, result_image, output_path)
1 个赞