C# OpenCvSharp 图片批量改名

目录

效果

项目

代码


C# OpenCvSharp 图片批量改名

效果

项目

代码

using NLog;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);
        }

        private static Logger _log = NLog.LogManager.GetCurrentClassLogger();

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        string inPath = "";
        string outPath = "";
        DirectoryInfo folder;
        List<FileInfo> files=new List<FileInfo>();
        String[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
        
        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            inPath = "";
            outPath = "";
            files.Clear();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                inPath = dialog.SelectedPath;
                textBox1.Text = inPath;
                outPath = inPath + "\\out";
                textBox2.Text = outPath;

                _log.Info("图片路径:" + inPath);
                _log.Info("保存路径:" + outPath);

                folder = new DirectoryInfo(inPath);
                var temp = folder.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo file in temp)
                {
                    if (imageExtensions.Contains(file.Extension.ToLower()))
                    {
                        files.Add(file);
                    }
                }
                _log.Info("一共["+ files .Count()+ "]张图片");
            }

        }

        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (files.Count()==0)
            {
                return;
            }
            outPath = textBox2.Text;
            //目录不存在 则创建
            if (!Directory.Exists(outPath))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(outPath);
                //创建目录
                directoryInfo.Create();
            }
            else {

                DirectoryInfo outFolder=new DirectoryInfo(outPath);
                if (outFolder.GetFiles("*.*", SearchOption.AllDirectories).Length>0)
                {
                    MessageBox.Show(outPath + "文件夹不为空,防止数据被覆盖,请更换!");
                    return;
                }
            }
          
            string oldName;
            string newName;
            Mat temp;
            int index = 0;
            foreach (FileInfo file in files)
            {
                oldName = file.Name;
                newName = index.ToString() + file.Extension;
                try
                {
                    temp = new Mat(inPath + "\\" + oldName);
                    //其他处理 ,例如
                    //图片缩放
                    //通道变换等
                    //……
                    Cv2.ImWrite(outPath + "\\" + newName, temp);
                    _log.Info(oldName + "-->" + newName);
                    index++;
                }
                catch (Exception ex)
                {
                    _log.Info(oldName+"修改异常,异常信息:"+ex.Message);
                }
            }

            _log.Info("全部修改完成!");
        }
    }
}

using NLog;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);
        }

        private static Logger _log = NLog.LogManager.GetCurrentClassLogger();

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        string inPath = "";
        string outPath = "";
        DirectoryInfo folder;
        List<FileInfo> files=new List<FileInfo>();
        String[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
        
        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            inPath = "";
            outPath = "";
            files.Clear();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                inPath = dialog.SelectedPath;
                textBox1.Text = inPath;
                outPath = inPath + "\\out";
                textBox2.Text = outPath;

                _log.Info("图片路径:" + inPath);
                _log.Info("保存路径:" + outPath);

                folder = new DirectoryInfo(inPath);
                var temp = folder.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo file in temp)
                {
                    if (imageExtensions.Contains(file.Extension.ToLower()))
                    {
                        files.Add(file);
                    }
                }
                _log.Info("一共["+ files .Count()+ "]张图片");
            }

        }

        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (files.Count()==0)
            {
                return;
            }
            outPath = textBox2.Text;
            //目录不存在 则创建
            if (!Directory.Exists(outPath))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(outPath);
                //创建目录
                directoryInfo.Create();
            }
            else {

                DirectoryInfo outFolder=new DirectoryInfo(outPath);
                if (outFolder.GetFiles("*.*", SearchOption.AllDirectories).Length>0)
                {
                    MessageBox.Show(outPath + "文件夹不为空,防止数据被覆盖,请更换!");
                    return;
                }
            }
          
            string oldName;
            string newName;
            Mat temp;
            int index = 0;
            foreach (FileInfo file in files)
            {
                oldName = file.Name;
                newName = index.ToString() + file.Extension;
                try
                {
                    temp = new Mat(inPath + "\\" + oldName);
                    //其他处理 ,例如
                    //图片缩放
                    //通道变换等
                    //……
                    Cv2.ImWrite(outPath + "\\" + newName, temp);
                    _log.Info(oldName + "-->" + newName);
                    index++;
                }
                catch (Exception ex)
                {
                    _log.Info(oldName+"修改异常,异常信息:"+ex.Message);
                }
            }

            _log.Info("全部修改完成!");
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/449514.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Pytorch_lightning先前版本的官方文档地址

https://lightning.ai/docs/pytorch/1.4.2/https://lightning.ai/docs/pytorch/1.4.2/ Pytorch_lightning更新到2后&#xff0c;和之前版本的变化较大&#xff0c;一些以前原有的api被删除了 比如&#xff1a;on_pretrain_routine_start 官方也没有在显眼的位置放置先前版本的…

【机器学习智能硬件开发全解】(一)—— 政安晨:从深度认知开始

从深刻认知开始 作者政安晨依据自身多年研发经验&#xff0c;本着成过的功与踩过的坑都是浮云的态度&#xff0c;为各位小伙伴们进行一个系列的智能硬件开发领域的示范解析&#xff0c;让对智能硬件感兴趣的小伙伴们少走弯路。 这是一路走一路全是干货的文章系列&#xff0c;…

基础的渗透测试

信息收集漏洞挖掘获取shell phpadmin文件上传与文件包含 对某一个网站的渗透 专注于web漏洞挖掘、内网渗透、免杀和代码审计&#xff0c;感谢各位师傅的关注&#xff01;网安之路漫长&#xff0c;与君共勉&#xff01; 信息收集 简单的进行一下信息收集&#xff0c;goby工具走一…

【工具】Jmeter安装入门

目录 前言 安装Jmeter 1.1 下载Jmeter 1.2 解压 1.3 运行 快速入门 2.1 设置中文语言 2.2 外观(主题)设置 2.3 基本用法 前言 1W&#xff1a;什么是 Jmeter &#xff1f; Jmeter 是一个用于进行性能测试、负载测试和功能测试的开源工具。最初为 Web 应用程序设计&#…

【已解决】git无法上传大于100MB问题

问题&#xff1a; 输入命令 git config http.postBuffer 524288000 查看命令 git config -l

C语言——指针全讲

1、指针是什么&#xff1a;数据存于内存中&#xff0c;CPU如果要对数据进行处理首先就要找到相对应的数据&#xff0c;就是通过地址来找到的。就像你所在的公寓&#xff0c;你如果要找到你的房间就需要根据门牌号寻找。 在CPU和内存之间有地址线连接&#xff0c;其中在x86环境下…

Socket通信Demo(Unity客户端和C#)

Socket通信基本流程 首先要启动服务器创建Socket&#xff0c;然后要绑定服务器的一个端口这样客户端通过服务器IP端口号就能连接到服务器了服务器接下来会设置监听队列&#xff0c;监听并等待要连接到它的客户端客户端在服务器启动之后也建立自己的Socket&#xff0c;然后使用…

2 月 Web3 游戏行业动态

作者&#xff1a;stellafootprint.network 数据来源&#xff1a;区块链游戏研究页面 - Footprint Analytics 2024 年 2 月&#xff0c;区块链游戏领域在加密货币价格上涨和活跃用户激增的推动下&#xff0c;实现了显著增长。然而&#xff0c;行业在维持用户参与度和留存率方面…

鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:NavRouter)

导航组件&#xff0c;默认提供点击响应处理&#xff0c;不需要开发者自定义点击事件逻辑。 说明&#xff1a; 该组件从API Version 9开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 必须包含两个子组件&#xff0c;其中第二个子组…

搭建交换机模拟环境及SSH连接,华为NSP软件入门使用教程

搭建交换机模拟环境及SSH连接&#xff0c;华为NSP软件入门使用教程 如果你是通过搜索搜到了这篇文章&#xff0c;那么一定是工作或者学习中需要用交换机&#xff0c;但是又没物理机测试学习&#xff0c;所以需要搭建本地的虚拟环境学习。 这篇文章是我进行交换机命令入门学习写…

在 windows 下安装并调试 CMake

一、前言 CMake是一个跨平台的开源工具&#xff0c;用于管理软件项目的构建过程。它不直接构建软件&#xff0c;而是生成用于特定平台或编译器的构建文件&#xff08;如Makefile或Visual Studio项目文件&#xff09;&#xff0c;然后利用这些文件来实际构建软件。 二、初次尝…

026—pandas 根据文本数据提取特征

前言 在数据处理中&#xff0c;源数据可能具有一定便于记录但又复杂的结构&#xff0c;我们在后续使用数据时还需要进一步进行处理。在本例中&#xff0c;要根据一列数据提取出数据中的相关特征&#xff0c;我们来看看 pandas 是如何完成的。 需求&#xff1a; 以上数据的 a …

【NR 定位】3GPP NR Positioning 5G定位标准解读(七)- GNSS定位方法

前言 3GPP NR Positioning 5G定位标准&#xff1a;3GPP TS 38.305 V18 3GPP 标准网址&#xff1a;Directory Listing /ftp/ 【NR 定位】3GPP NR Positioning 5G定位标准解读&#xff08;一&#xff09;-CSDN博客 【NR 定位】3GPP NR Positioning 5G定位标准解读&#xff08;…

安全信息化管理平台——数据分析与可视化

在当今的信息化时代&#xff0c;数据分析与可视化已经成为各个领域中不可或缺的组成部分。对于企业而言&#xff0c;如何将安全信息进行整合、分析并直观地呈现出来&#xff0c;成为了一项至关重要的任务。这就催生了一种新型的管理平台——安全信息化管理平台。 数据分析&…

【恒源智享云】conda虚拟环境的操作指令

conda虚拟环境的操作指令 由于虚拟环境经常会用到&#xff0c;但是我总忘记&#xff0c;所以写个博客&#xff0c;留作自用。 在恒源智享云上&#xff0c;可以直接在终端界面输入指令&#xff0c;例如&#xff1a; 查看已经存在的虚拟环境列表 conda env list查看当前虚拟…

【C++精简版回顾】21.迭代器,实现迭代器

1.什么是迭代器&#xff1f; 用来遍历容器&#xff0c;访问容器数据。 2.迭代器使用 1.初始化 //初始化 list<int> mylist;//list的整数对象 list<int>::iterator iter;//list内部类&#xff0c;迭代器对象(正向输出) list<int>::reverse_iterator riter;//…

华为配置ISP选路实现报文按运营商转发

Web举例&#xff1a;配置ISP选路实现报文按运营商转发 介绍通过配置ISP选路实现报文按运营商转发的配置举例。 组网需求 如图1所示&#xff0c;FW作为安全网关部署在网络出口&#xff0c;企业分别从ISP1和ISP2租用一条链路。 企业希望访问Server 1的报文从ISP1链路转发&#…

把 Windows 装进 Docker 容器里

本篇文章聊聊如何在 Docker 里运行 Windows 操作系统&#xff0c; Windows in Docker Container&#xff08;WinD&#xff09;。 写在前面 我日常使用 macOS 和 Ubuntu 来学习和工作&#xff0c;但是时不时会有 Windows 使用的场景&#xff0c;不论是运行某个指定的软件&…

记录一下某外资的面试

文章目录 标题English introduction标题What did u do in this gap time标题What’S the big challenge in your work experience标题 4、介绍一个自己熟悉的项目或最近的项目&#xff0c;包括项目的背景&#xff0c;使用的技术&#xff0c;在里面的角色标题5、项目中有多少个微…

Java中 常见的开源图库介绍

阅读本文之前请参阅------Java中 图的基础知识介绍 在 Java 中&#xff0c;有几种流行的开源图库&#xff0c;它们提供了丰富的图算法和高级操作&#xff0c;可以帮助开发者更高效地处理图相关的问题。以下是几种常见的 Java 图库及其特点和区别&#xff1a; JGraphT …
最新文章