示例#1
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)
示例#2
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
示例#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 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()
示例#7
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)
示例#8
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)
示例#9
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]
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)
示例#11
0
    back_svg = load_svg("back.svg")
    empty_svg = load_svg("empty.svg")


def render_svgs(scale):
    global card_surfaces, icon_surfaces, back_surface, empty_surface
    card_surfaces = {k: render_svg(v, scale) for k, v in card_svgs.items()}
    icon_surfaces = {k: render_svg(v, scale) for k, v in icon_svgs.items()}
    back_surface = render_svg(back_svg, scale)
    empty_surface = render_svg(empty_svg, scale)


def normalize_path(file):
    dir = getattr(sys, "_MEIPASS", "")
    return os.path.join(dir, "assets", file)


def load_svg(file):
    return Parser.parse_file(normalize_path(file))


rasterizer = Rasterizer()


def render_svg(svg, scale, convert=True):
    global rasterizer
    surface_size = round(svg.width * scale), round(svg.height * scale)
    buffer = rasterizer.rasterize(svg, *surface_size, scale)
    surface = pygame.image.frombuffer(buffer, surface_size, "RGBA")
    return surface.convert_alpha() if convert else surface