changeset 8c4df591f2d5 in /home/hg/repos/gajim
author: tmolitor thilo@eightysoft.de branches: details:http://hg.gajim.org/gajim?cmd=changeset;node=8c4df591f2d5 description: Better transformation from pil.image to gtk pixbuf (see last commit)
diffstat:
src/gtkgui_helpers.py | 56 ++++++++++++++++++++++++++++++-------------------- 1 files changed, 33 insertions(+), 23 deletions(-)
diffs (83 lines):
diff -r bb1902ae63e3 -r 8c4df591f2d5 src/gtkgui_helpers.py --- a/src/gtkgui_helpers.py Tue Apr 05 18:59:55 2016 +0200 +++ b/src/gtkgui_helpers.py Wed Apr 06 23:38:30 2016 +0200 @@ -42,6 +42,7 @@ except: pass from io import BytesIO +import array
import logging log = logging.getLogger('gajim.gtkgui_helpers') @@ -417,39 +418,48 @@ else: return path_to_script
+# see http://stackoverflow.com/a/8892894/3528174 +# and https://developer.gnome.org/gdk-pixbuf/unstable/gdk-pixbuf-Image-Data-in-Mem... +def pillow2pixbuf(im): + arr = array.array('B', im.tobytes()) + width, height = im.size + return GdkPixbuf.Pixbuf.new_from_data(arr, GdkPixbuf.Colorspace.RGB, + False, 8, width, height, width * 3) + def get_pixbuf_from_data(file_data, want_type = False): """ Get image data and returns GdkPixbuf.Pixbuf if want_type is True it also returns 'jpeg', 'png' etc """ - pixbufloader = GdkPixbuf.PixbufLoader() # try to open and convert every image format supported by PILLOW to png format try: - avatar = Image.open(BytesIO(file_data)).convert("RGB") - output = BytesIO() - avatar.save(output, format='PNG') - file_data = output.getvalue() - output.close() + im = Image.open(BytesIO(file_data)).convert("RGB") + pixbuf = pillow2pixbuf(im) + if want_type: + typ = "png" + return pixbuf, typ + else: + return pixbuf except: - log.debug("Could not use pillow to convert avatar image (this is non fatal)") - pass # this didn't work, so just use the old gtk code to import the image - try: - pixbufloader.write(file_data) - pixbufloader.close() - pixbuf = pixbufloader.get_pixbuf() - except GLib.GError: # 'unknown image format' - pixbufloader.close() - pixbuf = None + log.info("Could not use pillow to convert avatar image to pixbuf, trying pixbufloader instead...") + pixbufloader = GdkPixbuf.PixbufLoader() + try: + pixbufloader.write(file_data) + pixbufloader.close() + pixbuf = pixbufloader.get_pixbuf() + except GLib.GError: # 'unknown image format' + pixbufloader.close() + pixbuf = None + if want_type: + return None, None + else: + return None + if want_type: - return None, None + typ = pixbufloader.get_format().get_name() + return pixbuf, typ else: - return None - - if want_type: - typ = pixbufloader.get_format().get_name() - return pixbuf, typ - else: - return pixbuf + return pixbuf
def get_invisible_cursor(): import cairo