C# OpenCvSharp實(shí)現(xiàn)圖片批量改名
更新時(shí)間:2024年03月11日 17:05:16 作者:天天代碼碼天天
這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合OpenCvSharp實(shí)現(xiàn)圖片批量改名功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
效果




項(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#發(fā)送和接收HTTP請(qǐng)求類HttpWebRequest的用法
這篇文章主要給大家介紹了關(guān)于C#發(fā)送和接收HTTP請(qǐng)求類HttpWebRequest用法的相關(guān)資料,C#中的HttpWebRequest是一個(gè)用于發(fā)送HTTP請(qǐng)求的類,它可以用于向Web服務(wù)器發(fā)送GET、POST、PUT、DELETE等請(qǐng)求,需要的朋友可以參考下2024-06-06
Unity的IPostBuildPlayerScriptDLLs實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的IPostBuildPlayerScriptDLLs實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
C#實(shí)現(xiàn)獲取運(yùn)行平臺(tái)系統(tǒng)信息的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取運(yùn)行平臺(tái)系統(tǒng)信息的方法,比較典型的C#應(yīng)用,需要的朋友可以參考下2014-07-07
WPF實(shí)現(xiàn)動(dòng)畫效果(五)之關(guān)鍵幀動(dòng)畫
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫效果之關(guān)鍵幀動(dòng)畫,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

