from PIL import Image def resize_image(input_image, output_image, size): try: with Image.open(input_image) as image: # Resize the image resized_image = image.resize(size) # Save the resized image resized_image.save(output_image) print("Image resized successfully!") except IOError: print("Unable to resize image") # Example usage input_image = "input.jpg" # Path to the input image output_image = "output.jpg" # Path to save the resized image size = (800, 600) # New size of the image (width, height) # Call the resize_image function resize_image(input_image, output_image, size)
Make sure you have the Pillow library installed. You can install it using the following command:
Copy code
pip install pillow
In the example code above, you need to replace the input_image variable with the path to your input image file. Likewise, update the output_image variable to specify the desired path and name for the resized image file. Lastly, modify the size variable to the desired dimensions for resizing the image.
After running the code, it will load the input image, resize it to the specified size, and save the resized image to the specified output path. You can adjust the code or incorporate it into a larger application according to your needs.