def get_pix(self, filename, size = None): if not filename in self._data: self._data[filename] = {} if size in self._data[filename]: pix = self._data[filename][size] else: try: img = Image.open(filename) (width, height) = img.size if img.mode != 'RGB': img = img.convert('RGB') if size: img.thumbnail((size, size), Image.ANTIALIAS) img = imtools.round_image(img, {}, False, None, 3, 255) img = imtools.drop_shadow(img, 4, 4, background_color=(255, 255, 255, 0), shadow_color=0x444444, border=8, shadow_blur=3, force_background_color=False, cache=None) # Convert Image -> Pixbuf (save to file, GTK3 is not reliable for that) f = tempfile.NamedTemporaryFile(delete=False) temp_filename = f.name f.close() img.save(temp_filename, "png") pix = [GdkPixbuf.Pixbuf.new_from_file(temp_filename), width, height] os.unlink(temp_filename) except Exception, detail: print "Failed to convert %s: %s" % (filename, detail) pix = None if pix: self._data[filename][size] = pix
class PixCache(object): def __init__(self): self._data = {} def get_pix(self, filename, size=None): try: mimetype = subprocess.check_output(["file", "-bi", filename]).split(";")[0] if not mimetype.startswith("image/"): print "Not trying to convert %s : not a recognized image file" % filename return None except Exception, detail: print "Failed to detect mimetype for %s: %s" % (filename, detail) return None if not filename in self._data: self._data[filename] = {} if size in self._data[filename]: pix = self._data[filename][size] else: try: if mimetype == "image/svg+xml": tmp_pix = GdkPixbuf.Pixbuf.new_from_file(filename) tmp_fp, tmp_filename = tempfile.mkstemp() os.close(tmp_fp) tmp_pix.savev(tmp_filename, "png", [], []) img = Image.open(tmp_filename) os.unlink(tmp_filename) else: img = Image.open(filename) (width, height) = img.size if img.mode != 'RGB': img = img.convert('RGB') if size: img.thumbnail((size, size), Image.ANTIALIAS) img = imtools.round_image(img, {}, False, None, 3, 255) img = imtools.drop_shadow(img, 4, 4, background_color=(255, 255, 255, 0), shadow_color=0x444444, border=8, shadow_blur=3, force_background_color=False, cache=None) # Convert Image -> Pixbuf (save to file, GTK3 is not reliable for that) f = tempfile.NamedTemporaryFile(delete=False) temp_filename = f.name f.close() img.save(temp_filename, "png") pix = [ GdkPixbuf.Pixbuf.new_from_file(temp_filename), width, height ] os.unlink(temp_filename) except Exception, detail: print "Failed to convert %s: %s" % (filename, detail) pix = None if pix: self._data[filename][size] = pix
class PixCache(object): def __init__(self): self._data = {} def get_pix(self, filename, size=None): try: mimetype = get_mimetype(filename) if not mimetype.startswith("image/"): print "Not trying to convert %s : not a recognized image file" % filename return None except Exception, detail: print "Failed to detect mimetype for %s: %s" % (filename, detail) return None if filename not in self._data: self._data[filename] = {} if size in self._data[filename]: pix = self._data[filename][size] else: try: h = hashlib.sha1(('%f%s' % (os.path.getmtime(filename), filename)).encode()).hexdigest() tmp_cache_path = GLib.get_user_cache_dir() + '/cs_backgrounds/' if not os.path.exists(tmp_cache_path): os.mkdir(tmp_cache_path) cache_filename = tmp_cache_path + h if os.path.exists(cache_filename): (width, height) = Image.open(filename).size else: if mimetype == "image/svg+xml": tmp_pix = GdkPixbuf.Pixbuf.new_from_file(filename) tmp_fp, tmp_filename = tempfile.mkstemp() os.close(tmp_fp) tmp_pix.savev(tmp_filename, "png", [], []) img = Image.open(tmp_filename) os.unlink(tmp_filename) else: img = Image.open(filename) img = apply_orientation(img) (width, height) = img.size if img.mode != 'RGB': img = img.convert('RGB') if size: img.thumbnail((size, size), Image.ANTIALIAS) img = imtools.round_image(img, {}, False, None, 3, 255) img = imtools.drop_shadow(img, 4, 4, background_color=(255, 255, 255, 0), shadow_color=0x444444, border=8, shadow_blur=3, force_background_color=False, cache=None) # Convert Image -> Pixbuf (save to file, GTK3 is not reliable for that) img.save(cache_filename, "png") pix = [GdkPixbuf.Pixbuf.new_from_file(cache_filename), width, height] except Exception, detail: print "Failed to convert %s: %s" % (filename, detail) pix = None if pix: self._data[filename][size] = pix
def get_pix(self, filename, size=None): if filename is None: return None mimetype = mimetypes.guess_type(filename)[0] if mimetype is None or not mimetype.startswith("image/"): return None if filename not in self._data: self._data[filename] = {} if size in self._data[filename]: pix = self._data[filename][size] else: try: h = hashlib.sha1(('%f%s' % (os.path.getmtime(filename), filename)).encode()).hexdigest() tmp_cache_path = GLib.get_user_cache_dir() + '/cs_backgrounds/' if not os.path.exists(tmp_cache_path): os.mkdir(tmp_cache_path) cache_filename = tmp_cache_path + h + "v2" loaded = False if os.path.exists(cache_filename): # load from disk cache try: with open(cache_filename, "rb") as cache_file: pix = pickle.load(cache_file) tmp_img = Image.open(BytesIO(pix[0])) pix[0] = self._image_to_pixbuf(tmp_img) loaded = True except Exception as detail: # most likely either the file is corrupted, or the file was pickled using the # python2 version of cinnamon settings. Either way, we want to ditch the current # cache file and generate a new one. This is still backward compatible with older # Cinnamon versions os.remove(cache_filename) if not loaded: if mimetype == "image/svg+xml": # rasterize svg with Gdk-Pixbuf and convert to PIL Image tmp_pix = GdkPixbuf.Pixbuf.new_from_file(filename) mode = "RGBA" if tmp_pix.props.has_alpha else "RGB" img = Image.frombytes( mode, (tmp_pix.props.width, tmp_pix.props.height), tmp_pix.read_pixel_bytes().get_data(), "raw", mode, tmp_pix.props.rowstride) else: img = Image.open(filename) img = apply_orientation(img) # generate thumbnail (width, height) = img.size if img.mode != "RGB": if img.mode == "RGBA": bg_img = Image.new("RGBA", img.size, (255, 255, 255, 255)) img = Image.alpha_composite(bg_img, img) img = img.convert("RGB") if size: img.thumbnail((size, size), Image.ANTIALIAS) img = imtools.round_image(img, {}, False, None, 3, 255) img = imtools.drop_shadow(img, 4, 4, background_color=(255, 255, 255, 0), shadow_color=0x444444, border=8, shadow_blur=3, force_background_color=False, cache=None) # save to disk cache try: png_bytes = BytesIO() img.save(png_bytes, "png") with open(cache_filename, "wb") as cache_file: pickle.dump([png_bytes.getvalue(), width, height], cache_file, PICKLE_PROTOCOL_VERSION) except Exception as detail: print("Failed to save cache file: %s: %s" % (cache_filename, detail)) pix = [self._image_to_pixbuf(img), width, height] except Exception as detail: print("Failed to convert %s: %s" % (filename, detail)) pix = None if pix: self._data[filename][size] = pix return pix
def get_pix(self, filename, size=None): if filename is None: return None mimetype = mimetypes.guess_type(filename)[0] if mimetype is None or not mimetype.startswith("image/"): return None if filename not in self._data: self._data[filename] = {} if size in self._data[filename]: pix = self._data[filename][size] else: try: h = hashlib.sha1(('%f%s' % (os.path.getmtime(filename), filename)).encode()).hexdigest() tmp_cache_path = GLib.get_user_cache_dir() + '/cs_backgrounds/' if not os.path.exists(tmp_cache_path): os.mkdir(tmp_cache_path) cache_filename = tmp_cache_path + h + "v2" loaded = False if os.path.exists(cache_filename): # load from disk cache try: with open(cache_filename, "rb") as cache_file: pix = pickle.load(cache_file) tmp_img = Image.open(BytesIO(pix[0])) pix[0] = self._image_to_pixbuf(tmp_img) loaded = True except Exception as detail: # most likely either the file is corrupted, or the file was pickled using the # python2 version of cinnamon settings. Either way, we want to ditch the current # cache file and generate a new one. This is still backward compatible with older # Cinnamon versions os.remove(cache_filename) if not loaded: if mimetype == "image/svg+xml": # rasterize svg with Gdk-Pixbuf and convert to PIL Image tmp_pix = GdkPixbuf.Pixbuf.new_from_file(filename) mode = "RGBA" if tmp_pix.props.has_alpha else "RGB" img = Image.frombytes(mode, (tmp_pix.props.width, tmp_pix.props.height), tmp_pix.read_pixel_bytes().get_data(), "raw", mode, tmp_pix.props.rowstride) else: img = Image.open(filename) img = apply_orientation(img) # generate thumbnail (width, height) = img.size if img.mode != "RGB": if img.mode == "RGBA": bg_img = Image.new("RGBA", img.size, (255,255,255,255)) img = Image.alpha_composite(bg_img, img) img = img.convert("RGB") if size: img.thumbnail((size, size), Image.ANTIALIAS) img = imtools.round_image(img, {}, False, None, 3, 255) img = imtools.drop_shadow(img, 4, 4, background_color=(255, 255, 255, 0), shadow_color=0x444444, border=8, shadow_blur=3, force_background_color=False, cache=None) # save to disk cache try: png_bytes = BytesIO() img.save(png_bytes, "png") with open(cache_filename, "wb") as cache_file: pickle.dump([png_bytes.getvalue(), width, height], cache_file, PICKLE_PROTOCOL_VERSION) except Exception as detail: print("Failed to save cache file: %s: %s" % (cache_filename, detail)) pix = [self._image_to_pixbuf(img), width, height] except Exception as detail: print("Failed to convert %s: %s" % (filename, detail)) pix = None if pix: self._data[filename][size] = pix return pix
class PixCache(object): def __init__(self): self._data = {} def get_pix(self, filename, size=None): if filename is None: return None mimetype = mimetypes.guess_type(filename)[0] if mimetype is None or not mimetype.startswith("image/"): return None if filename not in self._data: self._data[filename] = {} if size in self._data[filename]: pix = self._data[filename][size] else: try: h = hashlib.sha1(('%f%s' % (os.path.getmtime(filename), filename)).encode()).hexdigest() tmp_cache_path = GLib.get_user_cache_dir() + '/cs_backgrounds/' if not os.path.exists(tmp_cache_path): os.mkdir(tmp_cache_path) cache_filename = tmp_cache_path + h + "v2" if os.path.exists(cache_filename): # load from disk cache try: with open(cache_filename, "r") as cache_file: pix = pickle.load(cache_file) tmp_img = Image.open(BytesIO(pix[0])) pix[0] = self._image_to_pixbuf(tmp_img) except Exception, detail: print "Failed to load cache file: %s: %s" % ( cache_filename, detail) pix = None else: if mimetype == "image/svg+xml": # rasterize svg with Gdk-Pixbuf and convert to PIL Image tmp_pix = GdkPixbuf.Pixbuf.new_from_file(filename) mode = "RGBA" if tmp_pix.props.has_alpha else "RGB" img = Image.frombytes( mode, (tmp_pix.props.width, tmp_pix.props.height), tmp_pix.read_pixel_bytes().get_data(), "raw", mode, tmp_pix.props.rowstride) else: img = Image.open(filename) img = apply_orientation(img) # generate thumbnail (width, height) = img.size if img.mode != "RGB": if img.mode == "RGBA": bg_img = Image.new("RGBA", img.size, (255, 255, 255, 255)) img = Image.alpha_composite(bg_img, img) img = img.convert("RGB") if size: img.thumbnail((size, size), Image.ANTIALIAS) img = imtools.round_image(img, {}, False, None, 3, 255) img = imtools.drop_shadow(img, 4, 4, background_color=(255, 255, 255, 0), shadow_color=0x444444, border=8, shadow_blur=3, force_background_color=False, cache=None) # save to disk cache try: png_bytes = BytesIO() img.save(png_bytes, "png") with open(cache_filename, "w") as cache_file: pickle.dump([png_bytes.getvalue(), width, height], cache_file, 2) except Exception, detail: print "Failed to save cache file: %s: %s" % ( cache_filename, detail)