File size: 1,252 Bytes
ffc9a51 |
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 |
from pathlib import Path
from PIL import Image
Image.MAX_IMAGE_PIXELS = 1000000000
def resize_image(file_path, scale_percent):
with Image.open(file_path) as img:
width, height = img.size
new_width = int(width * scale_percent / 100)
new_height = int(height * scale_percent / 100)
img_resized = img.resize((new_width, new_height))
return img_resized
def process_files_in_directory(directory_path, size_threshold, scale_percent):
dir_path = Path(directory_path)
for file_path in dir_path.glob('*.png'):
file_size = file_path.stat().st_size
if file_size >= size_threshold:
img_resized = resize_image(file_path, scale_percent)
img_resized.save(file_path)
resized_file_size = file_path.stat().st_size # リサイズ後のファイルサイズを取得
print(f"Resized and saved {file_path} with new size: {resized_file_size} bytes")
if __name__ == "__main__":
directory_path = r"E:\Dataset\XXXXXXX" # ここにディレクトリへのパスを入力してください
size_threshold = 32 * 1024 * 1024 # 32MB
scale_percent = 50 # 縮小率50%
process_files_in_directory(directory_path, size_threshold, scale_percent)
|