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 does what you described. Save it as `code.py`:

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    try:
        # Iterate through all files in the folder
        for filename in os.listdir(folder_path):
            if filename.lower().endswith(image_extensions):
                file_path = os.path.join(folder_path, filename)
                
                # Open the image
                with Image.open(file_path) as img:
                    # Convert image to RGB if it's in RGBA
                    if img.mode == 'RGBA':
                        img = img.convert('RGB')
                    
                    # Resize the image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save and overwrite the original image
                    resized_img.save(file_path)
                    print(f"Successfully resized {filename}")
                    
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    # If a folder path is provided as command line argument, use it
    # Otherwise use default path (/tmp)
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)
You can run this script in two ways: 1. Using default folder (/tmp):
1
python code.py
2. Specifying a custom folder path:
1
python code.py /path/to/your/folder
The script will: 1. Look for all image files (png, jpg, jpeg, bmp, gif, tiff) in the specified folder 2. Resize each image to 32x32 pixels using the Lanczos resampling algorithm 3. Overwrite the original files with their resized versions 4. Print a success message for each processed image 5. Handle errors and print error messages if something goes wrong Make sure you have Pillow installed before running the script:
1
pip install Pillow
Note: Be careful when running this script as it will overwrite the original images. Make sure to backup your images if needed before running the script.

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    try:
        # Iterate through all files in the folder
        for filename in os.listdir(folder_path):
            if filename.lower().endswith(image_extensions):
                file_path = os.path.join(folder_path, filename)
                
                # Open the image
                with Image.open(file_path) as img:
                    # Convert image to RGB if it's in RGBA
                    if img.mode == 'RGBA':
                        img = img.convert('RGB')
                    
                    # Resize the image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save and overwrite the original image
                    resized_img.save(file_path)
                    print(f"Successfully resized {filename}")
                    
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    # If a folder path is provided as command line argument, use it
    # Otherwise use default path (/tmp)
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    try:
        # Iterate through all files in the folder
        for filename in os.listdir(folder_path):
            if filename.lower().endswith(image_extensions):
                file_path = os.path.join(folder_path, filename)
                
                # Open the image
                with Image.open(file_path) as img:
                    # Convert image to RGB if it's in RGBA
                    if img.mode == 'RGBA':
                        img = img.convert('RGB')
                    
                    # Resize the image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save and overwrite the original image
                    resized_img.save(file_path)
                    print(f"Successfully resized {filename}")
                    
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    # If a folder path is provided as command line argument, use it
    # Otherwise use default path (/tmp)
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)
And got the output: Successfully resized random_image_9.jpg Successfully resized random_image_1.jpg Successfully resized random_image_2.jpg Successfully resized random_image_5.jpeg Successfully resized random_image_7.jpg Successfully resized random_image_4.jpg Successfully resized random_image_3.jpeg Successfully resized random_image_8.jpeg Successfully resized random_image_10.jpeg Successfully 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 does what you described. Save it as `code.py`:

 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
43
44
45
46
47
48
from PIL import Image
import os
import sys

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    # Check if folder exists
    if not os.path.exists(folder_path):
        print(f"Error: Folder '{folder_path}' does not exist")
        return

    # Counter for processed images
    processed = 0

    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Full path to the image
                image_path = os.path.join(folder_path, filename)
                
                # Open and resize image
                with Image.open(image_path) as img:
                    # Convert to RGB if necessary
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save the resized image (overwrite original)
                    resized_img.save(image_path)
                    
                    processed += 1
                    print(f"Resized: {filename}")
                    
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")
    
    print(f"\nCompleted! Resized {processed} images.")

if __name__ == "__main__":
    # If a folder path is provided as command line argument, use it
    # Otherwise use default path (/tmp)
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)
To use this script: 1. Save it as `code.py` 2. Run it in one of these ways: - To use default folder (/tmp): `python code.py` - To specify a different folder: `python code.py /path/to/your/folder` The script will: - Look for images with extensions .png, .jpg, .jpeg, .bmp, .gif, or .tiff - Resize each image to 32x32 pixels using Lanczos resampling - Overwrite the original files with the resized versions - Print progress messages and a final count of processed images Make sure you have Pillow installed:
1
pip install Pillow
Note: This script will permanently modify the original images. Make sure to backup your images if needed before running the script.

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
43
44
45
46
47
48
from PIL import Image
import os
import sys

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    # Check if folder exists
    if not os.path.exists(folder_path):
        print(f"Error: Folder '{folder_path}' does not exist")
        return

    # Counter for processed images
    processed = 0

    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Full path to the image
                image_path = os.path.join(folder_path, filename)
                
                # Open and resize image
                with Image.open(image_path) as img:
                    # Convert to RGB if necessary
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save the resized image (overwrite original)
                    resized_img.save(image_path)
                    
                    processed += 1
                    print(f"Resized: {filename}")
                    
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")
    
    print(f"\nCompleted! Resized {processed} images.")

if __name__ == "__main__":
    # If a folder path is provided as command line argument, use it
    # Otherwise use default path (/tmp)
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)

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
43
44
45
46
47
48
from PIL import Image
import os
import sys

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    # Check if folder exists
    if not os.path.exists(folder_path):
        print(f"Error: Folder '{folder_path}' does not exist")
        return

    # Counter for processed images
    processed = 0

    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Full path to the image
                image_path = os.path.join(folder_path, filename)
                
                # Open and resize image
                with Image.open(image_path) as img:
                    # Convert to RGB if necessary
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save the resized image (overwrite original)
                    resized_img.save(image_path)
                    
                    processed += 1
                    print(f"Resized: {filename}")
                    
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")
    
    print(f"\nCompleted! Resized {processed} images.")

if __name__ == "__main__":
    # If a folder path is provided as command line argument, use it
    # Otherwise use default path (/tmp)
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)
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_4.jpg Resized: random_image_2.jpeg Resized: random_image_3.jpg Resized: random_image_8.jpeg Resized: random_image_10.jpeg Resized: random_image_6.jpeg Completed! Resized 10 images.

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 does what you described. Save it as `code.py`:

 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
43
44
from PIL import Image
import os
import sys

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp')
    
    # Check if folder exists
    if not os.path.exists(folder_path):
        print(f"Error: Folder {folder_path} does not exist")
        return

    # Counter for processed images
    processed = 0

    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Open image
                image_path = os.path.join(folder_path, filename)
                with Image.open(image_path) as img:
                    # Convert image to RGB if it's in RGBA
                    if img.mode == 'RGBA':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save image (overwrite)
                    resized_img.save(image_path)
                    processed += 1
                    print(f"Processed: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")

    print(f"\nTotal images processed: {processed}")

if __name__ == "__main__":
    # Get folder path from command line argument if provided, otherwise use default
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)
To use this script: 1. Save it as `code.py` 2. Run it in one of these ways: - `python code.py` (will use default /tmp folder) - `python code.py /path/to/your/folder` (will use the specified folder) The script will: - Look for image files with extensions .png, .jpg, .jpeg, .gif, and .bmp - Resize each image to 32x32 pixels using Lanczos resampling - Overwrite the original files with the resized versions - Print progress and any errors that occur - Show the total number of images processed at the end Make sure you have Pillow installed. If not, install it using:
1
pip install Pillow
Note: This script will permanently overwrite the original images, so make sure to backup your images if needed before running the script.

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
43
44
from PIL import Image
import os
import sys

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp')
    
    # Check if folder exists
    if not os.path.exists(folder_path):
        print(f"Error: Folder {folder_path} does not exist")
        return

    # Counter for processed images
    processed = 0

    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Open image
                image_path = os.path.join(folder_path, filename)
                with Image.open(image_path) as img:
                    # Convert image to RGB if it's in RGBA
                    if img.mode == 'RGBA':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save image (overwrite)
                    resized_img.save(image_path)
                    processed += 1
                    print(f"Processed: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")

    print(f"\nTotal images processed: {processed}")

if __name__ == "__main__":
    # Get folder path from command line argument if provided, otherwise use default
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)

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
43
44
from PIL import Image
import os
import sys

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp')
    
    # Check if folder exists
    if not os.path.exists(folder_path):
        print(f"Error: Folder {folder_path} does not exist")
        return

    # Counter for processed images
    processed = 0

    # Iterate through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Open image
                image_path = os.path.join(folder_path, filename)
                with Image.open(image_path) as img:
                    # Convert image to RGB if it's in RGBA
                    if img.mode == 'RGBA':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save image (overwrite)
                    resized_img.save(image_path)
                    processed += 1
                    print(f"Processed: {filename}")
            
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")

    print(f"\nTotal images processed: {processed}")

if __name__ == "__main__":
    # Get folder path from command line argument if provided, otherwise use default
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    resize_image(folder_path)
And got the output: Processed: random_image_5.jpg Processed: random_image_1.jpeg Processed: random_image_8.jpg Processed: random_image_6.jpg Processed: random_image_2.jpg Processed: random_image_7.jpg Processed: random_image_4.jpeg Processed: random_image_9.jpeg Processed: random_image_10.jpg Processed: random_image_3.jpg 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 complete Python script that does what you described:

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    # Walk through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Construct full file path
                file_path = os.path.join(folder_path, filename)
                
                # Open image
                with Image.open(file_path) as img:
                    # Convert image to RGB if it's not
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save and overwrite original image
                    resized_img.save(file_path)
                    
                print(f"Successfully resized {filename}")
                
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")

if __name__ == "__main__":
    resize_image()
Save this as `code.py` and run it with:
1
python code.py
This script will: 1. Look for image files in the `/tmp` folder by default 2. Resize any found images to 32x32 pixels 3. Overwrite the original files with their resized versions 4. Print success or error messages for each file To use a different folder, you can modify the code to pass a different path:
1
resize_image("/path/to/your/folder")
Make sure you have Pillow installed:
1
pip install Pillow
Note: This script will permanently overwrite the original images, so make sure to back up your images if needed before running the script.

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    # Walk through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Construct full file path
                file_path = os.path.join(folder_path, filename)
                
                # Open image
                with Image.open(file_path) as img:
                    # Convert image to RGB if it's not
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save and overwrite original image
                    resized_img.save(file_path)
                    
                print(f"Successfully resized {filename}")
                
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
    
    # Walk through all files in the folder
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(image_extensions):
            try:
                # Construct full file path
                file_path = os.path.join(folder_path, filename)
                
                # Open image
                with Image.open(file_path) as img:
                    # Convert image to RGB if it's not
                    if img.mode != 'RGB':
                        img = img.convert('RGB')
                    
                    # Resize image
                    resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                    
                    # Save and overwrite original image
                    resized_img.save(file_path)
                    
                print(f"Successfully resized {filename}")
                
            except Exception as e:
                print(f"Error processing {filename}: {str(e)}")

if __name__ == "__main__":
    resize_image()
And got the output: Successfully resized random_image_7.jpeg Successfully resized random_image_9.jpg Successfully resized random_image_8.jpg Successfully resized random_image_6.jpg Successfully resized random_image_1.jpg Successfully resized random_image_2.jpg Successfully resized random_image_5.jpeg Successfully resized random_image_4.jpeg Successfully resized random_image_3.jpg Successfully resized random_image_10.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 does what you described. Save it as `resize_image.py`:

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif')
    
    # Walk through the directory
    for root, dirs, files in os.walk(folder_path):
        for filename in files:
            if filename.lower().endswith(image_extensions):
                filepath = os.path.join(root, filename)
                try:
                    # Open the image
                    with Image.open(filepath) as img:
                        # Convert image to RGB if it's not
                        if img.mode != 'RGB':
                            img = img.convert('RGB')
                        
                        # Resize image to 32x32
                        resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                        
                        # Save and overwrite the original image
                        resized_img.save(filepath, quality=95)
                        print(f"Successfully resized: {filepath}")
                        
                except Exception as e:
                    print(f"Error processing {filepath}: {str(e)}")

if __name__ == "__main__":
    import sys
    
    # Use command line argument if provided, otherwise use default
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    
    resize_image(folder_path)
You can run this script in two ways: 1. Using default folder (/tmp):
1
python resize_image.py
2. Specifying a custom folder:
1
python resize_image.py /path/to/your/folder
This script will: 1. Look for image files with extensions .png, .jpg, .jpeg, .bmp, and .gif 2. Resize them to 32x32 pixels 3. Overwrite the original files with the resized versions 4. Print success messages or error messages if something goes wrong Make sure you have Pillow installed. If not, you can install it with:
1
pip install Pillow
Note: Always make sure you have backups of your images before running this script, as it overwrites the original files!

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif')
    
    # Walk through the directory
    for root, dirs, files in os.walk(folder_path):
        for filename in files:
            if filename.lower().endswith(image_extensions):
                filepath = os.path.join(root, filename)
                try:
                    # Open the image
                    with Image.open(filepath) as img:
                        # Convert image to RGB if it's not
                        if img.mode != 'RGB':
                            img = img.convert('RGB')
                        
                        # Resize image to 32x32
                        resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                        
                        # Save and overwrite the original image
                        resized_img.save(filepath, quality=95)
                        print(f"Successfully resized: {filepath}")
                        
                except Exception as e:
                    print(f"Error processing {filepath}: {str(e)}")

if __name__ == "__main__":
    import sys
    
    # Use command line argument if provided, otherwise use default
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    
    resize_image(folder_path)

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

def resize_image(folder_path="/tmp"):
    # Supported image extensions
    image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif')
    
    # Walk through the directory
    for root, dirs, files in os.walk(folder_path):
        for filename in files:
            if filename.lower().endswith(image_extensions):
                filepath = os.path.join(root, filename)
                try:
                    # Open the image
                    with Image.open(filepath) as img:
                        # Convert image to RGB if it's not
                        if img.mode != 'RGB':
                            img = img.convert('RGB')
                        
                        # Resize image to 32x32
                        resized_img = img.resize((32, 32), Image.Resampling.LANCZOS)
                        
                        # Save and overwrite the original image
                        resized_img.save(filepath, quality=95)
                        print(f"Successfully resized: {filepath}")
                        
                except Exception as e:
                    print(f"Error processing {filepath}: {str(e)}")

if __name__ == "__main__":
    import sys
    
    # Use command line argument if provided, otherwise use default
    folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp"
    
    resize_image(folder_path)
And got the output: Successfully resized: /tmp/random_image_5.jpg Successfully resized: /tmp/random_image_9.jpg Successfully resized: /tmp/random_image_6.jpg Successfully resized: /tmp/random_image_1.jpg Successfully resized: /tmp/random_image_2.jpg Successfully resized: /tmp/random_image_7.jpg Successfully resized: /tmp/random_image_4.jpeg Successfully resized: /tmp/random_image_3.jpeg Successfully resized: /tmp/random_image_8.jpeg Successfully resized: /tmp/random_image_10.jpeg

PyFunc

final: True Resulting in output: True