Coverage for /home/antoine/projects/xpra-git/dist/python3/lib64/python/xpra/notifications/common.py : 46%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of Xpra.
2# Copyright (C) 2018-2019 Antoine Martin <antoine@xpra.org>
3# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
4# later version. See the file COPYING for details.
6import os.path
7from io import BytesIO
9from xpra.log import Logger
11log = Logger("dbus", "notify")
14def parse_image_data(data):
15 try:
16 width, height, rowstride, has_alpha, bpp, channels, pixels = data
17 log("parse_image_data(%i, %i, %i, %s, %i, %i, %i bytes)",
18 width, height, rowstride, bool(has_alpha), bpp, channels, len(pixels))
19 from PIL import Image
20 if channels==4:
21 rgb_format = "BGRA"
22 fmt = "RGBA"
23 elif channels==3:
24 rgb_format = "BGR"
25 fmt = "RGB"
26 img = Image.frombytes(fmt, (width, height), pixels, "raw", rgb_format, rowstride)
27 if channels==4 and not has_alpha:
28 img = img.convert("RGB")
29 return image_data(img)
30 except Exception as e:
31 log("parse_image_data(%s)", data, exc_info=True)
32 log.error("Error parsing icon data for notification:")
33 log.error(" %s", e)
34 return None
36def parse_image_path(path):
37 if path and os.path.exists(path):
38 try:
39 from PIL import Image
40 img = Image.open(path)
41 return image_data(img)
42 except Exception as e:
43 log.error("Error parsing image path '%s' for notification:", path)
44 log.error(" %s", e)
45 return None
47def image_data(img):
48 buf = BytesIO()
49 img.save(buf, "png")
50 data = buf.getvalue()
51 buf.close()
52 w,h = img.size
53 return ("png", w, h, data)