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
Here's the Python code that resizes all images in a specified folder (default: `/tmp`) to 32x32 using Pillow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import os from PIL import Image def resize_image(folder='/tmp'): for filename in os.listdir(folder): filepath = os.path.join(folder, filename) if os.path.isfile(filepath): try: with Image.open(filepath) as img: resized_img = img.resize((32, 32)) ext = os.path.splitext(filename)[1].lower() if ext in ['.jpg', '.jpeg'] and resized_img.mode != 'RGB': resized_img = resized_img.convert('RGB') resized_img.save(filepath) except IOError: pass 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 | import os from PIL import Image def resize_image(folder='/tmp'): for filename in os.listdir(folder): filepath = os.path.join(folder, filename) if os.path.isfile(filepath): try: with Image.open(filepath) as img: resized_img = img.resize((32, 32)) ext = os.path.splitext(filename)[1].lower() if ext in ['.jpg', '.jpeg'] and resized_img.mode != 'RGB': resized_img = resized_img.convert('RGB') resized_img.save(filepath) except IOError: pass 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 | import os from PIL import Image def resize_image(folder='/tmp'): for filename in os.listdir(folder): filepath = os.path.join(folder, filename) if os.path.isfile(filepath): try: with Image.open(filepath) as img: resized_img = img.resize((32, 32)) ext = os.path.splitext(filename)[1].lower() if ext in ['.jpg', '.jpeg'] and resized_img.mode != 'RGB': resized_img = resized_img.convert('RGB') resized_img.save(filepath) except IOError: pass if __name__ == "__main__": resize_image() |
final: True Resulting in output: True