GdkPixbuf returns "gi._glib.GError: Couldn't recognize the image file format"

4.4k Views Asked by At

I have a problem loading PNG images in Gtk3. I have broken image symbols in my ToolButtons and after some investigation it appears that it comes from GdkPixbuf not being able to read the PNG files.

I have reproduced the problem with the python console

>>> from gi.repository import GdkPixbuf
>>> print(GdkPixbuf)
<gi.module.DynamicModule 'GdkPixbuf' from '/home/user1/ctcils/dusserm/applications/gobject-introspection/1.40.0/lib/girepository-1.0/GdkPixbuf-2.0.typelib'>
>>> GdkPixbuf.Pixbuf.new_from_file("/home/user1/ctcils/dusserm/applications/pycharm-community-3.4.1/bin/pycharm.png")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gi._glib.GError: Couldn't recognize the image file format for file '/home/user1/ctcils/dusserm/applications/pycharm-community-3.4.1/bin/pycharm.png'

Many of the problems I faced in the last days were due to the fact we had to compile Gtk3, pygobject and all their dependencies and to install them in non-standard directories. I suspect a problem with the gobject introspection (the last from a long list).

3

There are 3 best solutions below

0
On

I got a similar error with unusual prefix, and solved it by setting XDG_DATA_DIRS, which I didn't have set.

export XDG_DATA_DIRS=.../usr/share

Source: https://bugs.gentoo.org/644136

1
On

It seems that the problem was due to the PNG library itself. I am quite surprised considering that it comes from a regular CentOS rpm libpng-1.2.49-1.el6_2.x86_64 and that PNG is a stable standard for ages. In addition we did not have the problem with GTK2 that was using the same lib I suppose...

Anyway, I compiled a bright new libpng 1.6.2 from the sources, I re-configured and re-installed gdk-pixbuf and now it works.

3
On

Not a full answer but some debugging tips: Check what image loaders gdk-pixbuf is providing (see also What image formats are supported by Gdk-Pixbuf (Gtk-Image?) by Default?). A Python snippet to check this:

from gi.repository import GdkPixbuf
for fmt in GdkPixbuf.Pixbuf.get_formats():
    print(fmt.get_extensions())

If nothing shows up, gdk-pixbuf is not finding any loaders which probably has something to do with the install location (--prefix and/or --libdir configure options). Verify you have loaders installed into the location gdk-pixbuf expects to find them (especially the png loader). This should be something like: <prefix>/lib[64]/gdk-pixbuf-2.0/<version>/loaders

See also: https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-query-loaders.html