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

相关文章

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效