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提高效率,提升自我。

相关推荐
Slow菜鸟1 小时前
AI学习篇(三) | AI效率工具指南(2026年)
人工智能·学习
北京软秦科技有限公司2 小时前
AI审核如何助力合规取证?IACheck打造环境检测报告电子存证与法律风险防控新路径
大数据·人工智能
qq_359716232 小时前
openpi使用过程中相关问题
人工智能·深度学习·机器学习
minhuan3 小时前
医疗AI智能体:从数据到关怀人文设计:告别冰冷精准,构建有温度的诊疗交互.131
人工智能·ai智能体·智能体的人文设计·医疗ai人文设计·构建医疗ai智能体
Promise微笑3 小时前
驾驭AI引用:Geo优化中的内容评分机制与实战策略深度解析
人工智能
ai生成式引擎优化技术4 小时前
全球唯一四元结构底层架构问世:TSPR-WEB-LLM-HIC v2.0 终结大模型投毒与幻觉的终极技术范式
人工智能
听你说324 小时前
伊萨推出 ROBBI 360 协作机器人焊接工作站 简化自动化焊接部署流程
人工智能·机器人·自动化
weixin_408099674 小时前
【实战对比】在线 OCR 识别 vs OCR API 接口:从个人工具到系统集成该怎么选?
图像处理·人工智能·后端·ocr·api·图片文字识别·文字识别ocr
罗西的思考4 小时前
【OpenClaw】通过Nanobot源码学习架构---(2)外层控制逻辑
人工智能·机器学习