How to use ChatGPT to make work easier

Due to work requirements, I need to process a large number of images into a uniform standard. The extra pixels can be white backgrounds only. Photoshop's Camera Raw can only reduce images in batches, and although there are online tools, they are charged. Suddenly, I asked ChatGPT, and when I first asked, I didn't want to use Python because I'm not familiar with this type of code language. However, I later found that Python can perform this type of simple yet complex and tedious task. Therefore, I tried to ask step by step, and after asking, I found that I wanted to add more questions, so I asked again, and the following code was obtained.


from PIL import Image
import os

def process_images(folder):
    total_images = 0
    processed_images = 0
    for root, dirs, files in os.walk(folder):
        for filename in files:
            filepath = os.path.join(root, filename)
            if is_image_file(filepath):
                total_images += 1

    for root, dirs, files in os.walk(folder):
        for filename in files:
            filepath = os.path.join(root, filename)
            if is_image_file(filepath):
                image = Image.open(filepath)
                resized_image = resize_image(image)

		# 限定正方圖為2250x2250
                canvas = Image.new("RGB", (2250, 2250), (255, 255, 255))
                paste_x = (2250 - resized_image.width) // 2
                paste_y = (2250 - resized_image.height) // 2
                canvas.paste(resized_image, (paste_x, paste_y))
                canvas.save(filepath, "JPEG")

		# 圖像處理進度...

                processed_images += 1
                print(f"處理進度:{processed_images}/{total_images}")


def resize_image(image):
    width, height = image.size
    max_dimension = max(width, height)

    if max_dimension <= 2250:
        return image
    
    if width > height:
        new_width = 2250
        ratio = new_width / width
        new_height = int(height * ratio)
    else:
        new_height = 2250
        ratio = new_height / height
        new_width = int(width * ratio)

    return image.resize((new_width, new_height))

def is_image_file(filename):
    supported_extensions = [".jpg", ".jpeg", ".png", ".gif"]
    file_extension = os.path.splitext(filename)[1].lower()
    
    return file_extension in supported_extensions

# 指定代文件所在位置的文件夾裡(可以是代码文件位置的文件夹或任何其他文件夹)
folder = os.path.dirname(os.path.abspath(__file__))

# 調用函數誰行圖像處理
process_images(folder)

The following is the process of asking ChatGPT

如想一鍵執行,可以複制下段代碼,保存為bat文件就可 (此文件名可以任改, 但Python文件必須改為autosize.py對應下方碼裡名字.

@echo off
python autosize.py
pause

Leave a Reply

3
AI Assistant
WhatsApp
Email
AI Assistant
WhatsApp
Email
AI Assistant
Important notice: AI responses may be slower than expected. AI provides information for reference only, and we are not responsible for its accuracy or completeness. AI responses do not represent the position of this company.

Hi there! I'm the AI assistant for . What would you like to know?

02:10