admin管理员组

文章数量:1630195

前言:之前看节目截了很多漂亮的图,使用前需要进行适当处理,比如裁剪,去除水印等。大量图片处理还是需要自动化,代码不熟悉,就用CharGPT哈哈哈哈

1. 图片处理前 VS 图片处理后

2.请ChatGPT生成批量裁剪图片的python代码

① 安装pillow,PIL

② 修改crop_height的值为实际图片需要裁剪的高度和图片输入输出路径

③ 运行代码

3.附GPT根据以上需求生成的代码

import os
from PIL import Image

def crop_top_bottom(image, crop_height):
    """Crop the top and bottom regions of an image by a specified number of pixels."""
    width, height = image.size
    if height <= 2 * crop_height:
        raise ValueError("The image height is too small to be cropped by this amount.")
    cropped_image = image.crop((0, crop_height, width, height - crop_height))
    return cropped_image

def process_images_in_folder(folder_path, output_folder_path, crop_height):
    """Process all PNG images in a folder to crop the top and bottom regions."""
    if not os.path.exists(output_folder_path):
        os.makedirs(output_folder_path)

    for filename in os.listdir(folder_path):
        if filename.lower().endswith('.png'):
            image_path = os.path.join(folder_path, filename)
            image = Image.open(image_path)
            cropped_image = crop_top_bottom(image, crop_height)
            output_path = os.path.join(output_folder_path, filename)
            cropped_image.save(output_path)
            print(f"Cropped and saved image: {output_path}")

input_folder = 'D:\\14图片蔬菜&视频熟菜Midjourney\\图片蔬菜\\like'
output_folder = 'D:\\14图片蔬菜&视频熟菜Midjourney\\图片蔬菜'
crop_height = 202  # Number of pixels to crop from top and bottom
process_images_in_folder(input_folder, output_folder, crop_height)

本文标签: 批量代码图片chatGPT