C# OpenCvSharp 图片批量改名

2024-03-11 16:12

本文主要是介绍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("全部修改完成!");}}
}

这篇关于C# OpenCvSharp 图片批量改名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/798336

相关文章

C# 中变量未赋值能用吗,各种类型的初始值是什么

对于一个局部变量,如果未赋值,是不能使用的 对于属性,未赋值,也能使用有系统默认值,默认值如下: 对于 int 类型,默认值是 0;对于 int? 类型,默认值是 null;对于 bool 类型,默认值是 false;对于 bool? 类型,默认值是 null;对于 string 类型,默认值是 null;对于 string? 类型,哈哈,没有这种写法,会出错;对于 DateTime 类型,默

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

C#中,decimal类型使用

在Microsoft SQL Server中numeric类型,在C#中使用的时候,需要用decimal类型与其对应,不能使用int等类型。 SQL:numeric C#:decimal

在服务器上浏览图片

@StarSky 2018-10-26 15:09 字数 15971 阅读 28 https://www.zybuluo.com/StarSky/note/1294871 来源 2018-09-27 线上服务器安装 imgcat Tool   2018-09-27 线上服务器安装 imgcat 0. 准备文件:iterm2_shell_integration.bash1. 在有权限

简鹿文件批量重命名:一款文件批量改名高手都在用的工具

作为 IT 行业的搬砖民工,互联网的数据量爆炸性增长,文件管理成为了一项日益重要的任务。"简鹿文件批量重命名"应运而生,旨在为用户提供一个高效、灵活的解决方案,以应对繁琐的文件命名、排序、创建及属性修改等挑战。 这款软件凭借其一键式操作、强大的自定义规则导入、以及全面的批量处理能力,极大地简化了文件管理流程,尤其适合处理大量文件的个人用户及企业环境,是提高工作效率、保持文件系统整洁的得力助手

el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作

<div v-for="(info, index) in zsjzqwhxqList.helicopterTourInfoList" :key="info.id" >编辑上传图片// oss返回线上地址http链接格式:<el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'http:

算法与数据结构面试宝典——回溯算法详解(C#,C++)

文章目录 1. 回溯算法的定义及应用场景2. 回溯算法的基本思想3. 递推关系式与回溯算法的建立4. 状态转移方法5. 边界条件与结束条件6. 算法的具体实现过程7. 回溯算法在C#,C++中的实际应用案例C#示例C++示例 8. 总结回溯算法的主要特点与应用价值 回溯算法是一种通过尝试各种可能的组合来找到所有解的算法。这种算法通常用于解决组合问题,如排列、组合、棋盘游

C# 命名管道中客户端访问服务器时,出现“对路径的访问被拒绝”

先还原一下我出现错误的情景:我用C#控制台写了一个命名管道服务器,然后用ASP.NET写了一个客户端访问服务器,运行之后出现了下面的错误: 原因:服务器端的访问权限不够,所以是服务器端的问题,需要增加访问权限。(网上很多都说是文件夹的权限不够,情况不同,不适用于我这种情况) 解决办法: (1)在服务器端相应地方添加以下代码。 PipeSecurity pse = new PipeSec

如何通过示例将旧版 C# 转换为 C# 12

随着 C# 的不断发展,每个新版本都会引入强大的新功能,从而提高语言的功能和可读性。通过从旧版本的 C# 迁移到 C# 12,您可以获得更高效、更易于维护和更具表现力的代码。 由于代码库遗留、公司限制以及对旧语言功能的熟悉,许多开发人员仍在使用旧版本的 C#。升级似乎很困难,但现代版本的 C# 具有显著的优势,例如更好的性能、增强的功能和更高的安全性。 通过增量重构、试点项目和团队培训逐步

C# 日志框架Serilog使用

1、框架和说明        C#日志框架Serilog支持多种场景输出,简单验证了一下,比较方便        包的安装,推荐直接使用“推荐NuGet包管理器”安装Serilog.AspNetCore,常见的组件都已经集成在一个包中,使用比较方便 2、配置文件        Serilog可以由配置文件来定义行为,而且配置文件的修改即时生效。参考配置文件如下: {"Serilog":