Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- namespace ImageDiff
- {
- internal class Program
- {
- /// <summary>
- /// Reads all PNG files from inputPath, compares each image to the PNG file at comparisonImagePath
- /// and outputs a new PNG for each input file where pixels that are the same or similar color as in
- /// the comparison image set as transparent
- /// </summary>
- /// <param name="args"></param>
- private static void Main(string[] args)
- {
- const long tolerance = 150;
- string inputPath = @"C:\images\in\";
- string outputPath = @"C:\images\out\";
- string comparisonImagePath = @"C:\images\blank.png";
- var comparisonImage = new Bitmap(comparisonImagePath);
- int height = comparisonImage.Height;
- int width = comparisonImage.Width;
- foreach(string file in Directory.GetFiles(inputPath, "*.png"))
- {
- var input = new Bitmap(file);
- //Init transparent
- Bitmap output = new Bitmap(width, height);
- using (Graphics gfx = Graphics.FromImage(output))
- using (SolidBrush brush = new SolidBrush(Color.Transparent))
- {
- gfx.FillRectangle(brush, 0, 0, width, height);
- }
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- Color bgColor = comparisonImage.GetPixel(x, y);
- Color inputColor = input.GetPixel(x, y);
- double diff = Math.Pow((inputColor.R - bgColor.R)*0.3, 2) +
- Math.Pow((inputColor.G - bgColor.G)*0.59, 2) +
- Math.Pow((inputColor.B - bgColor.B)*0.11, 2);
- if (diff > tolerance)
- output.SetPixel(x, y, inputColor);
- }
- }
- output.Save(Path.Combine(outputPath, Path.GetFileName(file)), ImageFormat.Png);
- Console.WriteLine("Converted " + file);
- }
- Console.WriteLine("Done");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement