Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Last active August 27, 2015 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krishnanraman/3865152aa62dcf1da03d to your computer and use it in GitHub Desktop.
Save krishnanraman/3865152aa62dcf1da03d to your computer and use it in GitHub Desktop.
import java.awt.image.{BufferedImage, WritableRaster}
import javax.imageio.ImageIO
import java.io.File
object grayscale extends App {
val (imgtype, imagefile, copyfile) = (args(0), args(1), args(2))
val img = ImageIO.read(new File(imagefile))
val mytype = img.getType
val raster:WritableRaster = img.getRaster
val (w,h) = (img.getWidth, img.getHeight)
(0 until w).foreach { x=>
(0 until h).foreach { y=>
val arr = Array.fill[Double](3)(0.0)
val bgr = raster.getPixel(x,y, arr)
val avg = (bgr(0) + bgr(1) + bgr(2))/3.0
raster.setPixel(x,y,Array(avg,avg,avg))
}
}
val copy = new BufferedImage(w,h,mytype)
copy.setData(raster)
ImageIO.write(copy, imgtype, new File(copyfile))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment