python實現(xiàn)照片集變成視頻的代碼實現(xiàn)
背景
一個安靜的下午,看著電腦里亂七八糟的照片,有大有小,寬高不一,突然想找個方式把他們統(tǒng)一起來,然后做成視頻更好(其實我在上高中的時候就喜歡把照片做成視頻,覺得意義很大)。要滿足批量、自動化,肯定得動用代碼了。于是首先我列舉了下我希望的功能:
照片來源:制定的目錄下所有的文件格式為照片的文件,按照照片的文件名進(jìn)行排序
照片質(zhì)量:按照目前的720P、1080P、甚至是2K、4K的畫質(zhì)來生成照片視頻
照片橫豎:可以自定義指定照片是橫屏還是豎屏
照片時間:可以自定義每一張照片的放映時間
照片比例:這個也是最為重要的,對此我分成了如下幾種case:
圖片的寬度 < 視頻寬度 * 50% or 圖片的高度 < 視頻高度 * 50%:舍棄掉
圖片寬度 < 視頻寬度 or 圖片的高度 < 視頻高度:居中等比放大,直到高度 = 視頻高度 or 寬度 = 視頻寬度=
其它尺寸,圖片居中等比縮小,直到高度 = 視頻高度 or 寬度 = 視頻寬度
這種照片比例的放大居中,基本上是強迫癥患者的福音了,嚴(yán)格的居中對齊。
實現(xiàn)
依托強大的python庫,這里主要用到的工具庫有:
Pillow,源代碼有簡單、精煉的解釋:Pillow is the friendly PIL fork by Alex Clark and Contributors.PIL is the Python Imaging Library by Fredrik Lundh and Contributors. 機器學(xué)習(xí)、圖像識別等場景用到的最多,這里主要使用它來調(diào)整圖片的大小MoviePy, 是一個用于視頻編輯的 Python 庫??梢?strong>剪輯視頻、添加音頻和字幕、調(diào)整視頻幀、簡單的特效等等。這里主要是根據(jù)照片序列生成視頻。
介紹完主要的庫之后,就是代碼環(huán)節(jié)了,代碼里注釋較多,輕松入手,不做過多的解釋:
# 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):
"""將圖片根據(jù)給定的大小進(jìn)行縮放和裁剪
?
1. 圖片的寬度 < 視頻寬度 * 50% or 圖片的高度 < 視頻高度 * 50%:舍棄掉
2. 圖片寬度 < 視頻寬度 or 圖片的高度 < 視頻高度:居中等比放大,直到高度 = 視頻高度 or 寬度 = 視頻寬度
3. 圖片居中等比縮小,直到高度 = 視頻高度 or 寬度 = 視頻寬度
?
Args:
image (str): 原圖片文件路徑
target_size (tuple): 視頻大小(寬度, 高度)
?
Returns:
Image: 調(diào)整后的圖片,可能為空
"""
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
?
# 計算目標(biāo)縮放比例
scale_x = video_width / img.width
scale_y = video_height / img.height
?
# 根據(jù)需要的寬度和高度選擇縮放比例
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)
?
# 創(chuàng)建視頻
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')
我們執(zhí)行腳本,這里是控制臺輸出:

再來看看視頻輸出:

標(biāo)準(zhǔn)的720P H264編碼,3&,21s的時長。有一張圖是橫屏的圖,這里生成的視頻中也根據(jù)高度放大進(jìn)行了居中裁剪:

整體的感覺還不錯,特此寫個博客分享出來。當(dāng)然還有很多的優(yōu)化點:
優(yōu)化項
其實做的還是相當(dāng)?shù)拇植?,但是基本上還是省事兒了??紤]到的優(yōu)化點有:
- 指定背景音樂并實現(xiàn)照片卡點
- 加上隨機的特效切換
咳,目前想到的就這么多。作為工具,我覺得越簡單越好,需要效率和我開發(fā)時間的權(quán)衡。
到此這篇關(guān)于python實現(xiàn)照片集變成視頻的代碼實現(xiàn)的文章就介紹到這了,更多相關(guān)python照片集變成視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)文字pdf轉(zhuǎn)換圖片pdf效果
當(dāng)我們把word轉(zhuǎn)化為pdf,wps默認(rèn)轉(zhuǎn)化為文字pdf,而圖片pdf要會員。所以本文將通過Python語言實現(xiàn)文字pdf轉(zhuǎn)換圖片pdf,需要的可以參考一下2022-04-04
關(guān)于Python的json字符串與json模塊解讀
這篇文章主要介紹了關(guān)于Python的json字符串與json模塊解讀,JSON采用完全獨立于語言的文本格式,但是也使用了類似于C語言家族的習(xí)慣(包括C,?C++,?C#,?Java,?JavaScript,?Perl,?Python等),這些特性使JSON成為理想的數(shù)據(jù)交換語言,需要的朋友可以參考下2023-07-07

