JuicyPixels can't load PNG files

479 Views Asked by At

I'm trying to learn how load, modify and save images using JuicyPixels version 3.2.5.1. I have the following code:

{-# 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"

The imageCreator function is taken with a slight modification from the JuicyPixels documentation: https://hackage.haskell.org/package/JuicyPixels-3.2.5.1/docs/Codec-Picture.html The modification was the addition of the two calls to fromIntegral, as this example did not compile without them. (It seems strange to me that an example in the documentation wouldn't compile, so if I'm doing something stupid, please let me know)

Running this program will create an image called "test.png" and will print:

Invalid PNG file, signature broken

which is presumably the error message coming from the call to decodePng. I have tried this with a few other PNGs, one that I created in Gimp, and another that I created in MS paint. I did so by removing the imageCreator "test.png" line from my main function, to avoid overwriting the images I wanted to test.

What should I change in order to load PNG images?

1

There are 1 best solutions below

0
On BEST ANSWER
loadSampleImage path = case decodePng path of

You're trying to literally decode the string "test.png" as a PNG. You want readPng, not decodePng. (Or you can just use readImage and not care about what format the file is in.)