def replaced_box_width(box, containing_block):
    """Set the used width for replaced boxes."""
    from .block import block_level_width

    width, height, ratio = box.replacement.get_intrinsic_size(
        box.style['image_resolution'], box.style['font_size'])

    # This algorithm simply follows the different points of the specification:
    # https://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width
    if box.height == box.width == 'auto':
        if width is not None:
            # Point #1
            box.width = width
        elif ratio is not None:
            if height is not None:
                # Point #2 first part
                box.width = height * ratio
            else:
                # Point #3
                block_level_width(box, containing_block)

    if box.width == 'auto':
        if ratio is not None:
            # Point #2 second part
            box.width = box.height * ratio
        elif width is not None:
            # Point #4
            box.width = width
        else:
            # Point #5
            # It's pretty useless to rely on device size to set width.
            box.width = 300
Set the used width for replaced boxes.
weasyprint/layout/replaced.py