import argparse
from PIL import Image
import os
Image.MAX_IMAGE_PIXELS = None
def resize_images(directory, max_size):
# 将目录路径转换为绝对路径
directory = os.path.abspath(directory)
# 遍历目录中的图像文件
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
# 检查文件是否为图像文件
if os.path.isfile(file_path) and filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
if filename.startswith('resize_') or os.path.exists(os.path.join(directory, f"resize_{filename}")):
print(f"Skipping {filename}...")
continue
# 打开图像文件
image = Image.open(file_path)
# 获取图像的宽度和高度
width, height = image.size
# 计算缩放比例
if width > height:
scale_factor = max_size / width
else:
scale_factor = max_size / height
# 计算缩小后的尺寸
new_width = int(width * scale_factor)
new_height = int(height * scale_factor)
new_size = (new_width, new_height)
# 缩小图像
resized_image = image.resize(new_size, Image.LANCZOS)
# 构造输出文件路径
output_filename = f"resize_{filename}"
output_path = os.path.join(directory, output_filename)
# 保存缩小后的图像
resized_image.save(output_path)
# 关闭图像文件
image.close()
if __name__ == '__main__':
# 创建解析器并定义命令行参数
parser = argparse.ArgumentParser(description='Batch resize images.')
parser.add_argument('directory', help='Directory path (relative or absolute)')
parser.add_argument('max_size', type=int, help='Maximum size for the longest edge')
# 解析命令行参数
args = parser.parse_args()
# 调用函数进行图像批量缩小
resize_images(args.directory, args.max_size)
ansuir@Ansuirs-Mac-Mini ~/D/C/t/蜘蛛侠篇> python3 resize.py . 4000^C
ansuir@Ansuirs-Mac-Mini ~/D/C/t/蜘蛛侠篇> open .
ansuir@Ansuirs-Mac-Mini ~/D/C/t/蜘蛛侠篇> python3 resize.py . 4000
cc^CTraceback (most recent call last):
File "resize.py", line 63, in <module>
resize_images(args.directory, args.max_size)
File "resize.py", line 41, in resize_images
resized_image = image.resize(new_size, Image.LANCZOS)
File "/usr/local/lib/python3.8/site-packages/PIL/Image.py", line 2185, in resize
self.load()
File "/usr/local/lib/python3.8/site-packages/PIL/ImageFile.py", line 291, in load
n, err_code = decoder.decode(b)
KeyboardInterrupt
^C⏎ ansuir@Ansuirs-Mac-Mini ~/D/C/t/蜘蛛侠篇> python3 resize.py ./test/ 4000
ansuir@Ansuirs-Mac-Mini ~/D/C/t/蜘蛛侠篇> cat resize.py
import argparse
from PIL import Image
import os
Image.MAX_IMAGE_PIXELS = None
def resize_images(directory, max_size):
# 将目录路径转换为绝对路径
directory = os.path.abspath(directory)
# 遍历目录中的图像文件
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
# 检查文件是否为图像文件
if os.path.isfile(file_path) and filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
if filename.startswith('resize_') or os.path.exists(os.path.join(directory, f"resize_{filename}")):
print(f"Skipping {filename}...")
continue
# 打开图像文件
image = Image.open(file_path)
# 获取图像的宽度和高度
width, height = image.size
# 计算缩放比例
if width > height:
scale_factor = max_size / width
else:
scale_factor = max_size / height
# 计算缩小后的尺寸
new_width = int(width * scale_factor)
new_height = int(height * scale_factor)
new_size = (new_width, new_height)
# 缩小图像
resized_image = image.resize(new_size, Image.LANCZOS)
# 构造输出文件路径
output_filename = f"resize_{filename}"
output_path = os.path.join(directory, output_filename)
# 保存缩小后的图像
resized_image.save(output_path)
# 关闭图像文件
image.close()
if __name__ == '__main__':
# 创建解析器并定义命令行参数
parser = argparse.ArgumentParser(description='Batch resize images.')
parser.add_argument('directory', help='Directory path (relative or absolute)')
parser.add_argument('max_size', type=int, help='Maximum size for the longest edge')
# 解析命令行参数
args = parser.parse_args()
# 调用函数进行图像批量缩小
resize_images(args.directory, args.max_size)
|