首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenCvSharp 图片批量改名

  • 25-02-19 03:01
  • 3814
  • 5821
blog.csdn.net

目录

效果

项目

代码

下载


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 files=new List();
        String[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
        
        ///


        /// 选择文件夹
        ///

        ///
        ///
        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()+ "]张图片");
            }

        }

        ///


        /// 修改名称
        ///

        ///
        ///
        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. using NLog;
  2. using OpenCvSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace OpenCvSharp_Demo
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);
  16. }
  17. private static Logger _log = NLog.LogManager.GetCurrentClassLogger();
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. }
  21. string inPath = "";
  22. string outPath = "";
  23. DirectoryInfo folder;
  24. List<FileInfo> files=new List<FileInfo>();
  25. String[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
  26. /// <summary>
  27. /// 选择文件夹
  28. /// </summary>
  29. /// <param name="sender"></param>
  30. /// <param name="e"></param>
  31. private void button1_Click(object sender, EventArgs e)
  32. {
  33. inPath = "";
  34. outPath = "";
  35. files.Clear();
  36. FolderBrowserDialog dialog = new FolderBrowserDialog();
  37. dialog.Description = "请选择文件路径";
  38. if (dialog.ShowDialog() == DialogResult.OK)
  39. {
  40. inPath = dialog.SelectedPath;
  41. textBox1.Text = inPath;
  42. outPath = inPath + "\\out";
  43. textBox2.Text = outPath;
  44. _log.Info("图片路径:" + inPath);
  45. _log.Info("保存路径:" + outPath);
  46. folder = new DirectoryInfo(inPath);
  47. var temp = folder.GetFiles("*.*", SearchOption.TopDirectoryOnly);
  48. foreach (FileInfo file in temp)
  49. {
  50. if (imageExtensions.Contains(file.Extension.ToLower()))
  51. {
  52. files.Add(file);
  53. }
  54. }
  55. _log.Info("一共["+ files .Count()+ "]张图片");
  56. }
  57. }
  58. /// <summary>
  59. /// 修改名称
  60. /// </summary>
  61. /// <param name="sender"></param>
  62. /// <param name="e"></param>
  63. private void button2_Click(object sender, EventArgs e)
  64. {
  65. if (files.Count()==0)
  66. {
  67. return;
  68. }
  69. outPath = textBox2.Text;
  70. //目录不存在 则创建
  71. if (!Directory.Exists(outPath))
  72. {
  73. DirectoryInfo directoryInfo = new DirectoryInfo(outPath);
  74. //创建目录
  75. directoryInfo.Create();
  76. }
  77. else {
  78. DirectoryInfo outFolder=new DirectoryInfo(outPath);
  79. if (outFolder.GetFiles("*.*", SearchOption.AllDirectories).Length>0)
  80. {
  81. MessageBox.Show(outPath + "文件夹不为空,防止数据被覆盖,请更换!");
  82. return;
  83. }
  84. }
  85. string oldName;
  86. string newName;
  87. Mat temp;
  88. int index = 0;
  89. foreach (FileInfo file in files)
  90. {
  91. oldName = file.Name;
  92. newName = index.ToString() + file.Extension;
  93. try
  94. {
  95. temp = new Mat(inPath + "\\" + oldName);
  96. //其他处理 ,例如
  97. //图片缩放
  98. //通道变换等
  99. //……
  100. Cv2.ImWrite(outPath + "\\" + newName, temp);
  101. _log.Info(oldName + "-->" + newName);
  102. index++;
  103. }
  104. catch (Exception ex)
  105. {
  106. _log.Info(oldName+"修改异常,异常信息:"+ex.Message);
  107. }
  108. }
  109. _log.Info("全部修改完成!");
  110. }
  111. }
  112. }

下载

源码下载 

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/136623862"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top