哪个haskell库会让我将2D数组/矢量保存到png / jpg / gif ...文件?

时间:2022-02-09 05:00:21

I am playing around with haskell, starting with simple plotting programs to wet my feet. I need a library that will let me save a 2D array/vector to an image file. I don't want to write a list of colors. I want to use containers that are meant for array/vector like computations and can be (well, almost) automagically parallelized.

我正在玩haskell,从简单的绘图程序开始弄湿我的脚。我需要一个库,让我将2D数组/矢量保存到图像文件中。我不想写一份颜色列表。我想使用用于数组/矢量计算的容器,并且可以(好吧,几乎)自动并行化。

EDIT Ability to store color images is a must.

编辑存储彩色图像的能力是必须的。

4 个解决方案

#1


8  

I'd start with PGM library. This is a very simple uncompressed graymap format. Almost no additinal dependencies. You can convert PGM to other formats with ImageMagick or other tools.

我从PGM库开始。这是一种非常简单的无压缩灰度图格式。几乎没有附加依赖。您可以使用ImageMagick或其他工具将PGM转换为其他格式。

PGM supports generic IArray interface, and should work with most of the standard Haskell arrays. You can easily parallelize array computations with Control.Parallel.Strategies.

PGM支持通用IArray接口,并且应该与大多数标准Haskell数组一起使用。您可以使用Control.Parallel.Strategies轻松地并行化数组计算。

PGM usage example:

PGM用法示例:

ghci> :m + Data.Array Graphics.Pgm 
ghci> let a = accumArray (+) 0 ((0::Int,0::Int),(127,127)) [ ((i,i), 1.0::Double) | i <- [0..127] ]
ghci> arrayToFile "t.pgm" (fmap round a)

And this is the image:

这是图像:

哪个haskell库会让我将2D数组/矢量保存到png / jpg / gif ...文件?

Otherwise you may use Codec-Image-DevIL which can save unboxed arrays to many of the image formats. You'll need DevIL library too. And you'll need to convert all arrays to that particular type of them (UArray (Int, Int, Int) Word8).

否则,您可以使用Codec-Image-DevIL,它可以将未装箱的阵列保存为许多图像格式。你也需要DevIL库。并且您需要将所有数组转换为它们的特定类型(UArray(Int,Int,Int)Word8)。

Finally, if you want bleeding edge, you may consider repa parallel arrays and corresponding repa-io library, which can write them to BMP images. Unfortunately, today repa is not yet buildable with the new GHC 7.0.2 and doesn't give performance advantages on old GHC 6.12.

最后,如果你想要前沿,你可以考虑修复并行数组和相应的repa-io库,它们可以将它们写入BMP图像。不幸的是,今天使用新的GHC 7.0.2尚未建立修复,并且在旧的GHC 6.12上没有性能优势。

#2


6  

A new combination is:

一个新的组合是:

  • repa; for n-dimensional arrays, plus
  • repA的;对于n维数组,加上
  • repa-devil, for image loading in dozens of formats.
  • repa-devil,用于数十种格式的图像加载。

Repa is the only widely used array library that is automatically parallelized.

Repa是唯一广泛使用的自动并行化的数组库。

An example, from the repa tutorial, using readImage and writeImage, to read an image, rotate it, and write it back out, in whatever format:

一个例子,来自repa教程,使用readImage和writeImage,以任何格式读取图像,旋转图像并将其写回:

import System.Environment
import Data.Word
import Data.Array.Repa hiding ((++))
import Data.Array.Repa.IO.DevIL

main = do
    [f] <- getArgs
    runIL $ do
        v   <- readImage f
        writeImage ("flip-"++f) (rot180 v)

rot180 :: Array DIM3 Word8 -> Array DIM3 Word8
rot180 g = backpermute e flop g
    where
        e@(Z :. x :. y :. _)   = extent g

        flop (Z :. i         :. j         :. k) =
             (Z :. x - i - 1 :. y - j - 1 :. k)

#3


2  

The more recent JuicyPixels library let you save image to Jpg/Png/Tiff easily, you can use it in combination with Repa with the JuicyPixels-repa library.

最近的JuicyPixels库让您可以轻松地将图像保存到Jpg / Png / Tiff,您可以将它与使用JuicyPixels-repa库的Repa结合使用。

#4


1  

You might also want to check out Diagrams

您可能还想查看图表

Example code for the dragon fractal:

龙分形的示例代码:

{- Heighway dragon.  See http://en.wikipedia.org/wiki/Dragon_curve. -}
module Main where

import Graphics.Rendering.Diagrams
import Control.Monad.State
import Data.Maybe

dragonStr :: Int -> String
dragonStr 0 = "FX"
dragonStr n = concatMap rules $ dragonStr (n-1)
  where rules 'X' = "X+YF+"
        rules 'Y' = "-FX-Y"
        rules c = [c]

strToPath :: String -> Path
strToPath s = pathFromVectors . catMaybes $ evalState c (0,-1)
  where c        = mapM exec s
        exec 'F' = Just `fmap` get
        exec '-' = modify left >> return Nothing
        exec '+' = modify right >> return Nothing
        exec _   = return Nothing
        left (x,y)  = (-y,x)
        right (x,y) = (y,-x)

dragon :: Int -> Diagram
dragon = lc red . curved 0.8 . strToPath . dragonStr

main = renderAs PNG "dragon.png" (Width 300) (dragon 12)

#1


8  

I'd start with PGM library. This is a very simple uncompressed graymap format. Almost no additinal dependencies. You can convert PGM to other formats with ImageMagick or other tools.

我从PGM库开始。这是一种非常简单的无压缩灰度图格式。几乎没有附加依赖。您可以使用ImageMagick或其他工具将PGM转换为其他格式。

PGM supports generic IArray interface, and should work with most of the standard Haskell arrays. You can easily parallelize array computations with Control.Parallel.Strategies.

PGM支持通用IArray接口,并且应该与大多数标准Haskell数组一起使用。您可以使用Control.Parallel.Strategies轻松地并行化数组计算。

PGM usage example:

PGM用法示例:

ghci> :m + Data.Array Graphics.Pgm 
ghci> let a = accumArray (+) 0 ((0::Int,0::Int),(127,127)) [ ((i,i), 1.0::Double) | i <- [0..127] ]
ghci> arrayToFile "t.pgm" (fmap round a)

And this is the image:

这是图像:

哪个haskell库会让我将2D数组/矢量保存到png / jpg / gif ...文件?

Otherwise you may use Codec-Image-DevIL which can save unboxed arrays to many of the image formats. You'll need DevIL library too. And you'll need to convert all arrays to that particular type of them (UArray (Int, Int, Int) Word8).

否则,您可以使用Codec-Image-DevIL,它可以将未装箱的阵列保存为许多图像格式。你也需要DevIL库。并且您需要将所有数组转换为它们的特定类型(UArray(Int,Int,Int)Word8)。

Finally, if you want bleeding edge, you may consider repa parallel arrays and corresponding repa-io library, which can write them to BMP images. Unfortunately, today repa is not yet buildable with the new GHC 7.0.2 and doesn't give performance advantages on old GHC 6.12.

最后,如果你想要前沿,你可以考虑修复并行数组和相应的repa-io库,它们可以将它们写入BMP图像。不幸的是,今天使用新的GHC 7.0.2尚未建立修复,并且在旧的GHC 6.12上没有性能优势。

#2


6  

A new combination is:

一个新的组合是:

  • repa; for n-dimensional arrays, plus
  • repA的;对于n维数组,加上
  • repa-devil, for image loading in dozens of formats.
  • repa-devil,用于数十种格式的图像加载。

Repa is the only widely used array library that is automatically parallelized.

Repa是唯一广泛使用的自动并行化的数组库。

An example, from the repa tutorial, using readImage and writeImage, to read an image, rotate it, and write it back out, in whatever format:

一个例子,来自repa教程,使用readImage和writeImage,以任何格式读取图像,旋转图像并将其写回:

import System.Environment
import Data.Word
import Data.Array.Repa hiding ((++))
import Data.Array.Repa.IO.DevIL

main = do
    [f] <- getArgs
    runIL $ do
        v   <- readImage f
        writeImage ("flip-"++f) (rot180 v)

rot180 :: Array DIM3 Word8 -> Array DIM3 Word8
rot180 g = backpermute e flop g
    where
        e@(Z :. x :. y :. _)   = extent g

        flop (Z :. i         :. j         :. k) =
             (Z :. x - i - 1 :. y - j - 1 :. k)

#3


2  

The more recent JuicyPixels library let you save image to Jpg/Png/Tiff easily, you can use it in combination with Repa with the JuicyPixels-repa library.

最近的JuicyPixels库让您可以轻松地将图像保存到Jpg / Png / Tiff,您可以将它与使用JuicyPixels-repa库的Repa结合使用。

#4


1  

You might also want to check out Diagrams

您可能还想查看图表

Example code for the dragon fractal:

龙分形的示例代码:

{- Heighway dragon.  See http://en.wikipedia.org/wiki/Dragon_curve. -}
module Main where

import Graphics.Rendering.Diagrams
import Control.Monad.State
import Data.Maybe

dragonStr :: Int -> String
dragonStr 0 = "FX"
dragonStr n = concatMap rules $ dragonStr (n-1)
  where rules 'X' = "X+YF+"
        rules 'Y' = "-FX-Y"
        rules c = [c]

strToPath :: String -> Path
strToPath s = pathFromVectors . catMaybes $ evalState c (0,-1)
  where c        = mapM exec s
        exec 'F' = Just `fmap` get
        exec '-' = modify left >> return Nothing
        exec '+' = modify right >> return Nothing
        exec _   = return Nothing
        left (x,y)  = (-y,x)
        right (x,y) = (y,-x)

dragon :: Int -> Diagram
dragon = lc red . curved 0.8 . strToPath . dragonStr

main = renderAs PNG "dragon.png" (Width 300) (dragon 12)