Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.36 KB | None | 0 0
  1. open System.Drawing
  2. open System
  3. open System.IO
  4. open System.Threading
  5. open System.Diagnostics
  6.  
  7. let transformImage trans (path: string) =
  8.     let from = new Bitmap(path)
  9.     let cvs = new Bitmap(from.Width, from.Height)
  10.    
  11.     for i in 0..cvs.Height - 1 do
  12.         for j in 0..cvs.Width - 1 do
  13.             let p: Color = trans from i j
  14.             cvs.SetPixel(j, i, p)
  15.  
  16.     cvs.Save(path + ".cvs.jpg")
  17.  
  18. let rand = new ThreadLocal<Random>(fun () -> new Random())
  19.  
  20. let grayscaleTrans (cvs: Bitmap) i j =
  21.     let p = cvs.GetPixel(j, i)
  22.     let h = (0.2126 * float p.R + 0.7152 * float p.G + 0.0722 * float p.B) / 255.0
  23.    
  24.     if rand.Value.NextDouble() < h
  25.     then Color.White
  26.     else Color.Black
  27.  
  28. let grayscale = transformImage grayscaleTrans
  29.  
  30. let batchProcess conv dir =
  31.     Directory.EnumerateFiles(dir) |> Seq.iter conv
  32.  
  33. let asyncProcess conv dir =
  34.     Directory.EnumerateFiles(dir)
  35.         |> Seq.map (fun path -> async { conv path })
  36.         |> Async.Parallel
  37.         |> Async.RunSynchronously
  38.         |> ignore
  39.  
  40. Directory.EnumerateFiles("input", "*.cvs.jpg") |> Seq.iter File.Delete
  41.  
  42. let t = Stopwatch.StartNew()
  43. asyncProcess grayscale "input"
  44. printf "asyncProcess %A" t.Elapsed
  45.  
  46. Directory.EnumerateFiles("input", "*.cvs.jpg") |> Seq.iter File.Delete
  47.  
  48. t.Restart()
  49. batchProcess grayscale "input"
  50. printf "batchProcess %A" t.Elapsed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement