Skip to content

Instantly share code, notes, and snippets.

@Kimserey
Last active May 1, 2016 11:20
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 Kimserey/6e9e30a1e727bfb65c9efff6ba1dae87 to your computer and use it in GitHub Desktop.
Save Kimserey/6e9e30a1e727bfb65c9efff6ba1dae87 to your computer and use it in GitHub Desktop.
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