Created
September 18, 2010 05:54
-
-
Save yohfee/585403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace Bipolarization | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
args.ToList().ForEach(a => Bipolarize(Image.FromFile(a)).Save(NewName(a))); | |
} | |
private static Image Bipolarize(Image i) | |
{ | |
var b = new Bitmap(i); | |
Map(b).ToList().ForEach(_ => Bipolarize(b, _)); | |
return b; | |
} | |
private static IEnumerable<Point> Map(Image i) | |
{ | |
return from x in Enumerable.Range(0, i.Width) | |
from y in Enumerable.Range(0, i.Height) | |
select new Point(x, y); | |
} | |
private static void Bipolarize(Bitmap b, Point p) | |
{ | |
b.SetPixel(p.X, p.Y, Bipolarization(b.GetPixel(p.X, p.Y))); | |
} | |
private static Color Bipolarization(Color c) | |
{ | |
return Average(c) < 30 ? Color.Black : Color.White; | |
} | |
private static double Average(Color c) | |
{ | |
return new int[] { c.R, c.G, c.B }.Average(); | |
} | |
private static string NewName(string n) | |
{ | |
return Regex.Replace(n, @"\.([^\.]+)$", @".new.$1"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
require 'System.Drawing' | |
include System::Drawing | |
def bipolarize(i) | |
b = Bitmap.new(i) | |
(0...b.width).each do |x| | |
(0...b.height).each do |y| | |
b.set_pixel(x, y, bipolarization(b.get_pixel(x, y))) | |
end | |
end | |
b | |
end | |
def bipolarization(c) | |
average(c) < 30 ? Color.Black : Color.White | |
end | |
def average(c) | |
[c.r, c.g, c.b].inject{|s, c| s += c} / 3 | |
end | |
def new_name(n) | |
n.sub(/\.([^\.]+)$/, ".new.\\1") | |
end | |
ARGV.each{|a| bipolarize(Image.from_file(a)).save(new_name(a))} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment