C
C#4mo ago
Kiel

✅ How to bulk compress a directory of images until each individual image is under a set size?

I have a directory of 1200 images, some of which are and some of which aren't under 256KB. I need all images that are to be compressed, resized, etc until they are under 256KB with as minimal changes as possible... I currently have Magick.NET in my project so I'd prefer to use that if possible, but I don't even know where to begin. Should I prefer compression? Resizing? Both? Something else?
2 Replies
Omnissiah
Omnissiah4mo ago
is that not a requisite? how can you not know that you need an image of a specific resolution for example
wujun68248@gmail.com
using System;
using System.Collections.Generic;
using System.IO;
using ImageMagick;

public class BatchImageCompressor
{
public static void CompressImages(string sourceDirectory, string targetDirectory, long maxSizeInBytes)
{
// Make sure the target directory exists
if (!Directory.Exists(targetDirectory))
Directory.CreateDirectory(targetDirectory);

// Get all image files in the source directory
string[] files = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);

// Compress Pictures
foreach (string file in files)
{
using (MagickImage image = new MagickImage(file))
{
// Try different compression levels until the file size meets your requirements.
while (image.GetBaseSize() > maxSizeInBytes && image.Quality > 1)
{
image.Quality--;
}

// Save the compressed image
string outputFile = Path.Combine(targetDirectory, Path.GetFileName(file));
image.Write(outputFile);
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using ImageMagick;

public class BatchImageCompressor
{
public static void CompressImages(string sourceDirectory, string targetDirectory, long maxSizeInBytes)
{
// Make sure the target directory exists
if (!Directory.Exists(targetDirectory))
Directory.CreateDirectory(targetDirectory);

// Get all image files in the source directory
string[] files = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);

// Compress Pictures
foreach (string file in files)
{
using (MagickImage image = new MagickImage(file))
{
// Try different compression levels until the file size meets your requirements.
while (image.GetBaseSize() > maxSizeInBytes && image.Quality > 1)
{
image.Quality--;
}

// Save the compressed image
string outputFile = Path.Combine(targetDirectory, Path.GetFileName(file));
image.Write(outputFile);
}
}
}
}
In this code, the CompressImages method accepts the source directory, the destination directory, and the maximum file size (in bytes) as parameters. It iterates through all the files in the source directory, and for each image file, loads the image using a MagickImage object, then enters a loop that continuously reduces the image quality until its size does not exceed the preset maximum. Finally, the compressed image is saved to the destination directory. I don't know if this can help you.
Want results from more Discord servers?
Add your server