예제 #1
0
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
예제 #2
0
 def scale_svg_image(self, cache_path, svg_image, bounding_box=None, scale=1.0):
     """ Scale SVG image
     
     :param cache_path: cache key for image
     :param svg_image: SVG image
     :param bounding_box: image bounding box
     :param scale: scale factor
     
     :return: scaled bitmap image
     """
     w = svg_image.width + 2
     h = svg_image.height + 2
     
     if bounding_box == None:
         bb_w = w * scale
         bb_h = h * scale
     else:
         bb_w = bounding_box.w * scale
         bb_h = bounding_box.h * scale
         
     w_scaled = bb_w / w
     h_scaled = bb_h / h
     scale_factor = min(w_scaled, h_scaled)
     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)
예제 #3
0
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()
예제 #4
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)
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
    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)
예제 #8
0
    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]