ChatGPT生成代码批量裁剪图片

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值