Haskell在图像上绘制图像

时间:2017-12-26 11:56:24

标签: haskell graphics

我想拍摄两张不同的图像(取自图像文件,如.png),并在不同位置多次绘制一张图像。生成的图像应显示在屏幕上或生成新的图像文件,以较容易的方式。我将通过进一步的操作来拍摄新图像并进一步绘制

是否有任何Haskell库允许我这样做?

1 个答案:

答案 0 :(得分:1)

您可以使用JuicyPixels执行此类操作:

module Triangles where

import Codec.Picture
import LineGraphics

{-| Parameterize color smoothly as a function of angle -}
colorWheel :: Float -> Colour
colorWheel x = (r, g, b, a)
  where
    r = floor $ (cos x + 1) * (255 / 2)
    g = floor $ (sin x + 1) * (255 / 2)
    b = floor $ (cos (x+(pi/2)) + 1) * (255 / 2)
    a = 255

{-| Draw a triangle centered about the point (x, y) -}
triangle :: Point -> Path
triangle (x, y) =
    [ (x - k, y - k)
    , (x + k, y - k)
    , (x, y + k)
    , (x - k, y - k)
    ]
  where
    size = 30
    k = size / 2

{-|
  Draw 'n' equally-spaced triangles at a radius of 'r' about a center
  point, '(x, y)'.
-}
triangles :: Float -> Radius -> Vector -> Picture
triangles n r (x, y) =
    [ (colorWheel theta, tri theta) | theta <- steps n ]
  where
    tri theta = triangle ((r * cos theta) + x, (r * sin theta) + y)

{-| Interpolate the range [0, 2pi] by 'n' steps -}
steps :: Float -> [Float]
steps n = map (\i -> i * (2*pi/n)) [0 .. n]

我们将使用此支持代码模块:

module LineGraphics (
    Point, Vector, Line, Path, Picture, Colour, Radius,
    black,
    drawPicture,
) where

import Graphics.Rasterific hiding (Point, Vector, Line, Path, polygon)
import Graphics.Rasterific.Texture
import Codec.Picture

type Radius  = Float
type Point   = (Float, Float)
type Vector  = (Float, Float)
type Line    = (Point, Point)
type Path    = [Point]
type Picture = [(Colour, Path)]
type Colour  = (Int, Int, Int, Int) -- red, green, blue, opacity

black = (0, 0, 0, 255)

drawPicture :: Float -> Picture -> Image PixelRGBA8
drawPicture linewidth picture =
    renderDrawing  800 800 (toColour black) $
        mapM_ renderFn picture
  where
    renderFn (col, path) = withTexture (uniformTexture $ toColour col) (drawPath path)
    drawPath points    = stroke linewidth  JoinRound (CapRound, CapStraight 0) $
        polyline (map (\(x, y) -> V2 x y) points)
    toColour (a,b,c,d) = PixelRGBA8
        (fromIntegral a) (fromIntegral b) (fromIntegral c) (fromIntegral d)

以下是我们得到的: The rendered image