Ejemplo n.º 1
0
def _generate_sprite(images, sprite_path):
    sprite_width = max(i.width for i in images)
    sprite_height = 0

    # put all the max-width and stretch-x images together at the top
    small_images = []
    for image in images:
        if image.width == sprite_width or image.stretch:
            if image.stretch:
                image.stretch_to_width(sprite_width)
            image.sprite_location = (0, sprite_height)
            sprite_height += image.height + SPRITE_PADDING
        else:
            small_images.append(image)

    # lay out the remaining images -- done with a greedy algorithm
    small_images.sort(key=lambda i: i.height, reverse=True)
    bins = []

    for image in small_images:
        # find a bin to fit in
        for bin in bins:
            if bin.has_space_for(image):
                break
        else:
            # or give up and create a new bin
            bin = SpriteBin(
                (0, sprite_height, sprite_width, sprite_height + image.height))
            sprite_height += image.height + SPRITE_PADDING
            bins.append(bin)

        bin.add_image(image)

    # generate the image
    sprite_dimensions = (sprite_width, sprite_height)
    background_color = (255, 69, 0, 0)  # transparent orangered
    sprite = Image.new('RGBA', sprite_dimensions, background_color)

    for image in images:
        sprite.paste(image.image, image.sprite_location)

    sprite.save(sprite_path, optimize=True)
    optimize_png(sprite_path)

    # give back the mangled name
    sprite_base, sprite_name = os.path.split(sprite_path)
    return generate_static_name(sprite_name, base=sprite_base)
Ejemplo n.º 2
0
def _generate_sprite(images, sprite_path):
    sprite_width = max(i.width for i in images)
    sprite_height = 0

    # put all the max-width and stretch-x images together at the top
    small_images = []
    for image in images:
        if image.width == sprite_width or image.stretch:
            if image.stretch:
                image.stretch_to_width(sprite_width)
            image.sprite_location = (0, sprite_height)
            sprite_height += image.height + SPRITE_PADDING
        else:
            small_images.append(image)

    # lay out the remaining images -- done with a greedy algorithm
    small_images.sort(key=lambda i: i.height, reverse=True)
    bins = []

    for image in small_images:
        # find a bin to fit in
        for bin in bins:
            if bin.has_space_for(image):
                break
        else:
            # or give up and create a new bin
            bin = SpriteBin((0, sprite_height, sprite_width, sprite_height + image.height))
            sprite_height += image.height + SPRITE_PADDING
            bins.append(bin)

        bin.add_image(image)

    # generate the image
    sprite_dimensions = (sprite_width, sprite_height)
    background_color = (255, 69, 0, 0)  # transparent orangered
    sprite = Image.new('RGBA', sprite_dimensions, background_color)

    for image in images:
        sprite.paste(image.image, image.sprite_location)

    sprite.save(sprite_path, optimize=True)
    optimize_png(sprite_path)

    # give back the mangled name
    sprite_base, sprite_name = os.path.split(sprite_path)
    return generate_static_name(sprite_name, base=sprite_base)