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 iris2(canvas, pos, size, tint, radius=10, shadow_enabled=True, rounded=0, shadow_size=0.08, alpha=255, resolution=20, anti_glitch=False): global fancy, mode intensity = int((alpha / 255) * 30) if intensity > 255: intensity = 255 elif intensity < 0: intensity = 0 size = [int(size[0]), int(size[1])] if fancy == False: tnt = list(pygame.transform.average_color(canvas, [0, 0, 50, 50]))[:3] resolution = resolution # percentage of pixels per inch to use. If one inch is 300 pixels, use 150 is var is set to 50% r = (resolution / 100.0) * inch2pix(1) inches_w = size[0] / inch2pix(1) r2 = inches_w * r resolution = (r2 / size[0]) * 100 smooth_pad = 0 if anti_glitch == True: smooth_pad = 5 if fancy == False: smooth_pad = 0 s = pygame.Surface( [size[0] + int(2 * smooth_pad), size[1] + int(2 * smooth_pad)]) # , pygame.SRCALPHA) if fancy == True: s.blit(canvas, [0, 0], [ pos[0] - smooth_pad, pos[1] - smooth_pad, size[0] + smooth_pad * 2, size[1] + smooth_pad * 2 ]) # , special_flags=pygame.BLEND_RGBA_ADD) s = pygame.transform.rotozoom(s, 0, (resolution / 100.0) * 1) size2 = s.get_size() if tint != False: pygame.gfxdraw.filled_polygon( s, [[0, 0], [size2[0], 0], size2, [0, size2[1]]], tint + [intensity]) if mode == "light": pygame.gfxdraw.filled_polygon( s, [[0, 0], [size2[0], 0], size2, [0, size2[1]]], [255, 255, 255] + [int(70 * (alpha / 255))]) rad = radius b = pygame.image.tostring(s, "RGBA", False) b = PIL.Image.frombytes("RGBA", size2, b) # b = replace_color(b, [0, 0, 0], [255, 0, 0]) b = b.filter(PIL.ImageFilter.GaussianBlur(radius=int(rad))) # b = b.crop((smooth_pad, smooth_pad, size2[0] - smooth_pad, size2[1] - smooth_pad)) # maxsize = (size) # b.thumbnail(maxsize, PIL.Image.ANTIALIAS) if size[0] <= 0: size[0] = 1 if size[1] <= 0: size[1] = 1 b = b.resize(size) if rounded != 0: b = imtools.round_image(b, {}, False, None, rounded, 255, back_color="#00000000") b = pygame.image.frombuffer(b.tobytes(), b.size, b.mode).convert_alpha() else: pass """ s.blit(canvas, [0, 0], [pos[0], pos[1], size[0], size[1]]) size2 = size pygame.gfxdraw.filled_polygon(s, [[0, 0], [size2[0], 0], size2, [0, size2[1]]], tint + [200]) b = s """ # pygame.draw.rect(canvas, [0, 200, 0], [pos[0], pos[1], size[0], size[1]]) if shadow_enabled == True: shadow_default = inch2pix(0.1) shadow_width = int(shadow_default * shadow_size) # Corners shadow_alpha = int((alpha / 255) * 150) s_cornera = s_corner.copy() s_cornera.set_alpha(shadow_alpha) scaled_corner = pygame.transform.scale(s_cornera, [shadow_width, shadow_width]) # TR canvas.blit(scaled_corner, [pos[0] - shadow_width, pos[1] - shadow_width]) # TL canvas.blit(pygame.transform.rotate(scaled_corner, -90), [pos[0] + size[0], pos[1] - shadow_width]) # BR canvas.blit(pygame.transform.rotate(scaled_corner, -180), [pos[0] + size[0], pos[1] + size[1]]) # BL canvas.blit(pygame.transform.rotate(scaled_corner, 90), [pos[0] - shadow_width, pos[1] + size[1]]) # edges s_edgea = s_edge.copy() s_edgea.set_alpha(shadow_alpha) # R scaled_r = pygame.transform.scale(s_edgea, [shadow_width, size[1]]) canvas.blit(scaled_r, [pos[0] - shadow_width, pos[1]]) # T rotated_t = pygame.transform.rotate( s_edgea, -90) # pygame.transform.rotozoom(s_edge, -90, 1) canvas.blit(pygame.transform.scale(rotated_t, [size[0], shadow_width]), [pos[0], pos[1] - shadow_width]) # L rotated_l = pygame.transform.flip(s_edgea, True, False) canvas.blit(pygame.transform.scale(rotated_l, [shadow_width, size[1]]), [pos[0] + size[0], pos[1]]) # B rotated_b = pygame.transform.rotate(s_edgea, 90) canvas.blit(pygame.transform.scale(rotated_b, [size[0], shadow_width]), [pos[0], pos[1] + size[1]]) # b = pygame.transform.rotozoom(b, 0, (100.0 / resolution) * 1) # if fancy == True: # b = pygame.transform.scale(b, size) if fancy == True: b.set_alpha(alpha) canvas.blit(b, pos) # , special_flags=pygame.BLEND_RGBA_ADD) else: """ pygame.gfxdraw.filled_polygon(canvas, [pos, [pos[0] + size[0], pos[1]], [pos[0] + size[0], pos[1] + size[1]], [pos[0], pos[1] + size[1]]], tint + [int(50 * (alpha / 255))]) """ if mode == "light": pygame.gfxdraw.filled_polygon(canvas, [ pos, [pos[0] + size[0], pos[1]], [pos[0] + size[0], pos[1] + size[1]], [pos[0], pos[1] + size[1]] ], [255, 255, 255] + [int(255 * (alpha / 255))]) else: pygame.gfxdraw.filled_polygon(canvas, [ pos, [pos[0] + size[0], pos[1]], [pos[0] + size[0], pos[1] + size[1]], [pos[0], pos[1] + size[1]] ], [50, 50, 50] + [int(255 * (alpha / 255))]) """
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)