_________ / Zatta /file_compressor.py
Default38693's picture
Upload 8 files
ffc9a51
raw
history blame
1.25 kB
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)