使用“hip”Haskell库像素化制作图像

eaf3rand  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(90)

我正在尝试用Haskell的hip库制作一个图像。下面的代码在ghci中运行时运行良好:

let grad_color = makeImageR VU (200, 200) (\(i, j) -> PixelRGB (fromIntegral i) (fromIntegral j) (fromIntegral (i + j))) / 400

writeImage "images/grad_color.png" grad_color

现在,我想分离出构造图像的函数。我试过了(我的尝试有点随机,因为我迷路了)::

fun :: (Int, Int) -> Pixel RGB Word8
fun (i, j) = PixelRGB (fromIntegral i) / 200 * (fromIntegral j) / 200 (fromIntegral (i + j))) / 400

但是这个函数不能编译。最终的目标是把碎片拼在一起:

gradColor :: (Int, Int) -> ????
gradColor (m, n) = makeImageR VU (m, n) fun

我知道有很多错误,但我又一次迷失了签名。

dly7yett

dly7yett1#

好吧,我设法制作了一个例子。这是比200/400更复杂的,抱歉我的问题中有括号。

module TestHip
    (colorFun
    , myImage
    , gradientColor
    , saveImage
    ) where
import Graphics.Image
    ( makeImageR, writeImage, RGB, Image, Pixel(PixelRGB), VU(..) )

colorFun :: (Int, Int) -> Pixel RGB Double
colorFun (i, j) =
  PixelRGB (fromIntegral i) (fromIntegral j) (fromIntegral (i + j)) / 400

myImage :: ((Int, Int) -> Pixel RGB Double) -> (Int, Int) -> Image VU RGB Double
myImage thefun (m, n) = makeImageR VU (m, n) thefun

gradientColor :: Image VU RGB Double
gradientColor = myImage colorFun (400, 400)

saveImage :: IO ()
saveImage = writeImage "images/mygradient.png" gradientColor

运行`saveImage ',您将得到以下图像:

编辑:一个漂亮的

相关问题