convert PIL.Image to GdkPixbuf, then load into GtkImage -- crash Windows if image too large
Thanks in advance for any insight.
I have a project which seems to work perfectly on Linux but mysteriously crashes Windows (without any terminal output) if certain constants are sufficiently large, though still modest. I believe the following Python script isolates the issue:
from PIL import Image import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GdkPixbuf, GLib topgtkwindow = Gtk.Window( title = "TEST" ) topgtkwindow.connect( "destroy", Gtk.main_quit ) gtkimage = Gtk.Image() topgtkwindow.add( gtkimage ) topgtkwindow.show_all() pillowimage = Image.new( "RGB", (800,600) ) # Line A glibbytes = GLib.Bytes.new( pillowimage.tobytes() ) gdkpixbuf = GdkPixbuf.Pixbuf.new_from_data( glibbytes.get_data(), GdkPixbuf.Colorspace.RGB, False, 8, pillowimage.width, pillowimage.height, len( pillowimage.getbands() )*pillowimage.width, None, None ) # Line B gtkimage.set_from_pixbuf( gdkpixbuf ) # Line C Gtk.main()
If the (800,600) on Line A is replaced with sufficiently small pair, say (200,150), then there is no crash. However, the crashes are slightly random: for several executions (400,300) seemed to succeed but then crashed eventually.
The problem is not concerned only with total displayed size: if you start with an acceptable size and replace the argument "gdkpixbuf" on Line C with the scaled version "gdkpixbuf.scale_simple( 800, 600, GdkPixbuf.InterpType.NEAREST )" then there is no crash. Also, there is no crash if "GdkPixbuf.Pixbuf.new_from_data" on Line B is changed to "GdkPixbuf.Pixbuf.new_from_bytes" ("glibbytes.get_data()" must also change to "glibbytes").
some Windows details: Windows 10 Home, version 1709, build 16299.492, Python 2.7.15, PyGObject 3.24.1 (via https://sourceforge.net/projects/pygobjectwin32/), Pillow 5.1.0
I hesitate to declare this a bug with Windows and/or GDK because it seems that there is no smooth way to install PyGObject on Windows and maybe there is something wrong with my packages. Or maybe there is a subtlety I don't understand...