基于Python实现批量缩放图片(视频)尺寸
作者:像风一样的男人@ 时间:2021-03-23 12:21:22
方法一
在 Pillow 中,resize() 方法会强制将图片缩放到指定的大小,可能会导致图片变形或失真;而 thumbnail() 方法则会按比例缩小图片,同时保持原始宽高比,不会变形或失真。
具体来说,thumbnail() 方法会根据指定的最大宽度和高度,计算出合适的缩放比例,然后将图片按照该比例缩小,最终得到的图片宽度和高度都不会超过指定的大小。而 resize() 方法则直接将图片强制缩放到指定的大小,如果指定的大小与原始图片宽高比不同,则会变形或失真。
因此,如果要保持图片原始宽高比并缩小图片,建议使用 thumbnail() 方法;如果需要将图片强制缩放到指定大小,可以使用 resize() 方法,但要注意可能会导致失真或变形。
from PIL import Image
import os
dir = r'C:\Users\1\Desktop\ffmpeg\bin\final_results'
out = r'C:\Users\1\Desktop\ffmpeg\bin\results'
def resize_image():
for path in os.listdir(dir):
img = os.path.join(dir, path)
print(f'缩放图:【{img}】')
with Image.open(img) as img:
img.thumbnail((1280, 720))
img.save(os.path.join(out, path))
# 调用示例
resize_image()
方法二
import cv2
def resize_image(input_path, output_path, size):
img = cv2.imread(input_path)
height, width, _ = img.shape
if width > height:
scale = size / width
else:
scale = size / height
img = cv2.resize(img, (int(width * scale), int(height * scale)))
cv2.imwrite(output_path, img)
# 调用示例
input_path = 'input.jpg'
output_path = 'output.jpg'
size = 640 # 宽度或高度为 640
resize_image(input_path, output_path, size)
在这个示例代码中,resize_image 函数接收三个参数:输入文件路径 input_path、输出文件路径 output_path,以及目标大小 size。在函数内部,首先用 cv2.imread 函数读取输入文件,并使用 shape 属性获取图片的宽度和高度。然后根据宽度和高度的大小关系,计算出缩放比例。最后使用 cv2.resize 函数将图片缩放到指定大小,并使用 cv2.imwrite 函数将缩放后的图片保存到输出文件。
需要注意的是,使用 OpenCV 库可能需要先安装相关依赖,具体安装方法可以查阅相关文档。另外,使用 OpenCV 库缩放图片时,要注意保持宽高比,避免变形或失真。具体来说,可以根据图片的宽高比,计算出缩放比例并将图片缩小到合适的尺寸。
方法三
from PIL import Image
def resize_image(input_path, output_path, max_size):
img = Image.open(input_path)
width, height = img.size
if max(width, height) > max_size:
if width > height:
new_width = max_size
new_height = int(height * (max_size / width))
else:
new_width = int(width * (max_size / height))
new_height = max_size
img = img.resize((new_width, new_height))
img.save(output_path)
# 调用示例
input_path = 'input.jpg'
output_path = 'output.jpg'
max_size = 640 # 最大边长为 640
resize_image(input_path, output_path, max_size)
在这个示例代码中,resize_image 函数接收三个参数:输入文件路径 input_path、输出文件路径 output_path,以及最大边长 max_size。在函数内部,首先使用 Image.open 函数读取输入文件,并使用 size 属性获取图片的宽度和高度。然后根据最大边长和图片大小的关系,计算出缩放比例。最后使用 resize 函数将图片缩放到指定大小,并使用 save 函数将缩放后的图片保存到输出文件。
需要注意的是,使用 Pillow 库的 resize 方法时,也要注意保持图片的宽高比,避免变形或失真。具体来说,可以根据图片的宽高比,计算出缩放比例并将图片缩小到合适的尺寸。
方法四(FFmpeg)
使用 ffmpeg 缩放视频尺寸的命令格式如下:
ffmpeg -i input.mp4 -vf scale=: output.mp4
其中,和 分别代表缩放后的视频宽度和高度,可以指定为具体的像素值,也可以使用百分比,如 50%。
例如,将一个输入视频文件 input.mp4 缩放为宽度为 640 像素,高度为 360 像素的输出视频文件 output.mp4,可以使用以下命令:
ffmpeg -i input.mp4 -vf scale=640:360 output.mp4
如果希望按照原始视频宽高比自动缩放,可以只指定宽度或高度,并保留原始视频宽高比:
# 指定宽度为 640 像素,高度按原始比例缩放
ffmpeg -i input.mp4 -vf scale=640:-1 output.mp4
# 指定高度为 360 像素,宽度按原始比例缩放
ffmpeg -i input.mp4 -vf scale=-1:360 output.mp4
还可以通过在 scale 参数前添加 force_original_aspect_ratio 选项,让 ffmpeg 在缩放过程中保留原始视频宽高比,以免变形:
ffmpeg -i input.mp4 -vf "scale=640:360:force_original_aspect_ratio=decrease,pad=640:360:(ow-iw)/2:(oh-ih)/2" output.mp4
来源:https://blog.csdn.net/weixin_44634704/article/details/129713362
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python利用Pygame实现翻转图像
![](https://img.aspxhome.com/file/2023/5/126895_0s.webp)
conda查看、创建、删除、激活与退出环境命令详解
![](https://img.aspxhome.com/file/2023/0/103530_0s.png)
利用ASP.NET MVC和Bootstrap快速搭建响应式个人博客站(一)
![](https://img.aspxhome.com/file/2023/6/56056_0s.jpg)
python可视化大屏库big_screen示例详解
![](https://img.aspxhome.com/file/2023/7/87487_0s.jpg)
Python虚拟环境项目实例
![](https://img.aspxhome.com/file/2023/1/100881_0s.png)
MySQL数据库中与 ALTER TABLE 有关的问题
Python实现模拟分割大文件及多线程处理的方法
PHP实现PDF转图片的详细过程(使用imagick)
![](https://img.aspxhome.com/file/2023/3/55523_0s.png)
Python使用Web框架Flask开发项目
Pyspark读取parquet数据过程解析
![](https://img.aspxhome.com/file/2023/9/109399_0s.png)
python命令行工具Click快速掌握
Windows环境下python环境安装使用图文教程
![](https://img.aspxhome.com/file/2023/9/123659_0s.png)
Pytorch中retain_graph的坑及解决
Python使用requests发送POST请求实例代码
简单介绍一下pyinstaller打包以及安全性的实现
![](https://img.aspxhome.com/file/2023/8/78718_0s.png)
如何利用ASP实现邮箱访问
Python&Matlab实现伏羲八卦图的绘制
![](https://img.aspxhome.com/file/2023/5/121635_0s.gif)
Flask 入门系列 Cookie与session的介绍
![](https://img.aspxhome.com/file/2023/9/103769_0s.png)
Python实现12306火车票抢票系统
![](https://img.aspxhome.com/file/2023/2/81792_0s.jpg)
win10 64bit下python NLTK安装教程
![](https://img.aspxhome.com/file/2023/6/68276_0s.jpg)