Dish_Recognition/tools/目标统一尺寸.py

44 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import cv2
from pathlib import Path
# ===================== 【配置项】 =====================
INPUT_FOLDER = r"/mnt/nvme/Model_YUE/Dish/Copy" # 你的裁剪图片文件夹
OUTPUT_FOLDER = r"/mnt/nvme/Model_YUE/Dish/Copy_224" # 输出文件夹
# 目标统一尺寸(YOLO-CLS 推荐 224)
TARGET_SIZE = (224, 224)
# 支持的图片格式
IMAGE_FORMATS = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff']
# ======================================================
def resize_images_high_quality(input_dir, output_dir, target_size):
os.makedirs(output_dir, exist_ok=True)
files = []
for ext in IMAGE_FORMATS:
files.extend(Path(input_dir).glob(f"*{ext}"))
print(f"📂 找到 {len(files)} 张图片,开始统一缩放为 {target_size}...")
for img_path in files:
img = cv2.imread(str(img_path))
if img is None:
print(f"❌ 读取失败:{img_path}")
continue
# 高质量缩放(最清晰,无损模式)
resized_img = cv2.resize(
img,
target_size,
interpolation=cv2.INTER_LANCZOS4 # 画质最好
)
# 保存到输出目录
save_path = os.path.join(output_dir, Path(img_path).name)
cv2.imwrite(save_path, resized_img)
print(f"\n🎉 全部完成!缩放后图片保存在:{output_dir}")
if __name__ == "__main__":
resize_images_high_quality(INPUT_FOLDER, OUTPUT_FOLDER, TARGET_SIZE)