Files
zjsh_yolov11/muju_cls/resize—image.py
琉璃月光 67883f1a50 最新推送
2026-03-10 14:14:14 +08:00

45 lines
1.2 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
# ================== 配置 ==================
IMAGE_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/error" # 修改为你的图片文件夹路径
TARGET_W = 1920
TARGET_H = 1080
PREFIX = "img" # 重命名前缀
EXT = ".png" # 统一保存格式(.jpg / .png
START_INDEX = 1 # 起始编号
# =========================================
# 支持的图片后缀
IMG_EXTS = (".jpg", ".jpeg", ".png", ".bmp", ".webp")
# 读取并排序(保证顺序稳定)
image_files = sorted([
f for f in os.listdir(IMAGE_DIR)
if f.lower().endswith(IMG_EXTS)
])
print(f"[INFO] 共找到 {len(image_files)} 张图片")
for idx, filename in enumerate(image_files, start=START_INDEX):
img_path = os.path.join(IMAGE_DIR, filename)
img = cv2.imread(img_path)
if img is None:
print(f"[WARN] 读取失败,跳过: {filename}")
continue
# resize
resized = cv2.resize(img, (TARGET_W, TARGET_H), interpolation=cv2.INTER_AREA)
# 新文件名
new_name = f"{PREFIX}_{idx:04d}{EXT}"
new_path = os.path.join(IMAGE_DIR, new_name)
# 保存
cv2.imwrite(new_path, resized)
print(f"[OK] {filename} -> {new_name}")
print("[DONE] 全部处理完成")