C#-oriented AI programming-batch convert PNG files to JPG to filter out small icons–[InsCodeAI Programming]

InsCodeAI’s series of articles on AI programming

InsCodeAI Programming Special Collection Framework Technology and Difficulty Factor (Five-Star System)
java’s AI-oriented programming-InsCode-batch convert PNG files to JPG to filter out small icons difficulty
java-oriented AI programming – read a code file and change the lines of code in it – [InsCodeAI programming]
C#-oriented AI programming – batch change file names Redemption – [InsCodeAI Programming]
c#-oriented AI programming – batch processing JPG images to fixed width

Article directory

  • This series of school motto
  • The technical foundation of the completion (operation)
  • Environment and tools:
  • job description
    • overall function
    • Trick to AI
    • What is the core code:
  • Summarize
  • supporting resources

This series of school motto

Hurt each other and entangle each other, you have to worry about studying hard, I am born with talents that will be useful, and my fate is up to me!
With AI Niu Fantian, the difficulty of the homework is average, so let’s pay attention to the collection points.

Technical foundation for completion (operation)

Language selection Include album link Volume degree
C After Zhang Xuefeng recommends choosing a computer major – roll it up during college – [College Life]
JAVA The knowledge scope and learning steps of the JAVA part of the black horse station B video
JAVAWEB The knowledge scope and learning steps of the JAVAWEB part of the black horse station B video
SpringBoot SpringBoot knowledge scope – learning steps [000 of JSB series]
WeChat Program A detailed analysis of the dark horse WeChat applet video – [Knowledge scope of mind map]
python Detailed analysis of python video selection – [Mind Map knowledge range] ?
php How to learn PHP – [Mind Map Knowledge Scope]

Environment and tools:

Environment of this series

Environment win11
Tools idea 2017/idea 2018 /visual studio 2017
jdk None
Database None
maven None
Project import method Open directory
Database front-end tools None
Use AI InsCode

Assignment Description

I used AI to make a JAVA batch change from PNG to JPG before, and the program ran without problems, but the JAVA code is good, and C# can be used as EXE.
So, I want to try again with AI and make a practical gadget with C#. In this way, it is convenient.
Now we are not studying the impact of AI on beginners, but how to use AI to better complete our work.
I even saw an article like this now
What are the application scenarios of AI in the field of recruitment? 》
“Proficient in “Micro-expression & Physiognomy” AI interviewer is here, please take the call”
HR has started to play with AI, so as a programmer or quasi-programmer, how can you break through if you don’t know AI yet?
Will it develop to the point where the interviewer just asks questions, all over the place, with cameras installed in the room, and then directly asks the AI after the interview, how many points will this candidate receive for his skills and quality? How many points are given for reaction ability and how many points are given for stress tolerance?
AI can veto candidates with one vote. Will it really come true in the not-too-distant future?

Overall functionality

Ok, let me talk about the function of this job.
It is to convert the PNG images in some project directories into JPG images (PNG images excluding those small icons are assumed to be less than 100 in width or less than 100 in height)

Tell AI


AI once again gives the linux command line.
Give it a turn.

The whole code is as follows:

using System;
using System.IO;

namespace FindPNGImages
{<!-- -->
    class Program
    {<!-- -->
        static void Main(string[] args)
        {<!-- -->
            string directoryPath = "C:\path\to\directory";

            FindPNGImages(directoryPath);

            Console.ReadKey();
        }

        static void FindPNGImages(string directoryPath)
        {<!-- -->
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            foreach (FileInfo file in directory. GetFiles("*.png"))
            {<!-- -->
                Console.WriteLine(file.FullName);
            }

            foreach (DirectoryInfo subDirectory in directory. GetDirectories())
            {<!-- -->
                FindPNGImages(subDirectory.FullName);
            }
        }
    }
}

Verify it:
It ran perfectly.


The code I put in the project:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System. Drawing;
using System.IO;
using System.Linq;
using System. Text;
using System. Threading. Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static void FindPNGImages(string directoryPath)
        {
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            foreach (FileInfo file in directory. GetFiles("*.png"))
            {
                Console.WriteLine(file.FullName);
            }

            foreach (DirectoryInfo subDirectory in directory. GetDirectories())
            {
                FindPNGImages(subDirectory.FullName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string directoryPath = "G:\vue";

            FindPNGImages(directoryPath);

            Console.ReadKey();


        }
    }
}

However, an exception occurred,

Since this abnormality appears in the middle of more than N pictures, let’s make a judgment first, skip it, and let the program run down smoothly.

Okay, I found hundreds of pictures,
I don’t understand the latter question. I’m not very good at C# either, so I’ll jump over there first.

using System;
using System. Drawing;
using System.IO;

namespace FindPNGImages
{
    class Program
    {
        static void Main(string[] args)
        {
            string directoryPath = "C:\path\to\directory";

            FindPNGImages(directoryPath);

            Console.ReadKey();
        }

        static void FindPNGImages(string directoryPath)
        {
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            foreach (FileInfo file in directory. GetFiles("*.png"))
            {
                Image image = Image.FromFile(file.FullName);
                if (image.Height > 100 & amp; & amp; image.Width > 120)
                {
                    Console.WriteLine(file.FullName);
                }
                image.Dispose();
            }

            foreach (DirectoryInfo subDirectory in directory. GetDirectories())
            {
                FindPNGImages(subDirectory.FullName);
            }
        }
    }
}

So, what we need is to convert to JPG, and then delete the PNG
First ask the AI one by one


This time the AI “thinks” for a longer time.

using System;
using System. Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace FindAndSavePNGImages
{<!-- -->
    class Program
    {<!-- -->
        static void Main(string[] args)
        {<!-- -->
            string directoryPath = "C:\path\to\directory";

            FindAndSavePNGImages(directoryPath);

            Console.ReadKey();
        }

        static void FindAndSavePNGImages(string directoryPath)
        {<!-- -->
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            foreach (FileInfo file in directory.GetFiles("*.png"))
            {<!-- -->
                using (Image image = Image.FromFile(file.FullName))
                {<!-- -->
                    if (image.Height > 100 & amp; & amp; image.Width > 120)
                    {<!-- -->
                        string jpegPath = Path.ChangeExtension(file.FullName, ".jpg");
                        using (Bitmap bitmap = new Bitmap(image.Width, image.Height))
                        {<!-- -->
                            using (Graphics graphics = Graphics.FromImage(bitmap))
                            {<!-- -->
                                graphics.DrawImage(image, Point.Empty);
                            }
                            bitmap.Save(jpegPath, ImageFormat.Jpeg);
                        }
                        Console.WriteLine($"Image {file.FullName} converted to {jpegPath}");
                    }
                }
            }

            foreach (DirectoryInfo subDirectory in directory.GetDirectories())
            {<!-- -->
                FindAndSavePNGImages(subDirectory.FullName);
            }
        }
    }
}

 

If you don’t know what’s going on, run it:

After a long few minutes. .
The program appears to have completed successfully? ?

It seems this is done.
The length, width, and size are all rotated, but the small ones are not.
It’s just that some PNGs are converted to black and some are converted to white. Let’s forget about this for now. After all, the actual big picture is full screen.

Then, we can delete the converted PNG.

using System;
using System. Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace FindAndSavePNGImages
{
    class Program
    {
        static void Main(string[] args)
        {
            string directoryPath = "C:\path\to\directory";

            FindAndSavePNGImages(directoryPath);

            Console.ReadKey();
        }

        static void FindAndSavePNGImages(string directoryPath)
        {
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            foreach (FileInfo file in directory.GetFiles("*.png"))
            {
                using (Image image = Image.FromFile(file.FullName))
                {
                    if (image.Height > 100 & amp; & amp; image.Width > 120)
                    {
                        string jpegPath = Path.ChangeExtension(file.FullName, ".jpg");
                        using (Bitmap bitmap = new Bitmap(image.Width, image.Height))
                        {
                            using (Graphics graphics = Graphics.FromImage(bitmap))
                            {
                                graphics.DrawImage(image, Point.Empty);
                            }
                            bitmap.Save(jpegPath, ImageFormat.Jpeg);
                        }
                        File.Delete(file.FullName);
                        Console.WriteLine($"Image {file.FullName} converted to {jpegPath} and deleted");
                    }
                }
            }

            foreach (DirectoryInfo subDirectory in directory.GetDirectories())
            {
                FindAndSavePNGImages(subDirectory.FullName);
            }
        }
    }
}

This also shows that AI is powerful, it really just adds one line.

Because I am not familiar with C#, so I had to hand over this error to AI.

using (Image image = Image.FromFile(file.FullName))
{
    if (image.Height > 100 & amp; & amp; image.Width > 120)
    {
        string jpegPath = Path.ChangeExtension(file.FullName, ".jpg");
        using (Bitmap bitmap = new Bitmap(image. Width, image. Height))
        {
            using (Graphics graphics = Graphics. FromImage(bitmap))
            {
                graphics.DrawImage(image, Point.Empty);
            }
            bitmap.Save(jpegPath, ImageFormat.Jpeg);
        }
        image.Dispose(); // Release image resources
        File.Delete(file.FullName);
        Console.WriteLine($"Image {file.FullName} converted to {jpegPath} and deleted");
    }
}

Open it again, the directory just now

Just so handsome.

This thing is done
If this code is combined with other codes, it will be a useful little tool.
This is the daily algorithm in the project. Of course, in your homework, it is absolutely the core code!

What is core code:

The core code of the application software refers to the code of the most critical part of the program. For example, WinRAR, its core code is the compression algorithm part, while parts such as user interface and operating system porting are insignificant.
The core code of the mall category refers to the code of the business layer. For example, the core code of your mall is: products, shopping carts, creating orders, and payment. These codes are the core codes.

As programmers, we often need to understand other people’s code. Especially in the open source community, we need to understand the code of many excellent open source projects. And on a code hosting platform like Gitee, how can we quickly and effectively understand other people’s code? This article will introduce you to some methods.

1. Read the README and project introduction

On Gitee, many open source projects will have their own README files or project introductions. These files generally introduce the background, functions, usage, etc. of the project, which can help us quickly understand the basic situation of this open source project. If we can find content related to ourselves from these files, we can quickly get started with the code of this open source project.

2. Understand project structure and code organization

Before reading the code, we need to first understand the code structure and code organization of this open source project. Usually, open source projects encapsulate different functional modules into different code files and organize them according to a certain directory structure. If we can understand how the code of this open source project is organized, we can find the code we need more quickly.

3. Leverage IDEs and Tools

IDEs and some code reading tools can help us read code faster and more efficiently. For example, Java developers can use IDEs such as Eclipse or IntelliJ IDEA to quickly open code files and view information such as classes, methods, and variables. In addition, some code reading tools, such as Source Insight, CodeCompare, etc., can help us more easily view the structure and relationships of the code, and quickly jump to related code.

4. Pay attention to code comments and documentation

Good code comments and documentation can help us understand the code more quickly. Therefore, when reading other people’s code, we can focus on code comments and documentation. Some open source projects provide detailed documentation, while others focus on code comments. If we can have a systematic reading and understanding of code comments and documentation, we can master other people’s code more quickly.

5. Run the test and run the project

If we want to have a deeper understanding of other people’s code, we can try to run through the relevant tests, or run the open source project directly. By running tests and running projects, we can understand the implementation details and specific business logic of the code more intuitively.

Summarize:

The above are some ways to quickly understand other people’s code on Gitee, I hope it will be helpful to everyone. Of course, reading code is a matter of patience and care, and requires us to spend a little more time and thought. Only by calming down and reading each line of code slowly can we truly understand their meaning and function.

Summary

In one sentence, AI is too powerful, really too powerful.
People who used to study were not so formal, but they dived into writing code. What about people now? In addition to BBLLs on various networks, can those who really write code not find a job? Even if you don’t know how to write, but you have watched so many videos, using AI is such a fun thing, why don’t you give it a try?

paper reference
“Design and Realization of Tourism Website in Saibei Villages and Towns Based on JSP – [Graduation Thesis]”
https://blog.csdn.net/dearmite/article/details/131962993

Supporting resources

C#’s AI-oriented programming – batch convert PNG files to JPG to filter out small icons – [InsCodeAI Programming]
https://download.csdn.net/download/dearmite/88250564

Compiled gadget:
Added a function to rename all files in a folder with a number as an index (subfolders are not supported)
Added PNG to JPG third version

https://download.csdn.net/download/dearmite/88250580