How do I load an image into a List?

haskell, image-processing

Solution

Using JuicyPixels-repa this is simple and doesn't require any external (C) libraries:

import Codec.Picture.Repa (readImageRGBA, collapseColorChannel)
import Data.Array.Repa as R
import Data.Word

loadImage :: FilePath -> IO [(Word8,Word8,Word8,Word8)]
loadImage fp = do
    img <- either error return =<< readImageRGBA fp
    let arr = collapseColorChannel img
    return $ R.toList arr

Or in a more point-free style:

loadImage = fmap (R.toList . collapseColorChannel . either error id) . readImageRGBA

(Note all this code is typed, not tested. Feel free to shout with any issues)

Truth in advertising: I maintain JP-repa.

Problem

I am using Haskell and attempting to write a function ``` loadImage :: FilePath -> IO [RGBAPixel] loadImage = ... type RBGAPixel = (Double, Double, Double, Double) ``` I realize that lists aren't the most efficient way to do this - but I'm just looking to shoehorn something into my existing structure for the moment. What's the easiest way to load an image (.jpg, .bmp, .png, or .tga) into a list in Haskell?

Original source