Discussion:
[Gtk2hs-users] Example of using pixbufSave
Bram Neijt
2014-08-19 15:44:22 UTC
Permalink
Hi,

I'm a Haskell beginner and wanted to use gtk2hs to load, scale and
save a jpeg image.

The code I have so far is:

import Graphics.UI.Gtk.Gdk.Pixbuf
import System.Glib.UTFString
import qualified Data.Text as T

jpegImageFormat :: ImageFormat
jpegImageFormat = pixbufGetFormats !! 6

pixbufSaveJpeg :: Pixbuf -> FilePath -> IO()
pixbufSaveJpeg pb toPath =
pixbufSave pb toPath jpegImageFormat []

scaleFile :: FilePath -> FilePath -> IO ()
scaleFile fromPath toPath = do
original <- pixbufNewFromFile fromPath
scaled <- pixbufScaleSimple original 800 800 InterpHyper
putStrLn toPath
pixbufSaveJpeg scaled toPath

main = scaleFile "hanginthere.jpg" "scaledinthere.jpg"


The ghc 7.8.3 will complains with:

src/Main.hs:18:9:
No instance for (GlibString string0)
arising from a use of ‘pixbufSave’
The type variable ‘string0’ is ambiguous
Note: there are several potential instances:
instance GlibString T.Text -- Defined in ‘System.Glib.UTFString’
instance GlibString [Char] -- Defined in ‘System.Glib.UTFString’
In the expression: pixbufSave pb toPath jpegImageFormat []
In an equation for ‘pixbufSaveJpeg’:
pixbufSaveJpeg pb toPath = pixbufSave pb toPath jpegImageFormat []

How can I solve this? Are there any examples of using pixbufSave?

Greetings,

Bram
Antonin Delpeuch (lists)
2014-08-19 15:52:22 UTC
Permalink
Hi!

With GTK 3 (gtk2hs 0.12.5.7), this code compiles fine (at least on my
system). Which version of gtk2hs are you using?

Cheers,
Antonin
Post by Bram Neijt
import Graphics.UI.Gtk.Gdk.Pixbuf
import System.Glib.UTFString
import qualified Data.Text as T
jpegImageFormat :: ImageFormat
jpegImageFormat = pixbufGetFormats !! 6
pixbufSaveJpeg :: Pixbuf -> FilePath -> IO()
pixbufSaveJpeg pb toPath =
pixbufSave pb toPath jpegImageFormat []
scaleFile :: FilePath -> FilePath -> IO ()
scaleFile fromPath toPath = do
original <- pixbufNewFromFile fromPath
scaled <- pixbufScaleSimple original 800 800 InterpHyper
putStrLn toPath
pixbufSaveJpeg scaled toPath
main = scaleFile "hanginthere.jpg" "scaledinthere.jpg"
koral
2014-08-19 16:25:14 UTC
Permalink
Hello,

Since gtk2hs 0.13, every function that used to be fed with a String, can now be fed with either a String or a Text.
This was implemented using the typeclass GlibString that has 2 instances: String and Text.
In your case, GHC was unable to deduce alone which one to use for the last argument of pixbufSave: the value [] doesn't help disambiguating between [(String, String)] and [(Text, Text)].
To fix it, simply write the type explicitly:

pixbufSave pb toPath jpegImageFormat ([] :: [(T.Text, T.Text)])

Regards.

Loading...