Commit 88d48266 authored by Thilo Molitor's avatar Thilo Molitor

Better transformation from pil.image to gtk pixbuf (see last commit)

parent 3c704f95
......@@ -42,6 +42,7 @@ try:
except:
pass
from io import BytesIO
import array
import logging
log = logging.getLogger('gajim.gtkgui_helpers')
......@@ -417,39 +418,48 @@ def get_abspath_for_script(scriptname, want_type = False):
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-Memory.html#gdk-pixbuf-new-from-bytes
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()
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
im = Image.open(BytesIO(file_data)).convert("RGB")
pixbuf = pillow2pixbuf(im)
if want_type:
return None, None
typ = "png"
return pixbuf, typ
else:
return None
return pixbuf
except:
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:
typ = pixbufloader.get_format().get_name()
return pixbuf, typ
else:
return pixbuf
if want_type:
typ = pixbufloader.get_format().get_name()
return pixbuf, typ
else:
return pixbuf
def get_invisible_cursor():
import cairo
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment