Last active
May 1, 2016 11:20
-
-
Save Kimserey/6e9e30a1e727bfb65c9efff6ba1dae87 to your computer and use it in GitHub Desktop.
Convert image to grayscale image (http://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale)
This file contains hidden or 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
open System | |
open System.Drawing | |
open System.Drawing.Imaging | |
// Direct translation of function taken from SO answer (http://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale) | |
let makeGrayscale (img: Image) = | |
let newImg = new Bitmap(img.Width, img.Height) | |
use g = Graphics.FromImage newImg | |
let attributes = new ImageAttributes() | |
attributes.SetColorMatrix( | |
new ColorMatrix( | |
[| [| 0.3f; 0.3f; 0.3f; 0.f; 0.f |] | |
[| 0.59f; 0.59f; 0.59f; 0.f; 0.f |] | |
[| 0.11f; 0.11f; 0.11f; 0.f; 0.f |] | |
[| 0.f; 0.f; 0.f; 1.f; 0.f |] | |
[| 0.f; 0.f; 0.f; 0.f; 1.f |] |])) | |
g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attributes) | |
newImg | |
[<EntryPoint>] | |
let main argv = | |
makeGrayscale(Bitmap.FromFile(__SOURCE_DIRECTORY__ + "/original.jpg")).Save("grayscale.jpg") | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment