Skip to content

Instantly share code, notes, and snippets.

@chrisnorris
Created September 11, 2019 10:04
Show Gist options
  • Save chrisnorris/e0d72f4e32fe1991dde5435085c324d6 to your computer and use it in GitHub Desktop.
Save chrisnorris/e0d72f4e32fe1991dde5435085c324d6 to your computer and use it in GitHub Desktop.
haskell script to asciify image, scaling is next step
#!/usr/bin/env stack
{- stack
--resolver lts-14.5
--install-ghc
exec ghci
--package JuicyPixels
--package lens
--package split
-}
{-# LANGUAGE ScopedTypeVariables #-}
import Codec.Picture
import Codec.Picture.Metadata
import Codec.Picture.Types
import Control.Lens
import Control.Lens.Prism
import Control.Monad
import Data.Monoid
import Data.List.Split(chunksOf)
main = readImage "poggers.png" >>= asciify
where
asciify (Left errorMsg) = putStrLn $ "error: " <> errorMsg
asciify (Right dynamicImage) =
let rgb8 = pixelMap gray $ convertRGB8 dynamicImage in
forM_ (chunksOf (imageWidth rgb8) (rgb8 ^.. imagePixels)) (putStrLn . concatMap grayRamp)
gray = computeLuma :: PixelRGB8 -> Pixel8
grayRamp (i :: Pixel8) =
let offset = round (fromIntegral i / 256 * 69) :: Int
in "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'." ^.. traversed.index offset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment