def layout_box_backgrounds(page, box, get_image_from_uri, layout_children=True,
                           style=None):
    """Fetch and position background images."""
    from ..draw.color import get_color

    # Resolve percentages in border-radius properties
    resolve_radii_percentages(box)

    if layout_children:
        for child in box.all_children():
            layout_box_backgrounds(page, child, get_image_from_uri)

    if style is None:
        style = box.style

    # This is for the border image, not the background, but this is a
    # convenient place to get the image.
    if style['border_image_source'][0] != 'none':
        type_, value = style['border_image_source']
        if type_ == 'url':
            box.border_image = get_image_from_uri(url=value)
        else:
            box.border_image = value

    if style['mask_border_source'][0] != 'none':
        type_, value = style['mask_border_source']
        if type_ == 'url':
            box.mask_border_image = get_image_from_uri(url=value)
        else:
            box.mask_border_image = value

    if style['visibility'] == 'hidden':
        images = []
        color = parse_color('transparent')
    else:
        orientation = style['image_orientation']
        images = [
            get_image_from_uri(url=value, orientation=orientation)
            if type_ == 'url' else value
            for type_, value in style['background_image']]
        color = get_color(style, 'background_color')

    if color.alpha == 0 and not any(images):
        if box != page:  # Pages need a background for bleed box
            box.background = None
            return

    layers = [
        layout_background_layer(box, page, style['image_resolution'], *layer)
        for layer in zip(images, *map(cycle, [
            style['background_size'],
            style['background_clip'],
            style['background_repeat'],
            style['background_origin'],
            style['background_position'],
            style['background_attachment']]))]
    box.background = Background(color, layers, style['image_rendering'])
Fetch and position background images.
weasyprint/layout/background.py