最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

最新評(píng)論

弥渡县| 宜黄县| 策勒县| 天门市| 沭阳县| 靖远县| 杨浦区| 来安县| 楚雄市| 屯门区| 梨树县| 山阴县| 东海县| 塘沽区| 宽甸| 新巴尔虎左旗| 易门县| 长寿区| 蕉岭县| 郎溪县| 阿瓦提县| 商南县| 新津县| 玉溪市| 南平市| 巴楚县| 临沂市| 视频| 青铜峡市| 嘉鱼县| 克东县| 疏勒县| 蓬安县| 陵水| 海伦市| 长沙县| 基隆市| 大埔县| 门头沟区| 海林市| 吉林省|