Advertisement
Guest User

ImageDiff (C# Console App to "diff" images)

a guest
Mar 30th, 2011
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5.  
  6. namespace ImageDiff
  7. {
  8.     internal class Program
  9.     {
  10.         /// <summary>
  11.         /// Reads all PNG files from inputPath, compares each image to the PNG file at comparisonImagePath
  12.         /// and outputs a new PNG for each input file where pixels that are the same or similar color as in
  13.         /// the comparison image set as transparent
  14.         /// </summary>
  15.         /// <param name="args"></param>
  16.         private static void Main(string[] args)
  17.         {
  18.             const long tolerance = 150;
  19.  
  20.             string inputPath = @"C:\images\in\";
  21.             string outputPath = @"C:\images\out\";
  22.             string comparisonImagePath = @"C:\images\blank.png";
  23.  
  24.             var comparisonImage = new Bitmap(comparisonImagePath);
  25.             int height = comparisonImage.Height;
  26.             int width = comparisonImage.Width;
  27.  
  28.             foreach(string file in Directory.GetFiles(inputPath, "*.png"))
  29.             {
  30.                 var input = new Bitmap(file);
  31.  
  32.                 //Init transparent
  33.                 Bitmap output = new Bitmap(width, height);
  34.                 using (Graphics gfx = Graphics.FromImage(output))
  35.                 using (SolidBrush brush = new SolidBrush(Color.Transparent))
  36.                 {
  37.                     gfx.FillRectangle(brush, 0, 0, width, height);
  38.                 }
  39.  
  40.                 for (int x = 0; x < width; x++)
  41.                 {
  42.                     for (int y = 0; y < height; y++)
  43.                     {
  44.                         Color bgColor = comparisonImage.GetPixel(x, y);
  45.                         Color inputColor = input.GetPixel(x, y);
  46.  
  47.                         double diff = Math.Pow((inputColor.R - bgColor.R)*0.3, 2) +
  48.                                       Math.Pow((inputColor.G - bgColor.G)*0.59, 2) +
  49.                                       Math.Pow((inputColor.B - bgColor.B)*0.11, 2);
  50.  
  51.                        
  52.                         if (diff > tolerance)
  53.                             output.SetPixel(x, y, inputColor);
  54.                     }
  55.                 }
  56.  
  57.                 output.Save(Path.Combine(outputPath, Path.GetFileName(file)), ImageFormat.Png);
  58.                 Console.WriteLine("Converted " + file);
  59.             }
  60.             Console.WriteLine("Done");
  61.             Console.ReadKey();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement