Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- namespace ResolutionSeparator
- {
- public class ImageInfo
- {
- public int Width { get; set; }
- public int Height { get; set; }
- public double Ratio { get; set; }
- public string FileName { get; set; }
- }
- public class RatioRange
- {
- public Tuple<double, double> ResolutionRatio { get; private set; }
- public List<ImageInfo> Images { get; set; }
- public RatioRange(Tuple<double, double> resolutionRatio)
- {
- ResolutionRatio = resolutionRatio;
- }
- }
- internal static class Program
- {
- private static void Main(string[] args)
- {
- const string inputFolderPath = @"C:\Users\Szymon\Desktop\cat.dogs\cat";
- const string outputFolderPath = @"C:\Users\Szymon\Desktop\cat.dogs_filtered\cat";
- const double ratioDivisionStep = 0.1;
- Console.WriteLine($"Input folder path: {inputFolderPath}");
- Console.WriteLine($"Output folder path: {outputFolderPath}");
- var resolutionList = new List<ImageInfo>();
- var files = Directory.GetFiles(inputFolderPath);
- Console.WriteLine("Getting resolution info of image files from input folder...");
- foreach (var file in files)
- {
- using (var img = Image.FromFile(file))
- {
- resolutionList.Add(new ImageInfo
- {
- Width = img.Width,
- Height = img.Height,
- Ratio = (double) img.Width/img.Height,
- FileName = file,
- });
- }
- }
- var ratio = resolutionList.Select(x => x.Ratio).ToList();
- var minRatio = ratio.Min();
- var maxRatio = ratio.Max();
- Console.WriteLine("Filtering and grouping image ratios...");
- var numOfsteps = (int)Math.Round((maxRatio - minRatio)/ratioDivisionStep);
- var steps = Enumerable.Range(0, numOfsteps).Select(x => minRatio + (maxRatio - minRatio) * ((double)x / (numOfsteps - 1))).ToList();
- var ratioRanges = steps.Zip(steps.Skip(1), (x, y) =>
- {
- return new RatioRange(new Tuple<double, double>(x, y))
- {
- Images = resolutionList.Where(res => res.Ratio >= x && res.Ratio < y).ToList()
- };
- }).ToList();
- Console.WriteLine("Ranges of ratios:");
- int i = 0;
- foreach (var ratioRange in ratioRanges)
- {
- Console.WriteLine($"{i++}. Range [{ratioRange.ResolutionRatio.Item1:0.00}, {ratioRange.ResolutionRatio.Item2:0.00}) => {ratioRange.Images.Count} files");
- }
- Console.WriteLine($"Which range to output? Type index of range (0, {ratioRanges.Count - 1})");
- int choice;
- while (!int.TryParse(Console.ReadLine(), out choice) || choice < 0 || choice > ratioRanges.Count - 1)
- Console.WriteLine($"Wrong choice. Type int of range (0, {ratioRanges.Count - 1})");
- Directory.CreateDirectory(outputFolderPath);
- var selectedRange = ratioRanges[choice];
- foreach (var imagePath in selectedRange.Images.Select(x => x.FileName))
- {
- var imageName = Path.GetFileName(imagePath);
- var outputFullpath = Path.Combine(outputFolderPath, imageName);
- File.Copy(imagePath, outputFullpath);
- }
- Console.WriteLine("Finished.");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement