from PIL import Image import os def reduce_size(input_image_path, output_image_path, max_size): original_image = Image.open(input_image_path) width, height = original_image.size aspect_ratio = height/width if width > max_size: new_width = max_size new_height = int(aspect_ratio * new_width) new_image = original_image.resize((new_width, new_height), Image.ANTIALIAS) new_image.save(output_image_path, optimize=True, quality=85) return True else: return False # Define the input and output image paths and the maximum size input_image_path = 'input_image.jpg' output_image_path = 'output_image.jpg' max_size = 800 # Call the reduce_size function and check if it was successful success = reduce_size(input_image_path, output_image_path, max_size) # Print a message based on the success of the operation if success: print(f"Image was successfully resized and saved to {os.path.abspath(output_image_path)}") else: print("Image was already smaller than the maximum size and was not resized.")