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("全部修改完成!");
        }
    }
}

下载

源码下载 

相关推荐

  1. 批量调整图片分辨率

    2024-03-15 14:08:01       26 阅读
  2. 图片批量导入PPT

    2024-03-15 14:08:01       70 阅读
  3. 使用Python批量压缩图片

    2024-03-15 14:08:01       27 阅读
  4. Python批量图像处理--图片重命名、图片旋转

    2024-03-15 14:08:01       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 14:08:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 14:08:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 14:08:01       87 阅读
  4. Python语言-面向对象

    2024-03-15 14:08:01       96 阅读

热门阅读

  1. Spring MVC LocaleResolver原理解析

    2024-03-15 14:08:01       41 阅读
  2. css动画和js动画的区别?

    2024-03-15 14:08:01       44 阅读
  3. 企业Centos7.9系统重装初步优化文档

    2024-03-15 14:08:01       40 阅读
  4. Linux iptables 防火墙设置

    2024-03-15 14:08:01       40 阅读
  5. 【数据结构】Map和Set

    2024-03-15 14:08:01       45 阅读
  6. 手写一个线程池

    2024-03-15 14:08:01       44 阅读
  7. selinux

    selinux

    2024-03-15 14:08:01      37 阅读
  8. vue和js常识

    2024-03-15 14:08:01       38 阅读
  9. LeetCode hot100-11

    2024-03-15 14:08:01       37 阅读
  10. C#学习汇总

    2024-03-15 14:08:01       45 阅读