While the following python code is running fine most of the times, the generated png times are off quality sometimes, could you pls look at where it is going wrong?

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

def render_math_to_png(math_text, output_path):
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--window-size=2048,1536')  # Increased window size
    options.add_argument('--hide-scrollbars')
    driver = webdriver.Chrome(options=options)
    
    html_content = f'''
    <html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS-MML_SVG"></script>
    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({{
            tex2jax: {{inlineMath: [['$','$'], ['\\\\(','\\\\)']]}},
            SVG: {{
                scale: 300,
                linebreaks: {{ automatic: true, width: "container" }}
            }},
            extensions: ["AMSmath.js", "AMSsymbols.js"],
            messageStyle: "none"
        }});
        MathJax.Hub.Queue(function() {{
            document.getElementById('ready').style.display = "";
        }});
    </script>
        <style>
            body {{
                margin: 40px;
                background-color: transparent;
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
            }}
            #container {{
                padding: 20px;
                background-color: transparent;
            }}
            #math {{
                font-size: 24px;
                visibility: visible;
                background-color: transparent;
                padding: 20px;
                margin: 20px;
                min-width: 400px;
            }}
            #ready {{
                display: none;
            }}
        </style>
    </head>
    <body>
        <div id="container">
            <div id="math">\\[{math_text}\\]</div>
            <div id="ready"></div>
        </div>
    </body>
    </html>
    '''

    temp_path = 'temp_math.html'
    with open(temp_path, 'w', encoding='utf-8') as f:
        f.write(html_content)

    driver.get(f'file://{os.path.abspath(temp_path)}')
    
    # Wait for MathJax to finish rendering[1]
    try:
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "ready"))
        )
        time.sleep(1)  # Additional small delay for safety
    except Exception as e:
        print(f"Warning: Timeout waiting for MathJax rendering: {e}")

    # Get the container element and its size
    container = driver.find_element(By.ID, "container")
    size = container.size
    
    # Set window size based on content
    driver.set_window_size(max(size['width'] + 100, 800), max(size['height'] + 100, 400))
    
    # Take screenshot
    container.screenshot(output_path)
    
    driver.quit()
    os.remove(temp_path)

# Your equations array remains the same
equations = [
# r"\begin{aligned} & \text{Two right circular cylinders } P \text{ and } Q \text{ have} \\ & \text{volumes of } 256\pi \text{ and } 8192\pi \text{ cubic units} \\ & \text{respectively. Cylinder } P \text{ has a radius of } 8 \text{ units.} \\ & \text{If their surface areas are } k\pi \text{ and } n\pi \text{ square} \\ & \text{units respectively, find } n-k. \\ & \\ & \text{Options: } A) 1344 \text{ } B) 1152 \text{ } C) 768 \text{ } D) 256\end{aligned}",
r"Ax + By = C",
r"f(x) = a(x - h)^2 + k",
r"{vertex} = (h, k)",
r"\text{fraction} = \frac{\text{part}}{\text{whole}}",
r"\text{percent} = \frac{\text{part}}{\text{whole}} \times 100",
r"\text{percent increase} = \text{multiply by }(1 + r)",
r"\text{percent decrease} = \text{multiply by }(1 - r)",
r"\text{distance} = \text{rate} \times \text{time}",
r"A = \pi r^2"
]

# Generate PNG files for each equation
for i, eq in enumerate(equations, 1):
    output_filename = f'equation_{i:02d}.png'
    render_math_to_png(eq, output_filename)
    print(f'Generated {output_filename}')