GNOME Bugzilla – Bug 566862
pixbuf_new_from_file does not autodetect format
Last modified: 2010-07-10 04:08:44 UTC
Please describe the problem: If I take a png file and rename it to *.jpg, pixbuf_new_from_file tries to load the image as a jpeg image instead of autodetecting that it is a png file. Using pygtk: >>> import gtk >>> f = gtk.gdk.pixbuf_new_from_file("/home/stonecrest/images/archlinux.png") >>> f = gtk.gdk.pixbuf_new_from_file("/home/stonecrest/images/archlinux.png.jpg") Traceback (most recent call last):
+ Trace 211337
>>> I'm quite certain this has worked correctly in previous gtk versions, although I'm not sure when it broke. Furthermore, the gtk documentation explicitly states that this function should detect the function. From http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-file-loading.html#gdk-pixbuf-new-from-file: "The file format is detected automatically." Steps to reproduce: Actual results: Expected results: Does this happen every time? Yes Other information: Tested using pygtk 2.13.0 on ArchLinux.
"this function should detect the function" - sorry, that should read "detect the format." And just for clarification $ file ~/images/archlinux.png.jpg /home/stonecrest/images/archlinux.png.jpg: PNG image, 1024 x 768, 8-bit grayscale, non-interlaced
Hello there, I had a look on gdk-pixbuf-io.c and especially the function _gdk_pixbuf_get_module (). This function uses g_content_type_guess () to determine the mime type of a file. I started the python interpreter and tested the function gio.content_type_guess () and it turned out that it uses the ending to determine the mime type. This is also true for the function mimetypes.guess_type (). This behaviour is also true for the following test program in C (attached). So maybe we should pass this bug to GIO or clarify the documentation.
Created attachment 125934 [details] A test program to obtain the mime type of a file This program uses g_content_type_guess () to get the mime type.
2009-01-07 Matthias Clasen <mclasen@redhat.com> Bug 566862 – pixbuf_new_from_file does not autodetect format * gdk-pixbuf-io.c (_gdk_pixbuf_get_module): Go back to sniffing without looking at the filename, to avoid breaking expected functionality.
SVN revision #22073 causes regression/breakage...see Bug #569412. I'd agree with Comment #2: let g_content_type_guess() have whatever data it can accept (i.e., "if there's a filename parameter, pass the filename"). If it should only look at content not name per its documentation, then that's an internal bug *there* not for the caller to work around. The work-around makes things worse too, because error messages don't have the filename so it's hard to figure out which file can't be loaded. Regarding how it *should* work, I don't see anything in the documentation statement "Creates a new pixbuf by loading an image from a file. The file format is detected automatically." that would restrict detection to the file's contents only (cf. filename). Conversely, g_content_type_guess() documentation is quite explicit that filename *is* usable for content-type analysis: "Guesses the content type based on example data. If the function is uncertain, result_uncertain will be set to TRUE. Either filename or data may be NULL, in which case the guess will be based solely on the other argument." I don't know the original intended behavior of pixbuf_new_from_file(), but it seems inconsistent to ignore filename in general, and especially given that another part explicitly uses filename.