import cv2 import os def resize_image(image_path, width, height): """调整图片大小""" img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) resized = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA) cv2.imwrite(image_path, resized) def extract_frames(video_path, target_path): """提取视频帧并保存封面图""" try: vc = cv2.VideoCapture(video_path) # 读取视频 success, frame = vc.read() # 读取当前帧,success用于判断读取是否成功 count = 0 # 初始化计数器 while success: file_name = os.path.splitext(os.path.basename(video_path))[0] + f'_{count}.jpg' frame_path = os.path.join(target_path, file_name) cv2.imwrite(frame_path, frame) # 将当前帧保存为图片到 frame_path resize_image(frame_path, 2560, 1440) # 调整图片大小 success, frame = vc.read() # 继续读取下一帧 count += 1 # 计数器加1 except Exception as e: print(f"获取视频帧失败: {e}") if __name__ == '__main__': video_folder = 'D:/path/to/videos/1/银二-主井皮带_20230523151417' # 视频文件夹目录 target_path = 'D:/path/to/frames/1' # 帧截图保存路径 if not os.path.exists(target_path): # 如果目标路径不存在原文件夹的话就创建 os.makedirs(target_path) for file_name in os.listdir(video_folder): file_path = os.path.join(video_folder, file_name) if os.path.isfile(file_path) and file_name.endswith('.mp4'): extract_frames(file_path, target_path) print("程序执行完毕!")
时间: 2023-12-30 11:05:49 浏览: 302
这是一段Python代码,主要功能是从指定的视频文件夹中提取视频帧并保存为图片。具体实现过程如下:
1. 引入cv2和os模块。
2. 定义resize_image函数,用于调整图片大小。该函数接收三个参数:图片路径、目标宽度和目标高度。
3. 定义extract_frames函数,用于提取视频帧并保存封面图。该函数接收两个参数:视频路径和目标路径。
4. 在extract_frames函数中,通过cv2.VideoCapture读取视频,并依次读取每一帧进行处理。在读取每一帧时,先生成当前帧的文件名,然后将该帧写入到指定路径下的文件中。
5. 调用resize_image函数,将图片大小调整为指定的宽度和高度。
6. 在主函数中,指定视频文件夹路径和目标路径,并循环遍历视频文件夹下的所有mp4格式的视频文件。对于每一个mp4格式的视频文件,调用extract_frames函数提取视频帧并保存为图片。
7. 输出程序执行完毕的提示信息。
需要注意的是,该代码需要安装OpenCV库才能正常运行。
相关问题
image.resize
`image.resize`通常是指图像处理库中的一个函数,用于调整图片的尺寸或大小。在Python中,这个功能可能存在于不同的库中,如PIL(Python Imaging Library)或者OpenCV等。
- **PIL (Pillow)库**: 如果你在使用Pillow(PIL的一个分支,更现代且有更多功能),你可以用`Image`对象的`resize()`方法。例如:
```python
from PIL import Image
def resize_image(image_path, new_size):
with Image.open(image_path) as im:
resized_im = im.resize(new_size)
resized_im.save('resized_' + image_path)
# 使用方法
resize_image('input.jpg', (800, 600))
```
- **OpenCV库**: OpenCV同样提供图像缩放功能,但语法稍有不同:
```python
import cv2
def resize_cv2(image_path, new_width, new_height):
img = cv2.imread(image_path)
resized_img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
cv2.imwrite('resized_' + image_path, resized_img)
# 使用方法
resize_cv2('input.jpg', 800, 600)
```
在这个例子中,`resize()`函数接收两个参数,一个是原始图片文件路径,另一个是新的宽度和高度,可以根据需求指定保持宽高比(例如`(width, height)`)或直接传入新尺寸`(new_size)`。
写一段 python 代码,可以用 cv2 在不改变原有图片的比例的前提下,将一个不是 640×640的图片转换成640×640, 用 pad 功能将新图片比例外的 pixel 填充为黑色
以下是可以实现此功能的 Python 代码:
```python
import cv2
import numpy as np
def resize_pad_image(image_path):
img = cv2.imread(image_path)
height, width, _ = img.shape
if height > width:
scale = 640 / height
new_height = 640
new_width = int(width * scale)
else:
scale = 640 / width
new_width = 640
new_height = int(height * scale)
resized_img = cv2.resize(img, (new_width, new_height))
pad_x = int((640 - new_width) / 2)
pad_y = int((640 - new_height) / 2)
padded_img = cv2.copyMakeBorder(resized_img, pad_y, pad_y, pad_x, pad_x, cv2.BORDER_CONSTANT, value=[0, 0, 0])
return padded_img
```
使用方法:
```python
padded_img = resize_pad_image('image.jpg')
cv2.imshow('Padded Image', padded_img)
cv2.waitKey(0)
```
其中,`image.jpg` 是需要转换的图片路径。此函数将返回一个大小为 640x640 的经过填充的图片。
阅读全文
相关推荐
















