Juicy Pixels is a great package for loading and saving images, and I often use it in little command line tools for munging images. However, over time I find those scripts have rather too much common code for comfort.
This module is an attempt to abstract that common boilerplate code.
Here is the complete source for a program to reverse the bit-order of each image on the command line. For example, foo.gif will be bit-reversed and saved as foo-f.png
import Toy.JuicyPixels import Codec.Picture import Data.Word import Data.Bits import qualified Data.List as L main = transformImagesInArgsPNG flipImage (++ "-f") flipImage :: ImageRGB8 -> ImageRGB8 flipImage = pixelMap (liftRGB flipByte) flipByte :: Word8 -> Word8 flipByte = memoizeWord8 flipByte' flipByte' :: Word8 -> Word8 flipByte' x = L.foldl' setBit 0 $ [ 7 - i | i <- [0..7], testBit x i ]
The next example illustrates writing to stdout rather than files. It prints the frequency with which pixels are seen in each image on the command line: the results are essentially useless for photos, but perhaps useful with little icons.
import Toy.JuicyPixels import Codec.Picture import qualified Data.List as L import Text.Printf main = describeImagesInArgs countPixels countPixels :: ImageRGB8 -> String countPixels = concatMap pp . freqs . pixelList freqs :: (Ord a, Eq a) => [a] -> [(a,Int)] freqs = map (ps -> (head ps, length ps)) . L.group . L.sort pp (PixelRGB8 r g b, n) = printf "%3d %3d %3d: %8dn" r g b n