Batch Image Resize Checking Min Max in Both Dimensions
2018-03-27 15:17
Here's some code that you can feel free to use, if it helps you. I had a large folder of photos that I use for Windows wallpaper. The sizes were all over the place, and I really wanted to better match my monitor's resolution. I looked around and didn't find a utility that exactly met my needs, which were:
- Batch resize photos using not to exceed values for width and height.
- Ignore files below specified minimum width or height.
- Simple way to ignore files based on name.
Basically, I wanted to resize most of my pictures to not exceed the monitor's natural width or height, and ignore really small ones.
For exampe, let's say I had these photos. (All dimensions are given in WIDTH x HEIGHT)
- One in landscape mode at 2500x1600
- One in landscape mode at 1400x1300
- One in landscape mode at 1000x500
- One in portrait mode at 2400x400 (weird!)
- One in portrait mode at 400x500
In the code, I set the variables this way:
// Set the min/max values of a LANDSCAPE photo.
// The code automatically handles if the photo is in portrait mode.
int maxWidth = 1800;
int maxHeight = 1190;
int minWidth = 900;
int minHeight = 600;
In plain-Engish, I'm expecting this:
- The width is too big, so reduce to 1800x1152
- The height is too big, so reduce to 1281x1190
- Both are too small. Resize to best fit of 1800x900
- Height is too big, reduce to 1190x198
- Ignored, because both dimensions are below our min dimensions
Here's the code. Happy resizing!
Warning
I'm not responsible if this doesn't work. Backup your photos, and I recommend using an empty target folder.
Dependency: NuGet package ImageLibrary
void Main()
{
bool OkToSave = true;
//Comment this to run in "test" mode
if (!OkToSave) { Console.WriteLine("Not OK to Save"); return;}
// Set the min/max values of a LANDSCAPE photo.
// The code automatically handles if the photo is in portrait mode.
int maxWidth = 1800;
int maxHeight = 1190;
int minWidth = 900;
int minHeight = 600;
// Set the source and target folders
string sourceFolder = @"C:\Users\charl\Google Drive\Documents\Clients\_SM\SktBlogImages";
string targetFolder = @"C:\Users\charl\Google Drive\Documents\Clients\_SM\Apps\HCMA Web Site Slider\SktBlogImages";
Directory.CreateDirectory(targetFolder);
string[] formats = { ".bmp", ".jpg", ".jpeg", ".png"};
//Exclude these files, if any. E.g. {"Bad Hair Day", "Music Date"}
//Note that if the filename includes the string, it will be exluded.
string[] files = { };
foreach (string file in Directory.EnumerateFiles(sourceFolder))
{
string fileName = Path.GetFileName(file);
string ext = Path.GetExtension(file).ToLower();
if (formats.Contains(ext) & !files.Contains(fileName))
{
var image = new KalikoImage(file);
//LANDSCAPE
int width = maxWidth;
int height = maxHeight;
//PORTRAIT
if (image.IsPortrait)
{
width = maxHeight;
height = maxWidth;
minWidth = minHeight;
minHeight = minWidth;
}
KalikoImage scaledImage;
//ignore if
if (image.Height < minHeight & image.Width < minWidth){
scaledImage = image;
}
else
{
//resize, preserve aspect ratio
scaledImage = image.Scale(new FitScaling(maxWidth, maxHeight));
}
//save with original resolution
ImageFormat format;
if (new string[] { ".jpg", ".jpeg" }.Contains(ext)) { format = ImageFormat.Jpeg; }
else if (ext == ".gif") { format = ImageFormat.Gif; }
else if (ext == ".bmp") { format = ImageFormat.Bmp; }
else { format = ImageFormat.Png; }
Console.WriteLine(fileName + " " + scaledImage.Width + " x " + scaledImage.Height);
if (OkToSave) { scaledImage.SaveImage(Path.Combine(targetFolder, fileName), format, true);}
}
}
}