Better transformation from pil.image to gtk pixbuf (see last commit)
This commit is contained in:
parent
3c704f95da
commit
88d4826693
|
@ -42,6 +42,7 @@ try:
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
import array
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger('gajim.gtkgui_helpers')
|
log = logging.getLogger('gajim.gtkgui_helpers')
|
||||||
|
@ -417,39 +418,48 @@ def get_abspath_for_script(scriptname, want_type = False):
|
||||||
else:
|
else:
|
||||||
return path_to_script
|
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):
|
def get_pixbuf_from_data(file_data, want_type = False):
|
||||||
"""
|
"""
|
||||||
Get image data and returns GdkPixbuf.Pixbuf if want_type is True it also
|
Get image data and returns GdkPixbuf.Pixbuf if want_type is True it also
|
||||||
returns 'jpeg', 'png' etc
|
returns 'jpeg', 'png' etc
|
||||||
"""
|
"""
|
||||||
pixbufloader = GdkPixbuf.PixbufLoader()
|
|
||||||
# try to open and convert every image format supported by PILLOW to png format
|
# try to open and convert every image format supported by PILLOW to png format
|
||||||
try:
|
try:
|
||||||
avatar = Image.open(BytesIO(file_data)).convert("RGB")
|
im = Image.open(BytesIO(file_data)).convert("RGB")
|
||||||
output = BytesIO()
|
pixbuf = pillow2pixbuf(im)
|
||||||
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
|
|
||||||
if want_type:
|
if want_type:
|
||||||
return None, None
|
typ = "png"
|
||||||
|
return pixbuf, typ
|
||||||
else:
|
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:
|
if want_type:
|
||||||
typ = pixbufloader.get_format().get_name()
|
typ = pixbufloader.get_format().get_name()
|
||||||
return pixbuf, typ
|
return pixbuf, typ
|
||||||
else:
|
else:
|
||||||
return pixbuf
|
return pixbuf
|
||||||
|
|
||||||
def get_invisible_cursor():
|
def get_invisible_cursor():
|
||||||
import cairo
|
import cairo
|
||||||
|
|
Loading…
Reference in New Issue