My conversation with ChatGPT

Sure! Here’s a Python script that should do what you’re asking for.
import os
from PIL import Image

# Set the path to the folder containing the images
PATH = "path/to/folder"

# Get a list of all the image files in the folder
images = [f for f in os.listdir(PATH) if f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png")]

# Loop through each image file
for image in images:
    # Get the name of the image file without the extension
    name = os.path.splitext(image)[0]
    
    # Create a folder with the same name as the image file
    os.makedirs(os.path.join(PATH, name), exist_ok=True)
    
    # Open the original image file
    with Image.open(os.path.join(PATH, image)) as img:
        # Resize and save the image with a width of 60 pixels
        img.resize((60, int(60 * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_60{os.path.splitext(image)[1]}"))
        
        # Resize and save the image with a width of 120 pixels
        img.resize((120, int(120 * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_120{os.path.splitext(image)[1]}"))
        
        # Resize and save the image with a width of 550 pixels
        img.resize((550, int(550 * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_550{os.path.splitext(image)[1]}"))
        
        # Resize and save the image with a width of 600 pixels
        img.resize((600, int(600 * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_600{os.path.splitext(image)[1]}"))
        
        # Resize and save the image with a width of 800 pixels
        img.resize((800, int(800 * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_800{os.path.splitext(image)[1]}"))
You can replace "path/to/folder" with your desired path. Let me know if you have any questions!

Yes, you’re right! Here’s the updated code that uses a loop to resize the images:
import os
from PIL import Image

# Set the path to the folder containing the images
PATH = "path/to/folder"

# Get a list of all the image files in the folder
images = [f for f in os.listdir(PATH) if f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png")]

# Loop through each image file
for image in images:
    # Get the name of the image file without the extension
    name = os.path.splitext(image)[0]
    
    # Create a folder with the same name as the image file
    os.makedirs(os.path.join(PATH, name), exist_ok=True)
    
    # Open the original image file
    with Image.open(os.path.join(PATH, image)) as img:
        # Set the sizes to resize the image to
        sizes = [60, 120, 550, 600, 800]
        
        # Loop through each size and resize the image
        for size in sizes:
            img.resize((size, int(size * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_{size}{os.path.splitext(image)[1]}"))
This should be more efficient and easier to read. Let me know if you have any other questions!

What is PIL and is it available? PyCharm says: ERROR: Could not find a version that satisfies the requirement PIL (from versions: none). Are there any alternatives? Sent message. from PIL import Image - what is PIL and is it available? PyCharm says: ERROR: Could not find a version that satisfies the requirement PIL (from versions: none). Are there any alternatives?

PIL (Python Imaging Library) is a library that adds support for opening, manipulating, and saving many different image file formats. It is not available for Python 3.5+. However, there is a fork of PIL called Pillow that works with Python 3.5+ and provides many of the same features as PIL1. You can install it using pip:

pip install Pillow

Alternatively, you can use other libraries such as OpenCV or scikit-image2. Let me know if you have any other questions!

Could you update you code so that it uses Pillow?

Sure! Here’s the updated code that uses Pillow:
import os
from PIL import Image

# Set the path to the folder containing the images
PATH = "path/to/folder"

# Get a list of all the image files in the folder
images = [f for f in os.listdir(PATH) if f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png")]

# Loop through each image file
for image in images:
    # Get the name of the image file without the extension
    name = os.path.splitext(image)[0]
    
    # Create a folder with the same name as the image file
    os.makedirs(os.path.join(PATH, name), exist_ok=True)
    
    # Open the original image file
    with Image.open(os.path.join(PATH, image)) as img:
        # Set the sizes to resize the image to
        sizes = [60, 120, 550, 600, 800]
        
        # Loop through each size and resize the image
        for size in sizes:
            img.resize((size, int(size * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_{size}{os.path.splitext(image)[1]}"))
Let me know if you have any other questions!

Should I escape any slashes in path "C:/folder/folder2/folder3" so that it works in Python?

No, you don’t need to escape the slashes in the path “C:/folder/folder2/folder3”. Python can handle forward slashes in paths just fine. However, if you want to use backslashes instead of forward slashes, you will need to escape them with another backslash like this:

path = "C:\\folder\\folder2\\folder3"

Let me know if you have any other questions!

We're almost there with your code! The only problem is that it doesn't take the bigger side of the image but takes its width. Meanwhile the height is larger that width. For example, it turns a 1815x2512 image into 60x83 which is not what I expected. I expected 43x60.

I see what you mean. You can fix this by checking whether the width or height is larger and then resizing based on that. Here’s the updated code:
import os
from PIL import Image

# Set the path to the folder containing the images
PATH = "path/to/folder"

# Get a list of all the image files in the folder
images = [f for f in os.listdir(PATH) if f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png")]

# Loop through each image file
for image in images:
    # Get the name of the image file without the extension
    name = os.path.splitext(image)[0]
    
    # Create a folder with the same name as the image file
    os.makedirs(os.path.join(PATH, name), exist_ok=True)
    
    # Open the original image file
    with Image.open(os.path.join(PATH, image)) as img:
        # Set the sizes to resize the image to
        sizes = [60, 120, 550, 600, 800]
        
        # Loop through each size and resize the image
        for size in sizes:
            if img.width > img.height:
                img.resize((size, int(size * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_{size}{os.path.splitext(image)[1]}"))
            else:
                img.resize((int(size * img.width / img.height), size)).save(os.path.join(PATH, name, f"{name}_{size}{os.path.splitext(image)[1]}"))
This should work correctly now. Let me know if you have any other questions!

Will it rewrite the existing images within the subfolders it created?

No, it won’t rewrite the existing images within the subfolders it created. If an image with the same name already exists in the subfolder, it will raise a FileExistsError exception. You can catch this exception and handle it however you like. For example, you could skip resizing the image or delete the existing image before resizing the new one.

Let's make the code rewrite the images within the folders it has created if the images with the same names already exist. Also, let's make it check whether the folders it's about to create already exist or not and only create them if they don't.

Sure! Here’s the updated code that rewrites the images within the folders it has created if the images with the same names already exist and checks whether the folders it’s about to create already exist or not and only creates them if they don’t:
import os
from PIL import Image

# Set the path to the folder containing the images
PATH = "path/to/folder"

# Get a list of all the image files in the folder
images = [f for f in os.listdir(PATH) if f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png")]

# Loop through each image file
for image in images:
    # Get the name of the image file without the extension
    name = os.path.splitext(image)[0]
    
    # Create a folder with the same name as the image file if it doesn't already exist
    if not os.path.exists(os.path.join(PATH, name)):
        os.makedirs(os.path.join(PATH, name))
    
    # Open the original image file
    with Image.open(os.path.join(PATH, image)) as img:
        # Set the sizes to resize the image to
        sizes = [60, 120, 550, 600, 800]
        
        # Loop through each size and resize the image
        for size in sizes:
            if img.width > img.height:
                img.resize((size, int(size * img.height / img.width))).save(os.path.join(PATH, name, f"{name}_{size}{os.path.splitext(image)[1]}"))
            else:
                img.resize((int(size * img.width / img.height), size)).save(os.path.join(PATH, name, f"{name}_{size}{os.path.splitext(image)[1]}"))
This should work as expected now. Let me know if you have any other questions!