-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize.py
More file actions
51 lines (41 loc) · 1.83 KB
/
Copy pathresize.py
File metadata and controls
51 lines (41 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from PIL import Image
# ========== 配置 ==========
SOURCE_FOLDER = "E:/zml/celebahq_test" # 源文件夹路径(存放原始图片)
OUTPUT_FOLDER = "E:/zml/celebahq256_test" # 输出文件夹路径(存放调整后图片)
TARGET_SIZE = (256, 256) # 目标尺寸:宽 x 高
# 支持的图片扩展名
IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp'}
# ========== 主程序 ==========
def resize_images():
# 创建输出文件夹(如果不存在)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# 获取所有图片文件
image_files = [
f for f in os.listdir(SOURCE_FOLDER)
if os.path.isfile(os.path.join(SOURCE_FOLDER, f)) and
os.path.splitext(f)[1].lower() in IMAGE_EXTENSIONS
]
if not image_files:
print("⚠️ 源文件夹中没有找到支持的图片文件。")
return
print(f"📁 找到 {len(image_files)} 张图片,开始处理...")
success_count = 0
for filename in image_files:
src_path = os.path.join(SOURCE_FOLDER, filename)
dst_path = os.path.join(OUTPUT_FOLDER, filename)
try:
with Image.open(src_path) as img:
# 调整尺寸(使用 LANCZOS 高质量重采样)
resized_img = img.resize(TARGET_SIZE, Image.Resampling.LANCZOS)
# 保存(自动处理格式)
resized_img.save(dst_path)
print(f"✅ {filename} -> {TARGET_SIZE}")
success_count += 1
except Exception as e:
print(f"❌ 处理失败 {filename}: {e}")
print(f"\n🎉 完成!成功处理 {success_count}/{len(image_files)} 张图片。")
print(f"📁 输出文件夹: {os.path.abspath(OUTPUT_FOLDER)}")
# ========== 运行 ==========
if __name__ == "__main__":
resize_images()