C# OpenCvSharp實(shí)現(xiàn)圖片批量改名
效果




項(xiàng)目

代碼
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 = "請(qǐng)選擇文件路徑";
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;
//目錄不存在 則創(chuàng)建
if (!Directory.Exists(outPath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(outPath);
//創(chuàng)建目錄
directoryInfo.Create();
}
else {
DirectoryInfo outFolder=new DirectoryInfo(outPath);
if (outFolder.GetFiles("*.*", SearchOption.AllDirectories).Length>0)
{
MessageBox.Show(outPath + "文件夾不為空,防止數(shù)據(jù)被覆蓋,請(qǐng)更換!");
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("全部修改完成!");
}
}
}到此這篇關(guān)于C# OpenCvSharp實(shí)現(xiàn)圖片批量改名的文章就介紹到這了,更多相關(guān)C# OpenCvSharp圖片改名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用doggleReport生成pdf報(bào)表的方法
這篇文章主要介紹了C#使用doggleReport生成pdf報(bào)表的方法,結(jié)合實(shí)例形式分析了C# doggleReport安裝及使用具體操作技巧,需要的朋友可以參考下2017-06-06
Unity Blend Tree動(dòng)畫混合樹使用入門教程
這篇文章主要為大家詳細(xì)介紹了Unity Blend Tree動(dòng)畫混合樹使用入門教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解
這篇文章主要介紹了Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
在C#中實(shí)現(xiàn)在字符串的第x個(gè)字符位置插入字符的常用方法
這篇文章主要介紹了在C#中6種在字符串第x個(gè)位置插入字符的方法,包括String.Insert()、Substring拼接、StringBuilder、字符數(shù)組、Span<T>和LINQ,并提供了性能比較和最佳實(shí)踐,需要的朋友可以參考下2026-02-02
C#獲取機(jī)器碼的方法詳解(機(jī)器名,CPU編號(hào),硬盤編號(hào),網(wǎng)卡mac等)
這篇文章主要介紹了C#獲取機(jī)器碼的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#獲取硬件機(jī)器名、CPU編號(hào)、硬盤編號(hào)、網(wǎng)卡mac等信息的相關(guān)實(shí)現(xiàn)方法,需要的朋友可以參考下2016-07-07
C#中關(guān)于double.ToString()的用法
這篇文章主要介紹了C#中關(guān)于double.ToString()的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
C# List實(shí)現(xiàn)行轉(zhuǎn)列的通用方案
本篇通過行轉(zhuǎn)列引出了System.Linq.Dynamic,并且介紹了過濾功能,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Unity實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02

