Batch Convert Images with Only 3 Lines of Code Using LEADTOOLS!

Batch image conversion can be a valuable tool within applications to streamline workflows and prepare images for compatibility, compression, standardization, optimization, or even machine learning. LEADTOOLS stands as a reliable solution with over 165+ supported file formats to effortlessly convert files with minimal code!

In this blog, we will demonstrate the simplicity of using LEADTOOLS when batch converting images. The key objective here is to save time and effort through efficient bulk conversion and as little code as possible.

Working Application in under 10 Minutes

For this example, I created a console application in .NET 6 using LEADTOOLS to convert images in a specified folder from PNG to WebP. In under 10 minutes, I had a low-code application ready to batch convert images:


static void Main(string[] args)
{
    InitLEAD();
    var inPath = "C:\\images\\inImages";
    var outPath = "C:\\images\\outImages";
    ConvertAll(inPath, outPath);
}
static void ConvertAll(string inPath, string outPath)
{
    var files = Directory.GetFiles(inPath);
    //1 - create and load the codecs
    using (var codecs = new RasterCodecs())
    {
        foreach(string file in files)
        {
            Console.WriteLine($"Converting file: {file}");
            //2 - using the codecs, load the image file
            using (var image = codecs.Load(file))
            {
                var fileName = Path.GetFileNameWithoutExtension(file);
                //3 - using the codecs, save and convert the new image file to the specified path
                codecs.Save(image, $"{outPath}\\{fileName}.webp", RasterImageFormat.Webp, 0);
            }
        }
        Console.WriteLine($"File Converted and saved at: {outPath}");
    }
}

As you can see in the code above, after setting the license (InitLEAD()), the core functionality of the application is encapsulated in just 3 lines of code where we create and load the codecs, load the image file, and save the converted image file to the specified path.

Additional Considerations:

While the above application is concise, we have to acknowledge considerations for practical scenarios. For instance:

Addressing non-existent output paths:


if (!Directory.Exists(outPath)) 
      Directory.CreateDirectory(outPath); 

Refining the conversion process for specific file types, such as converting only JPG files:


var files = Directory.GetFiles(directory, "*.jpg"); 

Customize to Fit Your Needs

This application is just the tip of the iceberg with showcasing the flexibility of the technology within LEADTOOLS! For a more intelligent application, you can check out our ready-built components, like our Document Converter SDK libraries to convert between image and document formats alike, or our document and medical web viewers, file converters, recognition engines, and more!

Try for Free Today!

Be sure to download our FREE 60-day evaluation toolkit to try LEADTOOLS yourself and gain access to our extensive online documentation, sample source code, demos, and tutorials. See just how easy it is to implement LEADTOOLS into your existing or new applications, all while being fully supported by our amazing support team.

About 

This entry was posted in File Formats, General Imaging and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *