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

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

今天借助小蜜蜂AIhttps://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 小时前
物业数字化落地思考:真正的转型,是底层数据秩序的重构
java·大数据·人工智能·微服务·系统架构
7177777 小时前
中小团队 DevOps 平台选哪家:2026 年主流平台对比与 Gitee 本土化方案解析
人工智能·gitee
武子康7 小时前
小智断网后还能做什么?沿一次唤醒看清设备与服务端的分工
人工智能·llm·agent
西安栈上月明软件科技7 小时前
从 Linux 0.01 到 AI 开源:星图邻的开源实践
人工智能·自然语言处理·架构·开源·fastapi
麻雀飞吧7 小时前
先判断工具用来学习、开发还是执行
人工智能·python
甲维斯8 小时前
ZCode:快来领“免费”3亿tokens和“Git打包服务”
人工智能
揽秀亭长8 小时前
视频转文字有哪些方法?在线AI、剪辑软件、本地对比
人工智能·音视频
RoboWizard8 小时前
三星和金士顿内存条哪个更适合游戏超频
大数据·人工智能
深圳市恒星物联科技有限公司9 小时前
轻量MCU设备通过OpenHarmony兼容性测评的全流程关键要点与实战踩坑经验
大数据·人工智能·物联网·鸿蒙
AI闲人9 小时前
企业 AI 最大的问题,不是数据不足,而是数据没有业务语义
人工智能·数字化·企业ai落地