def test_rast_to_bytes(): svg = Parser.parse_file('tests/Londonhackspacelogo.svg') r = Rasterizer() buff = r.rasterize(svg, svg.width, svg.height) assert isinstance(buff, bytes) img = Image.open('tests/Londonhackspacelogo.png') assert buff == img.tobytes()
def load_svg(filename, scale=None, size=None, clip_from=None, fit_to=None): """Returns Pygame Image object from rasterized SVG If scale (float) is provided and is not None, image will be scaled. If size (w, h tuple) is provided, the image will be clipped to specified size. If clip_from (x, y tuple) is provided, the image will be clipped from specified point. If fit_to (w, h tuple) is provided, image will be scaled to fit in specified rect. """ svg = Parser.parse_file(filename) tx, ty = 0, 0 if size is None: w, h = svg.width, svg.height else: w, h = size if clip_from is not None: tx, ty = clip_from if fit_to is None: if scale is None: scale = 1 else: fit_w, fit_h = fit_to scale_w = float(fit_w) / svg.width scale_h = float(fit_h) / svg.height scale = min([scale_h, scale_w]) rast = Rasterizer() req_w = int(w * scale) req_h = int(h * scale) buff = rast.rasterize(svg, req_w, req_h, scale, tx, ty) image = pygame.image.frombuffer(buff, (req_w, req_h), 'RGBA') return image
def load_svg_icon(self, filename, bounding_box=None, scale=1.0): """ Load SVG image :param filename: svg image file name :param bounding_box: image bounding box :param scale: scale factor :return: bitmap image rasterized from svg image """ filename += EXT_SVG path = os.path.join(FOLDER_ICONS, filename) cache_path = path + "_" + str(scale) try: i = self.image_cache[cache_path] return (cache_path, i) except KeyError: pass try: svg_image = Parser.parse_file(path) except: logging.debug("Problem parsing file %s", path) return None return self.scale_svg_image(cache_path, svg_image, bounding_box, scale)
def load_multi_color_svg_icon(self, filename, bounding_box=None, scale=1.0): """ Load SVG image :param filename: svg image file name :param bounding_box: image bounding box :param scale: scale factor :return: bitmap image rasterized from svg image """ filename += EXT_SVG path = os.path.join(FOLDER_ICONS, filename) cache_path = path + "_" + str(scale) try: i = self.image_cache[cache_path] return (cache_path, i) except KeyError: pass try: svg_image = Parser.parse_file(path) except: logging.debug("Problem parsing file %s", path) return None if self.config[USAGE][USE_WEB]: try: self.svg_cache[cache_path] except KeyError: t = cache_path.replace('\\','/') self.svg_cache[t] = codecs.open(path, "r").read() return self.scale_svg_image(cache_path, svg_image, bounding_box, scale)
def __init__(self, pos, size, svgFile, rotation=0.0): super().__init__(pos, size) # use the same size for the icon as the grid element width, height = self.rect.size # pressed version scale ds = 0.9 dx = dy = (1 - ds) / 2 # the position offset to use in x and y directions # use pynanosvg to convert svg file to bytes svg = Parser.parse_file(svgFile) #, PPI) ..I do not understand DPI # figure out the scaling, making sure not to draw outside the lines scale = min(width / svg.width, height / svg.height) buf = rasterizer.rasterize(svg, width, height, scale) self.surfDefault = image.frombuffer(buf, self.rect.size, 'RGBA') # make a mini version for activation state buf = rasterizer.rasterize(svg, width, height, scale * ds, tx=dx * width, ty=dy * height) self.surfPressed = image.frombuffer(buf, self.rect.size, 'RGBA') # apply rotation if (rotation != 0.0): self.surfDefault = transform.rotate(self.surfDefault, rotation) self.surfPressed = transform.rotate(self.surfPressed, rotation) # initialize the active surface self.surf = self.surfDefault
def create_png_from_svg_string(svg, png_name): svg = Parser.parse(svg) logging.info('Image is {} by {}.'.format(svg.width, svg.height)) rast = Rasterizer() buff = rast.rasterize(svg, svg.width, svg.height) im = Image.frombytes('RGBA', (svg.width, svg.height), buff) im.save(png_name)
def rasterize(vectorpath, vectorgraphic, scale=1): svg = Parser.parse_file(os.path.join(imgpath, vectorpath, vectorgraphic)) rast = Rasterizer() buff = rast.rasterize(svg, int(svg.width * scale), int(svg.height * scale), scale) im = Image.frombytes("RGBA", (int(svg.width * scale), int(svg.height * scale)), buff) return ImageTk.PhotoImage(im)
def test_rast_with_buffer(): svg = Parser.parse_file('tests/Londonhackspacelogo.svg') r = Rasterizer() stride = svg.width * 4 buff = bytes(stride * svg.height) assert isinstance(buff, bytes) r.rasterize_to_buffer(svg, svg.width, svg.height, stride, buff) assert isinstance(buff, bytes) img = Image.open('tests/Londonhackspacelogo.png') assert buff == img.tobytes()
def load_svg(filename, surface, position, size=None): if size is None: w = surface.get_width() h = surface.get_height() else: w, h = size svg = Parser.parse_file(filename) rast = Rasterizer() buff = rast.rasterize(svg, w, h) image = pygame.image.frombuffer(buff, (w, h), 'ARGB') surface.blit(image, position)
def load_svg_icon(self, filename, color_1, bounding_box=None, scale=1.0, color_2=None, gradient=False): """ Load monochrome SVG image with replaced color :param filename: svg image file name :param color_1: base icon hex color :param bounding_box: image bounding box :param scale: scale factor :return: bitmap image rasterized from svg image """ filename += EXT_SVG path = os.path.join(FOLDER_ICONS, filename) t = path.replace('\\','/') if color_2: c_2 = "_" + color_2 else: c_2 = "" cache_path = t + "_" + str(scale) + "_" + color_1 + c_2 try: i = self.image_cache[cache_path] return (cache_path, i) except KeyError: pass s = codecs.open(path, "r").read() if gradient: g = "url(#gradient)" s = s.replace(SVG_DEFAULT_GRADIENT_COLOR_1, color_2) s = s.replace(SVG_DEFAULT_GRADIENT_COLOR_2, color_1) s = s.replace(SVG_DEFAULT_COLOR_1, g) s = s.replace(SVG_DEFAULT_COLOR_2, g) else: if color_2: s = s.replace(SVG_DEFAULT_COLOR_2, color_1) s = s.replace(SVG_DEFAULT_COLOR_1, color_2) else: s = s.replace(SVG_DEFAULT_COLOR_1, color_1) try: bitmap_image = Parser.parse(s) except: logging.debug("Problem parsing file %s", path) return None if self.config[USAGE][USE_WEB]: self.svg_cache[cache_path] = s return self.scale_svg_image(cache_path, bitmap_image, bounding_box, scale)
def load_svg(filename, scale=None, size=None, clip_from=None, fit_to=None, foramt='RGBA'): svg = Parser.parse_file(filename) scale = min((fit_to[0] / svg.width, fit_to[1] / svg.height) if fit_to else ([scale if scale else 1] * 2)) width, height = size if size else (svg.width, svg.height) surf_size = round(width * scale), round(height * scale) buffer = Rasterizer().rasterize(svg, *surf_size, scale, *(clip_from if clip_from else 0, 0)) return pygame.image.frombuffer(buffer, surf_size, foramt)
def load_svg_icon(self, folder, image_name, bounding_box=None): """ Load SVG image :param folder: icon folder :param image_name: svg image file name :param bounding_box: image bounding box :return: bitmap image rasterized from svg image """ base_path = self.weather_config[BASE_PATH] path = os.path.join(base_path, folder, image_name) cache_path = path + "." + str(bounding_box.w) + "." + str( bounding_box.h) try: i = self.image_cache[cache_path] return (cache_path, i) except KeyError: pass try: svg_image = Parser.parse_file(path) except: logging.debug("Problem parsing file %s", path) return None w = svg_image.width + 2 h = svg_image.height + 2 k_w = bounding_box.w / w k_h = bounding_box.h / h scale_factor = min(k_w, k_h) w_final = int(w * scale_factor) h_final = int(h * scale_factor) r = Rasterizer() buff = r.rasterize(svg_image, w_final, h_final, scale_factor) image = pygame.image.frombuffer(buff, (w_final, h_final), 'RGBA') self.image_cache[cache_path] = image return (cache_path, image)
def getIcon(self, component, key): name = self.getStyle(component, "{}.{}".format(key, "name")) size = self.getStyle(component, "{}.{}".format(key, "size")) iconPath = os.path.join(*Styling.ICONS_PATH, name) iconID = "{}_{}".format(name, size) if type(size) is int: w = h = size elif type(size) is tuple: w, h = size else: w = h = 32 if iconID not in self.iconCache: svg = Parser.parse_file(iconPath) rast = Rasterizer() buff = rast.rasterize(svg, w, h) self.iconCache[iconID] = pygame.image.frombuffer( buff, (w, h), 'ARGB') return self.iconCache[iconID]
def load_mono_svg_icon(self, filename, color, bounding_box=None, scale=1.0): """ Load monochrome SVG image with replaced color :param filename: svg image file name :param color: base icon hex color :param bounding_box: image bounding box :param scale: scale factor :return: bitmap image rasterized from svg image """ filename += EXT_SVG path = os.path.join(FOLDER_ICONS, filename) t = path.replace('\\', '/') cache_path = t + "_" + str(scale) + "_" + color try: i = self.image_cache[cache_path] return (cache_path, i) except KeyError: pass s = codecs.open(path, "r").read() s = s.replace(SVG_DEFAULT_COLOR, color) try: bitmap_image = Parser.parse(s) except: logging.debug("Problem parsing file %s", path) return None if self.config[USAGE][USE_WEB]: self.svg_cache[cache_path] = s return self.scale_svg_image(cache_path, bitmap_image, bounding_box, scale)
def test_parse_str(): with open('tests/Londonhackspacelogo.svg') as s: svg = Parser.parse(s.read()) assert isinstance(svg, SVG)
def test_svg_size(): svg = Parser.parse_file('tests/Londonhackspacelogo.svg') assert svg.width == 114 assert svg.height == 114 assert isinstance(svg.width, int) assert isinstance(svg.height, int)
def load_svg(file): return Parser.parse_file(normalize_path(file))
def test_parse_file(): svg = Parser.parse_file('tests/Londonhackspacelogo.svg') assert isinstance(svg, SVG)