Haskell在图像上绘制图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haskell在图像上绘制图像相关的知识,希望对你有一定的参考价值。

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

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

答案

您可以使用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

以上是关于Haskell在图像上绘制图像的主要内容,如果未能解决你的问题,请参考以下文章

在 Haskell 中使用 OpenGL 渲染 PNG 图像

在Haskell中使用OpenGL渲染PNG图像

在画布上绘制背景图像并保存图像

我想在背景图像上绘制可拖动的 JComponent

如何在haskell(光泽)中加载图像数组?

在 UIImage 上绘制另一个图像 -