How to upload an image to the list?

I am using Haskell and trying to write a function

loadImage :: FilePath -> IO [RGBAPixel]
loadImage = ...
type RBGAPixel = (Double, Double, Double, Double)

I understand that lists are not the most efficient way to do this, but I'm just looking to draw something in my existing structure at the moment. What is the easiest way to upload an image (.jpg, .bmp, .png or .tga) to a list in Haskell?

+3
source share
2 answers

Using JuicyPixels-repa , it is simple and does not 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 quieter style:

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

(Please note that all of this code is printed, not verified. Feel free to scream with any problems)

True in advertising: I support JP-repa.

+4
source

Haskell, JuicyPixels -, :

>>> img <- readImageRGB "image.jpg"
>>> :t (toLists img)
(toLists img) :: [[Pixel RGB Double]]  

, , .

0

All Articles