def get_image_orientation(image_path):
with Image.open(image_path) as img:
return "landscape" if width > height else "portrait"
def convert_to_webp(image_path, output_folder, max_pixels=178956970):
with Image.open(image_path) as img:
if width * height > max_pixels:
print(f"Skipping {image_path} because it exceeds the size limit.")
output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + ".webp")
img.save(output_path, "webp")
print(f"Failed to convert {image_path}: {e}")
def process_images(input_folder, output_folder_landscape, output_folder_portrait):
for filename in os.listdir(input_folder):
if filename.endswith(('.jpg', '.jpeg', '.png')):
image_path = os.path.join(input_folder, filename)
orientation = get_image_orientation(image_path)
if orientation == "landscape":
convert_to_webp(image_path, output_folder_landscape)
convert_to_webp(image_path, output_folder_portrait)
print(f"Error processing {image_path}: {e}. Skipping this image.")
input_folder = "/root/input"
output_folder_landscape = "/root/landscape"
output_folder_portrait = "/root/portrait"
process_images(input_folder, output_folder_landscape, output_folder_portrait)