Skip to content

Instantly share code, notes, and snippets.

@virasak
Created March 31, 2010 04:40
Show Gist options
  • Save virasak/349942 to your computer and use it in GitHub Desktop.
Save virasak/349942 to your computer and use it in GitHub Desktop.
Pixel count from image files with preset threshold at 127
if (!args.isEmpty) {
println("Filename\t\tTotal\tBlack\tWhite")
args map to_file filter only_real_file foreach { file =>
val image = javax.imageio.ImageIO.read(file)
val (height, width, total) = (image.getHeight, image.getWidth, image.getHeight*image.getWidth)
val rgbs = for (y <- 0 until height; x <- 0 until width) yield image.getRGB(x,y)
val black = rgbs map to_gray count if_under_threshold
println("%s\t\t%d\t%d\t%d" format (file.getName, total, black, total - black))
}
} else {
println("Usage: scala PixelCount.scala [list of image file names]")
}
def to_gray = (rgb: Int) => ((rgb&0xFF0000>>16) + (rgb&0x00FF00>>8) + (rgb&0x0000FF))/3
def to_file = new java.io.File(_: String)
def if_under_threshold = (_: Int) < 127
def only_real_file = (_: java.io.File).isFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment