Skip to content

Instantly share code, notes, and snippets.

@JLimperg
Last active August 29, 2015 14:11
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 JLimperg/03e460d8061ac67bf97a to your computer and use it in GitHub Desktop.
Save JLimperg/03e460d8061ac67bf97a to your computer and use it in GitHub Desktop.
friday usage sample
module ImageTest where
import Control.Monad
import Data.Functor
import Vision.Image.RGB -- [1]
import Vision.Image.Storage
import Vision.Image.Type
import Vision.Primitive.Shape
-- [1] For convenience, you can also just import Vision.Image, which
-- reexports the whole friday API.
imgPath :: FilePath
imgPath = "./test.png"
loadImg :: IO (Either StorageError StorageImage)
loadImg = load Nothing imgPath -- [2]
-- [2] The `Maybe` argument allows you to specify how your input image
-- is supposed to be encoded. Since I don't care, I didn't specify
-- anything here.
convertImg :: StorageImage -> RGB
convertImg = convert
middlePixel :: (Image i) => i -> ImagePixel i
middlePixel img = index img middle
where
Z :. height :. width = shape img -- [3]
middle = ix2 (height `div` 2) (width `div` 2) -- [4]
-- [3] `Z :. height :. width` pattern matches on friday's slightly
-- complex representation of a point. It is created using
-- the constructors `Z` and `:.` and has type `DIM2`. This encoding
-- is similar to Peano numbers, with `Z` corresponding to the zero
-- constructor and `:.` corresponding to `successor`, only written
-- as an infix operator.
-- [4] `ix2` creates a `DIM2` value. This is equivalent to
-- Z :. height :. width.
isMoreRedThanGreen :: RGBPixel -> Bool
isMoreRedThanGreen (RGBPixel red green _) = red >= green
main :: IO ()
main = either reportError processImg =<< loadImg
where
processImg :: StorageImage -> IO ()
processImg = putStrLn . middlePixelMsg . middlePixel . convertImg
where
middlePixelMsg px
| isMoreRedThanGreen px = "Middle is more red than green."
| otherwise = "Middle is more green than red."
reportError :: StorageError -> IO ()
reportError err = putStrLn $ "Failed to open image: " ++ show err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment