Supercharging Python Development with ChatGPT.

Supercharging Python Development with ChatGPT.


In the rapidly evolving world of software development, the pursuit of tools and resources that enhance productivity and streamline workflows is constant. Can you believe that there may come a time when AI is capable of writing code? It’s hard to imagine! But ChatGPT is capable to write a code in programming language you will ask.

As a creator, I have always sought innovative ways to optimize my coding process and tap into the power of artificial intelligence. One such groundbreaking tool that has transformed the way I approach Python development is ChatGPT.

How ChatGPT is helping me to write a code?

First of all, I am not a full-time developer, but I engage in various activities, including running a blog, where resource (time) management is crucial.

Recently, I encountered a task that required renaming a large number of images on my disk to follow a specific pattern: “image” + image number + “.jpg”.

Traditionally, I would have approached this task manually, involving a series of clicks, renames, and copy-pasting. Alternatively, I might have utilized Excel macros on occasions.

However, this time, I opted to leverage Python for this particular use case.

ChatGPT, please give me the code 🙂

I did ask from ChatGPT:

I have the list of images in folder and I want to rename them to “image” with adding number of image

Chat GPT did answer (even with warning at the end):

To rename the images in a folder with a specific naming format, such as “image1.jpg”, “image2.jpg”, and so on, you can use the following code:

import os

# Path to the folder containing the images
image_folder = 'C:/images/'

# Retrieve the list of files in the folder
file_list = os.listdir(image_folder)

# Sort the files in alphanumeric order
file_list.sort()

# Iterate over the files and rename them
for i, file_name in enumerate(file_list):
    if file_name.endswith('.jpg'):
        new_name = f'image{i+1}.jpg'
        old_path = os.path.join(image_folder, file_name)
        new_path = os.path.join(image_folder, new_name)
        os.rename(old_path, new_path)
        print(f'Renamed: {file_name} to {new_name}')
 
print('Image renaming complete!')

In this code, image_folder should be set to the path of the folder containing the images. The code retrieves the list of files in the folder, sorts them in alphanumeric order, and then iterates over the files. For each file that has a “.jpg” extension, it generates a new name using the desired naming format (“image1.jpg”, “image2.jpg”, etc.) and renames the file accordingly using the os.rename() function.

Please make sure to back up your image files before running this code to avoid any accidental data loss.

Have a code working

Created new Python file in my Visual Studio Code, pasted the code from ChatGPT response.

Modified the variable to have the real one with my images:

image_folder = 'C:\myprojects\testproject\images'

Also did a backup of folder with my images.

Executed the script.

Yep, it went all good, all my images were renamed to image1.jpg /// image75.jpg

Some improvements by me

What if I add more images to the folder where the images are already renamed?

In Python, when sorting the list of images, there may be cases where the list contains mix of renamed and new images. A list may looks like “aaa.jpg”, “image1.jpg”, “zz.jpg”.

In such cases, it would be necessary to rename the entire list starting from the very first image. To address this situation, I have implemented by introducing an additional subroutine.

import os

# Path to the folder containing the images
image_folder = 'C:\myprojects\testproject\images'

# Retrieve the list of files in the folder
file_list = os.listdir(image_folder)

# Sort the files in alphanumeric order
file_list.sort()

#rename by skipping imagexx files to zzz in order to put on list end

for i, file_name in enumerate(file_list):
    if file_name.endswith('.jpg'):
        if (not file_name.startswith('image')) &  (not file_name.startswith('zz')) :
            new_name = f'zz{i+1}.jpg'
            old_path = os.path.join(image_folder, file_name)
            new_path = os.path.join(image_folder, new_name)
            os.rename(old_path, new_path)
            print(f'Renamed: {file_name} to {new_name}')

#retrieve full list of images again
file_list = os.listdir(image_folder)

#Sort the list again and have zzzx.jpg at the end of the list
file_list.sort()

# Iterate over the files and rename only those starting with zz
for i, file_name in enumerate(file_list):
    if file_name.endswith('.jpg'):
        if  file_name.startswith('zz') :
            new_name = f'image{i+1}.jpg'
            old_path = os.path.join(image_folder, file_name)
            new_path = os.path.join(image_folder, new_name)
            os.rename(old_path, new_path)
            print(f'Renamed: {file_name} to {new_name}')

print('Image renaming complete!')

When ChatGPT assists me in idea generation during the design thinking process, it also provides help with coding.