JuicyPixels 无法加载 PNG 文件

JuicyPixels can't load PNG files

我正在尝试学习如何使用 JuicyPixels 3.2.5.1 版加载、修改和保存图像。我有以下代码:

{-# LANGUAGE OverloadedStrings #-}

import Codec.Picture

imageCreator :: String -> IO ()
imageCreator path = writePng path $ generateImage pixelRenderer 250 300
   where pixelRenderer x y = PixelRGB8 (fromIntegral x) (fromIntegral y) 128

loadSampleImage path = case decodePng path of
  Left errorMsg -> putStrLn errorMsg
  Right sampleImage -> putStrLn "Loaded Successfully"

main = do 
  imageCreator "test.png"
  loadSampleImage "test.png"

imageCreator 函数是对 JuicyPixels 文档稍作修改:https://hackage.haskell.org/package/JuicyPixels-3.2.5.1/docs/Codec-Picture.html 修改是添加对 fromIntegral 的两次调用,因为这个例子没有编译没有他们。 (我觉得文档中的一个示例无法编译,这对我来说似乎很奇怪,所以如果我做了一些愚蠢的事情,请告诉我)

运行 该程序将创建一个名为 "test.png" 的图像并打印:

Invalid PNG file, signature broken

这大概是来自对 decodePng 的调用的错误消息。我已经用其他几个 PNG 尝试过这个,一个是我在 Gimp 中创建的,另一个是我在 MS paint 中创建的。为此,我从主函数中删除了 imageCreator "test.png" 行,以避免覆盖我想要测试的图像。

我应该更改什么才能加载 PNG 图像?

loadSampleImage path = case decodePng path of

您正在尝试将字符串 "test.png" 逐字解码为 PNG。你想要 readPng,而不是 decodePng。 (或者您可以只使用 readImage 而不必关心文件的格式。)