I have setup the docker container to run the model evaluation.
Write function resize_image in python that open get a folder path as in put and looks for all of the images files in that folder using only pillow and resize them to 32x32 and overwrite it. Just give me the python code that I can run by python code.py and the default folder is /tmp
Write function resize_image in python that open get a folder path as in put and looks for all of the images files in that folder using only pillow and resize them to 32x32 and overwrite it. Just give me the python code that I can run by python code.py and the default folder is /tmp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Import necessary libraries from PIL import Image import os def resize_image(folder_path='/tmp'): """ This function resizes all image files in a given folder to 32x32 pixels and overwrites the original files. Args: folder_path (str): The path to the folder containing the image files. Defaults to '/tmp'. """ # Check if the folder exists if not os.path.exists(folder_path): print(f"The folder {folder_path} does not exist.") return # Get a list of all files in the folder for filename in os.listdir(folder_path): # Check if the file is an image file if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')): # Open the image file img_path = os.path.join(folder_path, filename) try: with Image.open(img_path) as img: # Resize the image to 32x32 pixels img = img.resize((32, 32)) # Save the resized image, overwriting the original file img.save(img_path) print(f"Resized {filename} to 32x32 pixels.") except Exception as e: print(f"Error resizing {filename}: {str(e)}") if __name__ == "__main__": resize_image() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Import necessary libraries from PIL import Image import os def resize_image(folder_path='/tmp'): """ This function resizes all image files in a given folder to 32x32 pixels and overwrites the original files. Args: folder_path (str): The path to the folder containing the image files. Defaults to '/tmp'. """ # Check if the folder exists if not os.path.exists(folder_path): print(f"The folder {folder_path} does not exist.") return # Get a list of all files in the folder for filename in os.listdir(folder_path): # Check if the file is an image file if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')): # Open the image file img_path = os.path.join(folder_path, filename) try: with Image.open(img_path) as img: # Resize the image to 32x32 pixels img = img.resize((32, 32)) # Save the resized image, overwriting the original file img.save(img_path) print(f"Resized {filename} to 32x32 pixels.") except Exception as e: print(f"Error resizing {filename}: {str(e)}") if __name__ == "__main__": resize_image() |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Import necessary libraries from PIL import Image import os def resize_image(folder_path='/tmp'): """ This function resizes all image files in a given folder to 32x32 pixels and overwrites the original files. Args: folder_path (str): The path to the folder containing the image files. Defaults to '/tmp'. """ # Check if the folder exists if not os.path.exists(folder_path): print(f"The folder {folder_path} does not exist.") return # Get a list of all files in the folder for filename in os.listdir(folder_path): # Check if the file is an image file if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')): # Open the image file img_path = os.path.join(folder_path, filename) try: with Image.open(img_path) as img: # Resize the image to 32x32 pixels img = img.resize((32, 32)) # Save the resized image, overwriting the original file img.save(img_path) print(f"Resized {filename} to 32x32 pixels.") except Exception as e: print(f"Error resizing {filename}: {str(e)}") if __name__ == "__main__": resize_image() |
Resized random_image_7.jpeg to 32x32 pixels.
Resized random_image_8.jpg to 32x32 pixels.
Resized random_image_6.jpg to 32x32 pixels.
Resized random_image_1.jpg to 32x32 pixels.
Resized random_image_5.jpeg to 32x32 pixels.
Resized random_image_9.jpeg to 32x32 pixels.
Resized random_image_10.jpg to 32x32 pixels.
Resized random_image_4.jpg to 32x32 pixels.
Resized random_image_2.jpeg to 32x32 pixels.
Resized random_image_3.jpg to 32x32 pixels.
final: True Resulting in output: True