ChatGPT编程—实现小工具软件(批量替换文本、批量处理图像文件)

ChatGPT编程---实现小工具软件(批量替换文本、批量处理图像文件)

今天借助[小蜜蜂AI][https://zglg.work]网站的ChatGPT编程实现一个功能:批量处理文件及其内容,例如批量替换文本、批量处理图像文件等。

环境:Pycharm 2021

系统:Mac OS

向ChatGPT输入如下内容:

ChatGPT收到后返回如下结果:

批量处理文件及其内容:

1. 批量替换文本文件内容
  • 使用Python的fileinput模块结合正则表达式,可以轻松批量替换文本文件中的内容。
python 复制代码
import fileinput
import re

# 批量替换文本文件中的指定内容
def batch_replace_text(file_pattern, search_pattern, replace_text):
    for line in fileinput.input(files=glob.glob(file_pattern), inplace=True):
        print(re.sub(search_pattern, replace_text, line), end='')

# 调用示例:将所有.txt文件中的"old_text"替换为"new_text"
batch_replace_text("*.txt", "old_text", "new_text")

根据ChatGPT返回的结果,进行如下修改并运行,如下所示:

python 复制代码
import fileinput
import re
import glob


# 批量替换文本文件中的指定内容
def batch_replace_text(file_pattern, search_pattern, replace_text):
    for line in fileinput.input(files=glob.glob(file_pattern), inplace=True):
        print(re.sub(search_pattern, replace_text, line), end='')


# 调用示例:将所有.txt文件中的"old_text"替换为"new_text"
batch_replace_text("*.txt", "AI", "人工只能(AI,Artificial Intelligence)")
2. 批量处理图像文件
  • 使用Pillow库(PIL Fork)来批量处理图像文件,例如调整大小、转换格式等操作。
python 复制代码
from PIL import Image
import os

# 批量调整图像大小
def resize_images(input_dir, output_dir, size=(128, 128)):
    os.makedirs(output_dir, exist_ok=True)
    for filename in os.listdir(input_dir):
        if filename.endswith(('.png', '.jpg', '.jpeg')):
            img = Image.open(os.path.join(input_dir, filename))
            img_resized = img.resize(size)
            img_resized.save(os.path.join(output_dir, filename))

# 调用示例:将input_images目录中的所有图片调整为128x128尺寸并保存到output_images目录
resize_images("input_images", "output_images")

根据ChatGPT返回的结果,进行如下修改并运行,如下所示:

python 复制代码
from PIL import Image
import os


# 批量调整图像大小
height = input('请输入图片高度:')
width = input('请输入图片宽度:')


def resize_images(input_dir, output_dir, size=(int(height), int(width))):
    os.makedirs(output_dir, exist_ok=True)
    for filename in os.listdir(input_dir):
        if filename.endswith(('.png', '.jpg', '.jpeg')):
            img = Image.open(os.path.join(input_dir, filename))
            img_resized = img.resize(size)
            img_resized.save(os.path.join(output_dir, filename))


# 调用示例:将input_images目录中的所有图片调整为height*width尺寸并保存到output_images目录
resize_images("input_images", "output_images")

使用上述代码示例,您可以轻松地批量处理文本文件内容或图像文件。根据具体需求可进一步定制功能,并确保在处理大量文件时进行适当优化以提高效率。

还是那句话,AI编程太高效了。用好AI提高效率,提升自我。

相关推荐
苦瓜汤补钙4 分钟前
论文阅读——Segment Anything(Meta AI)——SAM
论文阅读·图像处理·人工智能·nlp·ai编程
共绩算力8 分钟前
混元图像3.0开源原生多模态生图新篇章
人工智能·ai作画·共绩算力
搞科研的小刘选手43 分钟前
【经济方向专题会议】第二届经济数据分析与人工智能国际学术会议 (EDAI 2025)
人工智能·机器学习·网络安全·大数据分析·经济·经济数据分析·绿色经济
六边形架构1 小时前
真相!Dify和n8n这两款LLM应用开发平台的最大区别,90%的人都不知道!
人工智能
敲代码的猴先生1 小时前
技术分享 | torch.profiler:利用探针收集模型执行信息的性能分析工具
人工智能·pytorch·经验分享·语言模型·性能优化
song150265372981 小时前
AI视觉检测设备 精准瑕疵尺寸外观检测机
人工智能·计算机视觉·视觉检测
song150265372981 小时前
橡胶塑胶件AI视觉检测 光学筛选机
人工智能·计算机视觉·视觉检测
happyprince1 小时前
2025年10月31日全球AI前沿动态
人工智能
深度学习lover1 小时前
<数据集>yolo螺丝螺母识别数据集<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·螺丝螺母识别