Tuesday, 3 September 2013

How do I turn an Image into a matrix of colours?

How do I turn an Image into a matrix of colours?

I'm playing with Graphics.GD and I'd like to read an image into a matrix
of Color values, a bit like this:
rectFromImage :: Image -> IO [[Color]]
rectFromImage img = do
size <- imageSize img
return [[getPixel (x,y) img | x <- [1 .. fst size]] | y <- [1 .. snd
size]]
Obviously, this does not work because getPixel returns IO Color, not Color:
Couldn't match type `IO Color' with `Foreign.C.Types.CInt'
Expected type: Color
Actual type: IO Color
In the return type of a call of `getPixel'
In the expression: getPixel (x, y) img
In the expression: [getPixel (x, y) img | x <- [1 .. fst size]]
How do I "get rid of the IO" in the return of the getPixel call?

No comments:

Post a Comment