Last active
August 27, 2015 23:35
-
-
Save krishnanraman/3865152aa62dcf1da03d 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
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