Esempio n. 1
0
def spritemap(css_fs, conf=None, out=sys.stderr):
    w_ln = lambda t: out.write(t + "\n")

    #: sum of all spritemaps used from any css files
    smaps = SpriteMapCollector(conf=conf)

    for css in css_fs:
        w_ln("mapping sprites in source %s" % (css.fname,))
        for sm in smaps.collect(css.map_sprites()):
            w_ln(" - %s" % (sm.fname,))

    # Weed out single-image spritemaps (these make no sense.)
    smaps = [sm for sm in smaps if len(sm) > 1]

    sm_plcs = []
    for smap in smaps:
        with open_sprites(smap, pad=conf.padding) as sprites:
            w_ln("packing sprites in mapping %s" % (smap.fname,))
            logger.debug("annealing %s in steps of %d",
                         smap.fname, conf.anneal_steps)
            packed = PackedBoxes(sprites, anneal_steps=conf.anneal_steps)
            print_packed_size(packed)
            sm_plcs.append((smap, packed.placements))

            w_ln("writing spritemap image at %s" % (smap.fname,))
            im = stitch(packed)
            with open(smap.fname, "wb") as fp:
                im.save(fp)

    replacer = SpriteReplacer(sm_plcs)
    for css in css_fs:
        w_ln("writing new css at %s" % (css.output_fname,))
        with open(css.output_fname, "wb") as fp:
            print_css(replacer(css), out=fp)
Esempio n. 2
0
def main():
    import os
    import time
    from spritecss.packing.sprites import open_sprites

    logging.basicConfig(level=logging.DEBUG)

    d = '/ssd/home/work/imagemap/images'
    f = [os.path.join(d, x)
         for x in sorted(os.listdir(d))
         if x.endswith('.png')]
    with open_sprites(f, pad=(1, 1)) as sprites:
        t1 = time.time()
        im, placements = naive_packing(sprites)
        t2 = time.time()
        print("naive_packing took %.2f seconds" % (t2 - t1,))

        with open('images.html', 'w') as fp:
            fp.write('<!DOCTYPE html><html><head><meta charset="utf-8" />' +
                     '<title>Naive sprite packing</title></head>\n')
            fp.write('<body style="background-color:black">\n')
            fp.write(('<div style="position:relative;width:%dpx;height:%dpx;' +
                      'background-color:gray;overflow:hidden">\n') %
                     (im.width, im.height))
            for placement, image in placements:
                fp.write(
                    ('<img src="{im.fname}" ' +
                     'style="position:absolute;' +
                     'left:{left}px;top:{top}px"/>\n').format(
                         im=image, left=placement[0], top=placement[1]))
            fp.write('</div></body></html>\n')
Esempio n. 3
0
    def input(self, filename=None, **kwargs):
        if not filename or not filename.startswith(settings.COMPRESS_ROOT) or not filename.endswith(".scss"):
            return self.content

        source_path = filename

        # Save the input data for later
        css = self.content

        # Build config object
        conf = LocalCSSConfig(base=self.options, fname=source_path, root=settings.COMPRESS_ROOT)

        # Instantiate a dummy file instance
        cssfile = FakeCSSFile(fname=source_path, conf=conf, data=css)

        # Find spritemaps
        smaps = SpriteMapCollector(conf=conf)
        smaps.collect(cssfile.map_sprites())

        # Weed out single-image spritemaps
        smaps = [sm for sm in smaps if len(sm) > 1]

        # Generate spritemapped image
        # This code is almost verbatim from spritecss.main.spritemap
        sm_plcs = []
        for smap in smaps:
            with open_sprites(smap, pad=conf.padding) as sprites:
                print("packing sprites in mapping %s" % (smap.fname,))

                if conf.packer == 'annealing':
                    print("annealing %s in steps of %d" % (smap.fname, conf.anneal_steps))
                    packed = PackedBoxes(sprites, anneal_steps=conf.anneal_steps)
                    print_packed_size(packed)
                    sm_plcs.append((smap, packed.placements))
                    im = stitch(packed)

                elif conf.packer == 'naive':
                    print("Naive packing")
                    im, placements = naive_packing(sprites)
                    sm_plcs.append((smap, placements))

                print("writing spritemap image at %s" % (smap.fname,))
                with open(smap.fname, "wb") as fp:
                    im.save(fp)

        # Instantiate a fake file instance again
        cssfile = FakeCSSFile(fname=source_path, conf=conf, data=css)

        out = StringIO()

        # Output rewritten CSS with spritemapped URLs
        replacer = SpriteReplacer(sm_plcs)
        for data in iter_print_css(replacer(cssfile)):
            out.write(data)

        return out.getvalue()
Esempio n. 4
0
    def input(self, _in, out, **kw):

        source_path = kw['source_path']

        # Save the input data for later
        css = _in.read()

        # Build config object
        conf = CSSConfig(base=self.options, fname=source_path)

        # Instantiate a dummy file instance
        cssfile = FakeCSSFile(fname=source_path, conf=conf, data=css)

        # Find spritemaps
        smaps = SpriteMapCollector(conf=conf)
        smaps.collect(cssfile.map_sprites())

        # Weed out single-image spritemaps
        smaps = [sm for sm in smaps if len(sm) > 1]

        # Generate spritemapped image
        # This code is almost verbatim from spritecss.main.spritemap
        sm_plcs = []
        for smap in smaps:
            with open_sprites(smap, pad=conf.padding) as sprites:
                print(("Packing sprites in mapping %s" % (smap.fname,)))
                packed = PackedBoxes(sprites, anneal_steps=conf.anneal_steps)
                print_packed_size(packed)
                sm_plcs.append((smap, packed.placements))
                print(("Writing spritemap image at %s" % (smap.fname,)))
                im = stitch(packed)
                with open(smap.fname, "wb") as fp:
                    im.save(fp)

        # Instantiate a fake file instance again
        cssfile = FakeCSSFile(fname=source_path, conf=conf, data=css)

        # Output rewritten CSS with spritemapped URLs
        replacer = SpriteReplacer(sm_plcs)
        for data in iter_print_css(replacer(cssfile)):
            out.write(data)