Docker Setup

I have setup the docker container to run the model evaluation.

Initial Query

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

LLM Generation

Query

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

Output

Here's a Python script that uses Pillow to resize all image files in a specified folder to 32x32 pixels:

 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
36
37
38
39
40
41
42
import os
from PIL import Image

def resize_image(folder_path):
    # List of common image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")

def main():
    # Default folder path
    folder_path = '/tmp'
    
    # Allow user to specify a different folder path via command-line argument
    import sys
    if len(sys.argv) > 1:
        folder_path = sys.argv[1]
    
    # Resize images in the specified folder
    resize_image(folder_path)

if __name__ == "__main__":
    main()
To use this script: 1. Save it as `code.py` 2. Make sure you have Pillow installed (`pip install Pillow`) 3. Run it using: - `python code.py` (uses `/tmp` folder by default) - `python code.py /path/to/your/folder` (to specify a different folder) Key features: - Supports multiple image formats - Uses LANCZOS resampling for high-quality resizing - Handles potential errors when processing images - Allows specifying a folder via command-line argument - Overwrites original images with resized versions Note: Be careful as this script will modify the original images in the specified folder.

Extract Code

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
36
37
38
39
40
41
42
import os
from PIL import Image

def resize_image(folder_path):
    # List of common image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")

def main():
    # Default folder path
    folder_path = '/tmp'
    
    # Allow user to specify a different folder path via command-line argument
    import sys
    if len(sys.argv) > 1:
        folder_path = sys.argv[1]
    
    # Resize images in the specified folder
    resize_image(folder_path)

if __name__ == "__main__":
    main()

Run Code Interpreter

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
36
37
38
39
40
41
42
import os
from PIL import Image

def resize_image(folder_path):
    # List of common image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")

def main():
    # Default folder path
    folder_path = '/tmp'
    
    # Allow user to specify a different folder path via command-line argument
    import sys
    if len(sys.argv) > 1:
        folder_path = sys.argv[1]
    
    # Resize images in the specified folder
    resize_image(folder_path)

if __name__ == "__main__":
    main()
And got the output: Resized: random_image_9.jpg Resized: random_image_1.jpg Resized: random_image_5.jpeg Resized: random_image_7.jpg Resized: random_image_4.jpeg Resized: random_image_10.jpg Resized: random_image_2.jpeg Resized: random_image_3.jpeg Resized: random_image_8.jpeg Resized: random_image_6.jpeg

PyFunc

final: True Resulting in output: True

Docker Setup

I have setup the docker container to run the model evaluation.

Initial Query

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

LLM Generation

Query

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

Output

Here's a Python script that uses Pillow to resize images in a specified folder to 32x32 pixels:

 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
36
37
38
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of common image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        filepath = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(filepath) and any(filename.lower().endswith(ext) for ext in image_extensions):
            try:
                # Open the image
                with Image.open(filepath) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(filepath)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If script is run directly
if __name__ == '__main__':
    # You can specify a different folder path here if needed
    resize_image()
To use this script: 1. Make sure you have Pillow installed. If not, install it using:
1
2
   pip install pillow
   
2. Save the script as `code.py` 3. Run the script:
1
2
   python code.py
   
Key features: - Defaults to `/tmp` folder if no path is specified - Supports common image formats - Uses LANCZOS resampling for high-quality resizing - Prints progress and total processed images - Overwrites original images - Error handling for problematic image files You can modify the folder path by calling `resize_image('/path/to/your/folder')` or changing the default parameter.

Extract Code

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
36
37
38
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of common image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        filepath = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(filepath) and any(filename.lower().endswith(ext) for ext in image_extensions):
            try:
                # Open the image
                with Image.open(filepath) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(filepath)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If script is run directly
if __name__ == '__main__':
    # You can specify a different folder path here if needed
    resize_image()

Run Code Interpreter

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
36
37
38
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of common image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        filepath = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(filepath) and any(filename.lower().endswith(ext) for ext in image_extensions):
            try:
                # Open the image
                with Image.open(filepath) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(filepath)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If script is run directly
if __name__ == '__main__':
    # You can specify a different folder path here if needed
    resize_image()
And got the output: Resized: random_image_5.jpg Resized: random_image_1.jpeg Resized: random_image_7.jpeg Resized: random_image_9.jpg Resized: random_image_6.jpg Resized: random_image_2.jpg Resized: random_image_4.jpeg Resized: random_image_10.jpg Resized: random_image_3.jpeg Resized: random_image_8.jpeg Total images processed: 10

PyFunc

final: True Resulting in output: True

Docker Setup

I have setup the docker container to run the model evaluation.

Initial Query

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

LLM Generation

Query

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

Output

Here's a Python script that uses Pillow to resize images in a specified folder to 32x32 pixels:

 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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of supported image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If the script is run directly
if __name__ == '__main__':
    resize_image()
Here's how to use the script: 1. Make sure you have Pillow installed. If not, install it using:
1
2
   pip install Pillow
   
2. Save the script as `code.py` 3. Run the script:
1
2
   python code.py
   
By default, it will process images in `/tmp`. You can also specify a different folder:
1
resize_image('/path/to/your/image/folder')
Features: - Supports multiple image formats - Resizes images to 32x32 pixels - Uses high-quality Lanczos resampling - Overwrites original images - Prints progress and total processed images - Handles potential errors during processing

Extract Code

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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of supported image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If the script is run directly
if __name__ == '__main__':
    resize_image()

Run Code Interpreter

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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of supported image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if the file is an image
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize the image to 32x32
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If the script is run directly
if __name__ == '__main__':
    resize_image()
And got the output: Resized: random_image_1.jpeg Resized: random_image_9.jpg Resized: random_image_8.jpg Resized: random_image_5.jpeg Resized: random_image_7.jpg Resized: random_image_4.jpeg Resized: random_image_10.jpg Resized: random_image_2.jpeg Resized: random_image_3.jpg Resized: random_image_6.jpeg Total images processed: 10

PyFunc

final: True Resulting in output: True

Docker Setup

I have setup the docker container to run the model evaluation.

Initial Query

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

LLM Generation

Query

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

Output

Here's a Python script that uses Pillow to resize images in a specified folder to 32x32 pixels:

 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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of allowed image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if it's a file and has an image extension
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize image to 32x32 using LANCZOS resampling (high-quality downsampling)
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If script is run directly
if __name__ == '__main__':
    resize_image()
To use this script: 1. Make sure you have Pillow installed:
1
pip install Pillow
2. Save the script as `code.py` 3. Run it:
1
python code.py
Features: - Default folder is `/tmp` - Supports multiple image formats - Resizes images to exactly 32x32 pixels - Uses high-quality LANCZOS resampling - Overwrites original images - Prints progress and total processed images You can also specify a different folder:
1
resize_image('/path/to/your/folder')
The script will: - Check each file in the folder - Verify it's an image file - Resize to 32x32 - Overwrite the original file - Handle potential errors (like corrupted images)

Extract Code

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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of allowed image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if it's a file and has an image extension
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize image to 32x32 using LANCZOS resampling (high-quality downsampling)
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If script is run directly
if __name__ == '__main__':
    resize_image()

Run Code Interpreter

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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # List of allowed image file extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get full file path
        file_path = os.path.join(folder_path, filename)
        
        # Check if it's a file and has an image extension
        if os.path.isfile(file_path) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(file_path) as img:
                    # Resize image to 32x32 using LANCZOS resampling (high-quality downsampling)
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(file_path)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If script is run directly
if __name__ == '__main__':
    resize_image()
And got the output: Resized: random_image_1.jpg Resized: random_image_5.jpeg Resized: random_image_7.jpg Resized: random_image_9.jpeg Resized: random_image_10.jpg Resized: random_image_4.jpg Resized: random_image_2.jpeg Resized: random_image_3.jpeg Resized: random_image_8.jpeg Resized: random_image_6.jpeg Total images processed: 10

PyFunc

final: True Resulting in output: True

Docker Setup

I have setup the docker container to run the model evaluation.

Initial Query

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

LLM Generation

Query

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

Output

Here's a Python script that uses Pillow to resize all image files in a specified folder to 32x32 pixels:

 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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # Supported image extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        filepath = os.path.join(folder_path, filename)
        
        # Check if the file is an image based on extension
        if os.path.isfile(filepath) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(filepath) as img:
                    # Resize the image to 32x32 pixels
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(filepath)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If the script is run directly
if __name__ == '__main__':
    resize_image()
To use this script: 1. Make sure you have Pillow installed. If not, install it using:
1
2
   pip install pillow
   
2. Save the script as `code.py` 3. Run the script:
1
2
   python code.py
   
By default, it will process images in `/tmp`. You can also specify a different folder when calling the function:
1
resize_image('/path/to/your/folder')
Features: - Supports multiple image formats - Uses high-quality LANCZOS resampling - Prints progress and handles errors - Overwrites original images - Defaults to `/tmp` if no folder specified

Extract Code

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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # Supported image extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        filepath = os.path.join(folder_path, filename)
        
        # Check if the file is an image based on extension
        if os.path.isfile(filepath) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(filepath) as img:
                    # Resize the image to 32x32 pixels
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(filepath)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If the script is run directly
if __name__ == '__main__':
    resize_image()

Run Code Interpreter

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
36
37
import os
from PIL import Image

def resize_image(folder_path='/tmp'):
    # Supported image extensions
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    
    # Counter for processed images
    processed_count = 0
    
    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        # Get the full file path
        filepath = os.path.join(folder_path, filename)
        
        # Check if the file is an image based on extension
        if os.path.isfile(filepath) and os.path.splitext(filename)[1].lower() in image_extensions:
            try:
                # Open the image
                with Image.open(filepath) as img:
                    # Resize the image to 32x32 pixels
                    resized_img = img.resize((32, 32), Image.LANCZOS)
                    
                    # Save the resized image, overwriting the original
                    resized_img.save(filepath)
                    
                    processed_count += 1
                    print(f"Resized: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {e}")
    
    print(f"\nTotal images processed: {processed_count}")

# If the script is run directly
if __name__ == '__main__':
    resize_image()
And got the output: Resized: random_image_5.jpg Resized: random_image_1.jpeg Resized: random_image_9.jpg Resized: random_image_8.jpg Resized: random_image_7.jpg Resized: random_image_4.jpg Resized: random_image_2.jpeg Resized: random_image_3.jpg Resized: random_image_10.jpeg Resized: random_image_6.jpeg Total images processed: 10

PyFunc

final: True Resulting in output: True