Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.42 KB | None | 0 0
  1. open System.Drawing
  2. open System.Drawing.Imaging
  3. open System.IO
  4. open System.Diagnostics
  5.  
  6.  
  7. let applyTransform file (trans: Bitmap -> Bitmap) =
  8.     let outFile = Path.GetFileNameWithoutExtension(file) + "-out.jpg";
  9.     use inputImage = new Bitmap(file)
  10.     use outputImage = trans inputImage
  11.     outputImage.Save(outFile, ImageFormat.Jpeg)
  12.  
  13.  
  14. let batchTransform directory (trans: Bitmap -> Bitmap) =
  15.     let asyncApply file = async { applyTransform file trans }
  16.  
  17.     Directory.EnumerateFiles(directory, "*.jpg")
  18.         |> Seq.map asyncApply
  19.         |> Async.Parallel
  20.         |> Async.RunSynchronously
  21.         |> ignore
  22.        
  23. type ColorTransform = Color -> Color
  24. type UVTransform = int * int -> int * int
  25.  
  26. let applyPixelTransform (colorTrans: ColorTransform) (uvTrans: UVTransform) (width: int) (height: int) (image: Bitmap) =
  27.     let output = new Bitmap(width, height)
  28.  
  29.     for i in 0..(width - 1) do
  30.         for j in 0..(height - 1) do
  31.             output.SetPixel(i, j, image.GetPixel(i, j) |> colorTrans) |> ignore
  32.  
  33.     output
  34.  
  35.  
  36. let var1_grayscalePixel (color: Color) =
  37.     let x = (int color.R + int color.G + int color.B) / 3
  38.     Color.FromArgb(x, x, x)
  39.  
  40. let var2_invertPixel (color: Color) =
  41.     let inv x = 255 - int x
  42.     Color.FromArgb(inv color.R, inv color.G, inv color.B)
  43.  
  44.  
  45.  
  46. let timer = Stopwatch.StartNew()
  47. batchTransform "input" (applyPixelTransform var1_grayscalePixel)
  48. printf "%A" timer.Elapsed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement