Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
Archivo: util.py Proyecto: Rucia1/Peppy
    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)
Ejemplo n.º 4
0
def test_parse_str():
    with open('tests/Londonhackspacelogo.svg') as s:
        svg = Parser.parse(s.read())
    assert isinstance(svg, SVG)