目次背景一个安静的下午,看着电脑里七零八落的照片,有大有小,宽高不一,突然想找个方式把他们统一起来,然后做成视频更好(实在我在上高中的时间就喜好把照片做成视频,觉自得义很大)。要满足批量、自动化,肯定得动用代码了。于是首先我枚举了下我希望的功能:
实现依托强大的python库,这里重要用到的工具库有:
先容完重要的库之后,就是代码环节了,代码里解释较多,轻松入手,不做过多的解释: [code] # create viode from a dictionary which contains image or video files import os from PIL import Image from moviepy.editor import ImageSequenceClip def resize_and_crop(image, target_size): """将图片根据给定的巨细进行缩放和裁剪 1. 图片的宽度 < 视频宽度 * 50% or 图片的高度 < 视频高度 * 50%:舍弃掉 2. 图片宽度 < 视频宽度 or 图片的高度 < 视频高度:居中等比放大,直到高度 = 视频高度 or 宽度 = 视频宽度 3. 图片居中等比缩小,直到高度 = 视频高度 or 宽度 = 视频宽度 Args: image (str): 原图片文件路径 target_size (tuple): 视频巨细(宽度, 高度) Returns: Image: 调解后的图片,可能为空 """ img = Image.open(image) video_width, video_height = target_size # 检查条件1:假如原图宽度或高度小于视频尺寸的50%,则返回None if img.width < video_width * 0.5 or img.height < video_height * 0.5: return None # 盘算目的缩放比例 scale_x = video_width / img.width scale_y = video_height / img.height # 根据必要的宽度和高度选择缩放比例 if img.width < video_width or img.height < video_height: # 居中等比放大 scale = max(scale_x, scale_y) else: # 居中等比缩小 scale = min(scale_x, scale_y) # 放大或缩小图片 new_size = (int(img.width * scale), int(img.height * scale)) img = img.resize(new_size, Image.ANTIALIAS) # 盘算裁剪框的位置 left = (img.width - video_width) // 2 top = (img.height - video_height) // 2 right = left + video_width bottom = top + video_height # 裁剪并返回终极图片 img = img.crop((left, top, right, bottom)) return img def create_video_from_images(folder_path, out_put_file_name='output_video.mp4', resolution='720p', is_horizontal=True, duration=3): """通过照片天生视频 Args: folder_path (_type_): 文件夹路径 out_put_file_name (str, optional): _description_. 视频输出路径 Defaults to 'output_video.mp4'. resolution (str, optional): _description_. 视频清晰度 Defaults to '720p'. is_horizontal (bool, optional): _description_. 是否是横屏 Defaults to True. duration (int, optional): _description_. 每张照片的放映时长 Defaults to 3. Raises: ValueError: 视频清晰度错误 """ resolution_mapping = { '720p': (1280, 720), '1080p': (1920, 1080), '2k': (2560, 1440), '4k': (3840, 2160), } if resolution not in resolution_mapping: raise ValueError( "Invalid resolution. Choose from '720p', '1080p', '2k', '4k'.") target_size = resolution_mapping[resolution] if not is_horizontal: target_size = (target_size[1], target_size[0]) images = [] # 读取文件夹下的文件并按照文件名排序 for filename in sorted(os.listdir(folder_path)): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp')): img = resize_and_crop(os.path.join(folder_path, filename), target_size) if img: images.append(img) # 照片的暂时位置 temp_files = [] for i, img in enumerate(images): temp_file = f'temp_image_{i}.png' img.save(temp_file) temp_files.append(temp_file) # 创建视频 clip = ImageSequenceClip(temp_files, fps=1 / duration) output_file = os.path.join(folder_path, out_put_file_name) clip.write_videofile(output_file, codec='libx264') # 打扫暂时文件 for temp_file in temp_files: os.remove(temp_file) if __name__ == '__main__': create_video_from_images( folder_path='/Users/xxxx/Downloads/xxx/imgs', is_horizontal=False, resolution='720p') [/code]我们执行脚本,这里是控制台输出: 再来看看视频输出: 标准的720P H264编码,3&,21s的时长。有一张图是横屏的图,这里天生的视频中也根据高度放大进行了居中裁剪: 团体的感觉还不错,特此写个博客分享出来。固然另有许多的优化点: 优化项实在做的还是相当的粗糙,但是根本上还是省事儿了。考虑到的优化点有:
咳,目前想到的就这么多。作为工具,我觉得越简单越好,必要服从和我开发时间的衡量。 到此这篇关于python实现照片集变成视频的代码实现的文章就先容到这了,更多相关python照片集变成视频内容请搜索脚本之家以前的文章或继续欣赏下面的相关文章希望各人以后多多支持脚本之家! 来源:https://www.jb51.net/python/328689laz.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 18:53 , Processed in 0.043333 second(s), 20 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.