|
import os |
|
from PIL import Image |
|
import concurrent.futures |
|
|
|
|
|
input_directory = "../original-images" |
|
output_directory = "../resized-original-images-test" |
|
|
|
|
|
os.makedirs(output_directory, exist_ok=True) |
|
|
|
def resize_image(input_path, output_path): |
|
with Image.open(input_path) as img: |
|
|
|
current_megapixels = (img.width * img.height) / 1_000_000 |
|
max_megapixels = 10 |
|
|
|
if current_megapixels > max_megapixels: |
|
|
|
scaling_factor = (max_megapixels / current_megapixels) ** 0.5 |
|
new_size = (int(img.width * scaling_factor), int(img.height * scaling_factor)) |
|
|
|
resized_img = img.resize(new_size, Image.LANCZOS) |
|
|
|
resized_img.save(output_path) |
|
else: |
|
|
|
img.save(output_path) |
|
|
|
def main(): |
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: |
|
for filename in os.listdir(input_directory): |
|
if filename.endswith(('.png', '.jpg', '.jpeg')): |
|
input_path = os.path.join(input_directory, filename) |
|
output_path = os.path.join(output_directory, filename) |
|
|
|
if not os.path.exists(output_path): |
|
executor.submit(resize_image, input_path, output_path) |
|
print(f"Submitted {filename} for resizing.") |
|
else: |
|
print(f"Skipped {filename}, already exists in {output_directory}") |
|
|
|
print("All images have been resized and saved to the output directory.") |
|
|
|
if __name__ == "__main__": |
|
main() |