Advertisement
kneefer

Resolution Separator

May 24th, 2016
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6.  
  7. namespace ResolutionSeparator
  8. {
  9.     public class ImageInfo
  10.     {
  11.         public int Width { get; set; }
  12.         public int Height { get; set; }
  13.  
  14.         public double Ratio { get; set; }
  15.  
  16.         public string FileName { get; set; }
  17.     }
  18.    
  19.     public class RatioRange
  20.     {
  21.         public Tuple<double, double> ResolutionRatio { get; private set; }
  22.         public List<ImageInfo> Images { get; set; }
  23.  
  24.         public RatioRange(Tuple<double, double> resolutionRatio)
  25.         {
  26.             ResolutionRatio = resolutionRatio;
  27.         }
  28.     }
  29.  
  30.     internal static class Program
  31.     {
  32.         private static void Main(string[] args)
  33.         {
  34.             const string inputFolderPath = @"C:\Users\Szymon\Desktop\cat.dogs\cat";
  35.             const string outputFolderPath = @"C:\Users\Szymon\Desktop\cat.dogs_filtered\cat";
  36.             const double ratioDivisionStep = 0.1;
  37.  
  38.             Console.WriteLine($"Input folder path: {inputFolderPath}");
  39.             Console.WriteLine($"Output folder path: {outputFolderPath}");
  40.  
  41.             var resolutionList = new List<ImageInfo>();
  42.             var files = Directory.GetFiles(inputFolderPath);
  43.  
  44.             Console.WriteLine("Getting resolution info of image files from input folder...");
  45.  
  46.             foreach (var file in files)
  47.             {
  48.                 using (var img = Image.FromFile(file))
  49.                 {
  50.                     resolutionList.Add(new ImageInfo
  51.                     {
  52.                         Width = img.Width,
  53.                         Height = img.Height,
  54.                         Ratio = (double) img.Width/img.Height,
  55.                         FileName = file,
  56.                     });
  57.                 }
  58.             }
  59.  
  60.             var ratio = resolutionList.Select(x => x.Ratio).ToList();
  61.             var minRatio = ratio.Min();
  62.             var maxRatio = ratio.Max();
  63.  
  64.             Console.WriteLine("Filtering and grouping image ratios...");
  65.  
  66.             var numOfsteps = (int)Math.Round((maxRatio - minRatio)/ratioDivisionStep);
  67.             var steps = Enumerable.Range(0, numOfsteps).Select(x => minRatio + (maxRatio - minRatio) * ((double)x / (numOfsteps - 1))).ToList();
  68.             var ratioRanges = steps.Zip(steps.Skip(1), (x, y) =>
  69.             {
  70.                 return new RatioRange(new Tuple<double, double>(x, y))
  71.                 {
  72.                     Images = resolutionList.Where(res => res.Ratio >= x && res.Ratio < y).ToList()
  73.                 };
  74.             }).ToList();
  75.  
  76.             Console.WriteLine("Ranges of ratios:");
  77.  
  78.             int i = 0;
  79.             foreach (var ratioRange in ratioRanges)
  80.             {
  81.                 Console.WriteLine($"{i++}. Range [{ratioRange.ResolutionRatio.Item1:0.00}, {ratioRange.ResolutionRatio.Item2:0.00}) => {ratioRange.Images.Count} files");
  82.             }
  83.  
  84.             Console.WriteLine($"Which range to output? Type index of range (0, {ratioRanges.Count - 1})");
  85.  
  86.             int choice;
  87.             while (!int.TryParse(Console.ReadLine(), out choice) || choice < 0 || choice > ratioRanges.Count - 1)
  88.                 Console.WriteLine($"Wrong choice. Type int of range (0, {ratioRanges.Count - 1})");
  89.  
  90.             Directory.CreateDirectory(outputFolderPath);
  91.  
  92.             var selectedRange = ratioRanges[choice];
  93.             foreach (var imagePath in selectedRange.Images.Select(x => x.FileName))
  94.             {
  95.                 var imageName = Path.GetFileName(imagePath);
  96.                 var outputFullpath = Path.Combine(outputFolderPath, imageName);
  97.                 File.Copy(imagePath, outputFullpath);
  98.             }
  99.  
  100.             Console.WriteLine("Finished.");
  101.             Console.ReadKey();
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement