atom-predict/egnn_v2/resize.py

55 lines
2.7 KiB
Python
Raw Permalink 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
from PIL import Image
import cv2
# 替换为你的文件夹路径
folder_path = '/home/gao/mouclear/cc/final_todo/53/xj_norm/raw'
# 定义一个函数来调整图像大小
def resize_images(target_width, target_height, folder_path):
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
# 检查文件扩展名是否为图片格式
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
# 构造完整的文件路径
file_path = os.path.join(folder_path, filename)
print(f"Resizing {file_path}...")
# 打开图片文件
with Image.open(file_path) as img:
# 计算缩放比例,保持图像宽高比
# scale_width = target_width / img.width
# scale_height = target_height / img.height
# scale = min(scale_width, scale_height)
#
# # 计算新的尺寸
# new_width = int(img.width * scale)
# new_height = int(img.height * scale)
# 调整图片大小到指定宽度和高度
# original_width, original_height = img.size
#
# if target_width <= original_width:
# # 裁剪区域的坐标
# left = 0 # 从左上角开始裁剪
# top = 0 # 裁剪区域的起始y坐标可以按需调整
# right = target_width # 裁剪区域的结束x坐标
# bottom = original_height # 裁剪区域的结束y坐标保持高度不变
#
# # 裁剪图片
# resized_img = img.crop((left, top, right, bottom))
target_width, target_height = img.size
resized_img = img.resize((target_width, target_height), Image.LANCZOS)
# 保存调整后的图片,可以选择覆盖原图或者保存为新文件
resized_img.save(file_path) # 覆盖原图
image = cv2.imread(file_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# cv2.imwrite(file_path, image)
cv2.imwrite(file_path, image)
# 或者
# new_file_path = os.path.join(folder_path, f"resized_{filename}")
# resized_img.save(new_file_path) # 保存为新文件
# 打印完成信息
# print(f"All images have been resized to {new_width}x{new_height} pixels.")
# 允许用户指定目标宽度和高度
# target_width = int(512)
# target_height = int(256)
#
# # 调用函数,传入目标宽度、高度和文件夹路径
# resize_images(target_width, target_height, folder_path)