def ascii_text_to_image(
    text,
    width,
    height,
    font_size=20,
    padding=10,
    line_spacing=1,
    bg_color="white",
    text_color="black",
):
    """Convert ASCII text into an image."""
    # Split the text into lines
    lines = text.splitlines()

    # Calculate initial image size based on text
    char_width = font_size * 0.6  # Approximate width of a character
    init_width = int(max(len(line) for line in lines) * char_width + 2 * padding)
    init_height = int(
        (len(lines) * font_size * line_spacing) + 2 * padding
    )  # 1.2 for line spacing

    # Create a new image with the calculated size
    image = Image.new("RGB", (init_width, init_height), color=bg_color)
    draw = ImageDraw.Draw(image)

    # Load a monospace font
    font = None
    for font_name in MONOSPACE_FONT_FILES:
        try:
            font = ImageFont.truetype(font_name, font_size)
            break
        except IOError:
            continue
    if font is None:
        raise ValueError("Cannot properly render ASCII art: missing monospace font.")

    # Draw each line of text
    y_text = padding
    for line in lines:
        draw.text((padding, y_text), line, font=font, fill=text_color)
        y_text += font_size * line_spacing  # Move to the next line

    # Resize the image to the specified dimensions
    image = image.resize((width, height), Image.Resampling.LANCZOS)

    # Convert the image to a NumPy array
    return image
Convert ASCII text into an image.
vlmeval/dataset/utils/megabench/scoring/common/conversions.py