Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active August 29, 2015 14:04
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 7shi/df3ee5f349a47dce81a4 to your computer and use it in GitHub Desktop.
Save 7shi/df3ee5f349a47dce81a4 to your computer and use it in GitHub Desktop.
[F#]グレースケールJPEGに変換
// This file is in the public domain.
#r "System"
#r "System.Xaml"
#r "PresentationCore"
#r "PresentationFramework"
#r "WindowsBase"
open System
open System.IO
open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Media.Imaging
let rec subdir cb dir =
Directory.GetDirectories dir |> Array.iter (subdir cb)
Directory.GetFiles dir |> Array.iter cb
let rec convert f =
if Directory.Exists f then subdir convert f else
if Path.GetExtension(f).ToLower() <> ".jpg" then () else
let dir = Path.GetDirectoryName f
let fn = Path.GetFileName f
let outdir = Path.Combine(dir, "output")
if not <| Directory.Exists outdir then
Directory.CreateDirectory outdir |> ignore
use fs1 = new FileStream(f, FileMode.Open)
let decoder = JpegBitmapDecoder(fs1,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default)
let encoder = JpegBitmapEncoder(QualityLevel = 85)
let gray = FormatConvertedBitmap(decoder.Frames.[0], PixelFormats.Gray8, null, 0.)
encoder.Frames.Add(BitmapFrame.Create gray)
use fs2 = new FileStream(Path.Combine(outdir, fn), FileMode.Create)
encoder.Save fs2
[<EntryPoint; STAThread>] do
let w = Window(Title = "グレースケール変換",
Width = 300., Height = 200., AllowDrop = true)
let g = Grid()
g.Children.Add (
Label(Content = "ここに .jpg ファイルを DnD してください。",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center)) |> ignore
w.Content <- g
w.DragEnter.Add <| fun e ->
if e.Data.GetDataPresent DataFormats.FileDrop then
e.Effects <- DragDropEffects.Link
w.Drop.Add <| fun e ->
let files = e.Data.GetData(DataFormats.FileDrop) :?> string[]
if files <> null then files |> Array.iter convert
Application().Run(w) |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment