def set_icon_from_data(self, pixels, has_alpha, w, h, rowstride, _options=None): self.clean_last_tmp_icon() #use a temporary file (yuk) from xpra.gtk_common.gtk_util import pixbuf_save_to_memory, get_pixbuf_from_data tray_icon = get_pixbuf_from_data(pixels, has_alpha, w, h, rowstride) png_data = pixbuf_save_to_memory(tray_icon) tmp_dir = osexpand(get_xpra_tmp_dir()) if not os.path.exists(tmp_dir): os.mkdir(tmp_dir, 0o755) fd = None try: fd, self.tmp_filename = tempfile.mkstemp(prefix="tray", suffix=".png", dir=tmp_dir) log("set_icon_from_data%s using temporary file %s", ("%s pixels" % len(pixels), has_alpha, w, h, rowstride), self.tmp_filename) os.write(fd, png_data) except OSError as e: log("error saving temporary file", exc_info=True) log.error("Error saving icon data to temporary file") log.error(" %s", e) return finally: if fd: os.fchmod(fd, 0o644) os.close(fd) self.do_set_icon_from_file(self.tmp_filename)
def load_icon_from_file(filename): if filename.endswith("xpm"): try: from xpra.gtk_common.gobject_compat import import_pixbufloader from xpra.gtk_common.gtk_util import pixbuf_save_to_memory data = load_binary_file(filename) loader = import_pixbufloader()() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() pngicondata = pixbuf_save_to_memory(pixbuf, "png") return pngicondata, "png" except Exception as e: log("pixbuf error loading %s", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) #try PIL: from PIL import Image try: img = Image.open(filename) except Exception as e: log("Image.open(%s)", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) return None buf = BytesIOClass() img.save(buf, "PNG") pngicondata = buf.getvalue() buf.close() return pngicondata, "png" icondata = load_binary_file(filename) if not icondata: return None log("got icon data from '%s': %i bytes", filename, len(icondata)) return icondata, os.path.splitext(filename)[1].rstrip(".")
def take_png_screenshot(window): log("grabbing screenshot") w,h = window.get_geometry()[2:4] pixbuf = get_pixbuf_from_window(window, 0, 0, w, h) data = pixbuf_save_to_memory(pixbuf, "png") rowstride = w*3 return w, h, "png", rowstride, data
def load_icon_from_file(filename): log("load_icon_from_file(%s)", filename) if filename.endswith("xpm"): from PIL import Image try: img = Image.open(filename) buf = BytesIO() img.save(buf, "PNG") pngicondata = buf.getvalue() buf.close() return pngicondata, "png" except ValueError as e: log("Image.open(%s)", filename, exc_info=True) except Exception as e: log("Image.open(%s)", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) #fallback to PixbufLoader: try: from xpra.gtk_common.gtk_util import pixbuf_save_to_memory data = load_binary_file(filename) from gi.repository import GdkPixbuf loader = GdkPixbuf.PixbufLoader() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() pngicondata = pixbuf_save_to_memory(pixbuf, "png") return pngicondata, "png" except Exception as e: log("pixbuf error loading %s", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) icondata = load_binary_file(filename) if not icondata: return None if filename.endswith("svg") and len(icondata)>MAX_ICON_SIZE//2: #try to resize it size = len(icondata) pngdata = svg_to_png(filename, icondata) if pngdata: log("reduced size of SVG icon %s, from %i bytes to %i bytes as PNG", filename, size, len(pngdata)) icondata = pngdata filename = filename[:-3]+"png" log("got icon data from '%s': %i bytes", filename, len(icondata)) if len(icondata)>MAX_ICON_SIZE and first_time("icon-size-warning-%s" % filename): global large_icons large_icons.append((filename, len(icondata))) return icondata, os.path.splitext(filename)[1].lstrip(".")
def set_icon_from_data(self, pixels, has_alpha, w, h, rowstride, _options=None): self.clean_last_tmp_icon() #use a temporary file (yuk) from xpra.gtk_common.gtk_util import COLORSPACE_RGB, pixbuf_new_from_data, pixbuf_save_to_memory import tempfile tmp_dir = osexpand(get_xpra_tmp_dir()) if not os.path.exists(tmp_dir): os.mkdir(tmp_dir, 0o755) self.tmp_filename = tempfile.mkstemp(prefix="tray", suffix=".png", dir=tmp_dir)[1] log("set_icon_from_data%s using temporary file %s", ("%s pixels" % len(pixels), has_alpha, w, h, rowstride), self.tmp_filename) tray_icon = pixbuf_new_from_data(pixels, COLORSPACE_RGB, has_alpha, 8, w, h, rowstride) png_data = pixbuf_save_to_memory(tray_icon) with open(self.tmp_filename, "wb") as f: f.write(png_data) self.do_set_icon_from_file(self.tmp_filename)
def load_icon_from_file(filename): log("load_icon_from_file(%s)", filename) if filename.endswith("xpm"): try: from xpra.gtk_common.gobject_compat import import_pixbufloader from xpra.gtk_common.gtk_util import pixbuf_save_to_memory data = load_binary_file(filename) loader = import_pixbufloader()() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() pngicondata = pixbuf_save_to_memory(pixbuf, "png") return pngicondata, "png" except Exception as e: log("pixbuf error loading %s", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) #try PIL: from PIL import Image try: img = Image.open(filename) except Exception as e: log("Image.open(%s)", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) return None buf = BytesIO() img.save(buf, "PNG") pngicondata = buf.getvalue() buf.close() return pngicondata, "png" icondata = load_binary_file(filename) if not icondata: return None log("got icon data from '%s': %i bytes", filename, len(icondata)) if len(icondata) > MAX_ICON_SIZE and first_time( "icon-size-warning-%s" % filename): log.warn("Warning: icon is quite large (%i KB):", len(icondata) // 1024) log.warn(" '%s'", filename) return icondata, os.path.splitext(filename)[1].lstrip(".")
def load_icon_from_file(filename): log("load_icon_from_file(%s)", filename) if filename.endswith("xpm"): try: from xpra.gtk_common.gobject_compat import import_pixbufloader from xpra.gtk_common.gtk_util import pixbuf_save_to_memory data = load_binary_file(filename) loader = import_pixbufloader()() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() pngicondata = pixbuf_save_to_memory(pixbuf, "png") return pngicondata, "png" except Exception as e: log("pixbuf error loading %s", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) #try PIL: from PIL import Image try: img = Image.open(filename) except Exception as e: log("Image.open(%s)", filename, exc_info=True) log.error("Error loading '%s':", filename) log.error(" %s", e) return None buf = BytesIO() img.save(buf, "PNG") pngicondata = buf.getvalue() buf.close() return pngicondata, "png" icondata = load_binary_file(filename) if not icondata: return None if filename.endswith("svg") and len(icondata) > MAX_ICON_SIZE // 2: #try to resize it try: size = len(icondata) import cairo import gi try: gi.require_version('Rsvg', '2.0') except ValueError as e: if first_time("no-rsvg"): log.warn( "Warning: cannot resize svg icons, Rsvg bindings not found:" ) log.warn(" %s", e) else: from gi.repository import Rsvg img = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128) ctx = cairo.Context(img) handle = Rsvg.Handle.new_from_data(icondata) handle.render_cairo(ctx) buf = BytesIO() img.write_to_png(buf) icondata = buf.getvalue() buf.close() log( "reduced size of SVG icon %s, from %i bytes to %i bytes as PNG", filename, size, len(icondata)) filename = filename[:-3] + "png" except ImportError: log("cannot convert svg", exc_info=True) except Exception: log.error("Error: failed to convert svg icon", exc_info=True) log("got icon data from '%s': %i bytes", filename, len(icondata)) if len(icondata) > MAX_ICON_SIZE and first_time( "icon-size-warning-%s" % filename): log.warn("Warning: icon is quite large (%i KB):", len(icondata) // 1024) log.warn(" '%s'", filename) return icondata, os.path.splitext(filename)[1].lstrip(".")