Skip to content

Instantly share code, notes, and snippets.

@ruslander
Created December 9, 2016 23:53
Show Gist options
  • Save ruslander/55f888b0c09b21dfe374d40c065e1af4 to your computer and use it in GitHub Desktop.
Save ruslander/55f888b0c09b21dfe374d40c065e1af4 to your computer and use it in GitHub Desktop.
Detect corrupted charts
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
namespace TooMuchWhite
{
class Program
{
static void Main(string[] args)
{
var files = Directory.EnumerateFiles(@"C:\Users\rrusu\Desktop\last check", "924.png", SearchOption.AllDirectories);
//var files = Directory.EnumerateFiles("Samples");
var cntr = 0;
Console.WriteLine("Files to process " + files.Count());
foreach (var file in files)
{
cntr++;
if (cntr%10 != 0)
Console.Write(".");
else
Console.Write(cntr);
var suspectCorrupted = IsSuspiciousEnough(file);
if (suspectCorrupted)
Console.WriteLine("\t " + file);
}
Console.ReadKey();
}
private static bool IsSuspiciousEnough(string file)
{
bool suspectCorrupted;
using (var image = Image.FromFile(file))
using (var bmp = GrayScale(new Bitmap(image)))
{
var colors = new Dictionary<Color, int>();
for (int i = 0; i < image.Height; i++)
{
for (int j = 0; j < image.Width; j++)
{
var nowColor = bmp.GetPixel(j, i);
if (colors.ContainsKey(nowColor))
colors[nowColor]++;
else
colors[nowColor] = 1;
}
}
var tops = colors.OrderByDescending(x => x.Value).Take(3);
var white = Color.White;
var topColor = tops.First().Key;
var total = image.Height*image.Width;
var inRatioOf = (int) Math.Round((double) (100*tops.First().Value)/total);
var topColorIsWhite =
white.A == topColor.A &&
white.R == topColor.R &&
white.G == topColor.G &&
white.B == topColor.B;
suspectCorrupted = topColorIsWhite && inRatioOf > 90;
/*else
{
Console.WriteLine(file);
}*/
/*foreach (var color in tops)
{
var colorPerc = (int)Math.Round((double)(100 * color.Value) / total);
Console.WriteLine("\t " + colorPerc + "% " + color.Key + " " + color.Value);
}
Console.WriteLine();
*/
}
return suspectCorrupted;
}
public static Bitmap GrayScale(Bitmap Bmp)
{
int rgb;
Color c;
for (int y = 0; y < Bmp.Height; y++)
for (int x = 0; x < Bmp.Width; x++)
{
c = Bmp.GetPixel(x, y);
rgb = (int)((c.R + c.G + c.B) / 3);
Bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
}
return Bmp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment