Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Drawing
- open System.Drawing.Imaging
- open System.IO
- open System
- type Sampler = int * int -> Color
- type PixelFunction = Sampler -> int * int -> Color
- let applyFunction (source: Bitmap) (target: Bitmap) (getPixel: PixelFunction) =
- let w = source.Width
- let h = source.Height
- let sampler (u: int, v: int) =
- source.GetPixel(
- (u + w) % w,
- (v + h) % h
- )
- for v in 0..(target.Height - 1) do
- for u in 0..(target.Width - 1) do
- target.SetPixel(u, v, getPixel sampler (u, v))
- let convertImage sourcePath transformation =
- let targetPath = Path.GetFileNameWithoutExtension(sourcePath) + "_output.jpg"
- use source = new Bitmap(sourcePath)
- use target: Bitmap = transformation source
- target.Save(targetPath, ImageFormat.Jpeg)
- let grayScale sourcePath =
- let rand = new Random()
- let transformation (source: Bitmap) =
- let target = new Bitmap(source.Width, source.Height)
- let getPixel samp (u, v) =
- let getIntensity u v =
- let (color: Color) = samp (u, v)
- (float color.R * 0.2126 + float color.G * 0.7152 + float color.B * 0.0722) / 255.0
- let sd = 0.5
- let range = 5
- let gaussianFunction x y =
- exp(-(float x ** 2.0 + float y ** 2.0) / (2.0 * sd ** 2.0)) / (2.0 * Math.PI * sd ** 2.0)
- let normalization =
- { -range .. range }
- |> Seq.sumBy (fun dx ->
- { -range .. range }
- |> Seq.sumBy (gaussianFunction dx))
- let pixel dx dy =
- (gaussianFunction dx dy) * getIntensity (u + dx) (v + dy) / normalization
- let intensity =
- { -range .. range }
- |> Seq.sumBy (fun dx ->
- { -range .. range }
- |> Seq.sumBy (pixel dx)
- )
- //if rand.NextDouble() > intensity
- //then Color.Black
- //else Color.White
- let x = int (intensity * 255.0)
- Color.FromArgb(x, x, x)
- applyFunction source target getPixel
- target
- convertImage sourcePath transformation
- let batchConvert (dir: string) (convertor: string -> unit) =
- Directory.EnumerateFiles(dir) |> Seq.iter convertor
- let batchConvertAsync (dir: string) (convertor: string -> unit) =
- let asyncConvertor file = async { convertor file }
- Directory.EnumerateFiles(dir)
- |> Seq.map asyncConvertor
- |> Async.Parallel
- |> Async.RunSynchronously
- |> ignore
- //batchConvertAsync "input" grayScale
- grayScale @"7_output.jpg"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement