Music_LMMs / html_image.py
fistyee
update
099ffc5
raw
history blame
5.96 kB
import tempfile
import time
from selenium import webdriver
from PIL import Image
import os
import subprocess
import json
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
chrome_path = "/tmp/chrome-extract/opt/google/chrome/google-chrome"
# Function to install and configure Chrome without sudo
def setup_chrome():
chrome_url = "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"
chrome_deb_path = "/tmp/google-chrome-stable_current_amd64.deb"
subprocess.run(["wget", chrome_url, "-O", chrome_deb_path])
extract_dir = "/tmp/chrome-extract"
os.makedirs(extract_dir, exist_ok=True)
# Extract Chrome browser files
subprocess.run(["ar", "x", chrome_deb_path], cwd=extract_dir)
data_tar_path = os.path.join(extract_dir, "data.tar.xz")
subprocess.run(["tar", "xf", data_tar_path], cwd=extract_dir)
# Set Chrome browser path
os.environ["CHROME_BINARY"] = chrome_path
# Add Chrome path to system PATH
os.environ["PATH"] = f"{os.path.dirname(chrome_path)}:{os.environ['PATH']}"
# Install ChromeDriver
chromedriver_autoinstaller.install()
#return chrome_path
# Function to convert HTML content to an image
def html_to_image(html_content):
# Save HTML content in a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as html_file:
html_file.write(html_content.encode('utf-8'))
html_file_path = html_file.name
options = Options()
options.binary_location = chrome_path
options.add_argument("--headless") # For headless mode
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
# Start the Chrome browser
driver = webdriver.Chrome(service=ChromeService(), options=options)
# Load the HTML file
driver.get(f'file://{html_file_path}')
# Wait for the page to load
time.sleep(2)
# Get the page dimensions
width = driver.execute_script("return document.body.scrollWidth")
height = driver.execute_script("return document.body.scrollHeight")
# Resize window to the page size
driver.set_window_size(width, height)
# Capture screenshot and save as a PNG file
screenshot_path = tempfile.NamedTemporaryFile(delete=False, suffix='.png').name
driver.save_screenshot(screenshot_path)
# Close the browser
driver.quit()
return screenshot_path
def render_abc(abc_string):
abc_string = json.dumps(abc_string)
print("in render_abc")
html_template = f"""
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/abcjs@6.4.2/dist/abcjs-basic.min.js"></script>
</head>
<body>
<div id='abcjs-render' style='text-align: center;'></div>
<script>
var abcNotation = {abc_string};
ABCJS.renderAbc('abcjs-render', abcNotation);
</script>
</body>
</html>
"""
# return html to image
#screenshot_path = html_to_image(html_template)
#return screenshot_path
# return html
return html_template
# Initialize the environment
#chrome_path = setup_chrome()
# Example of how to call the function
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ABCJS Example</title>
<script src="https://cdn.jsdelivr.net/npm/abcjs@6.4.3/dist/abcjs-basic.min.js"></script>
</head>
<body>
<div id="abc"></div>
<script>
var abcNotation = `
X:1
T:菊次郎的夏天(Summer版本C)
%%score { 1 | 2 }
L:1/16
Q:1/4=114
M:4/4
I:linebreak $
K:C
V:1 treble nm="钢琴" snm="Pno."
V:2 bass
L:1/8
V:1
"^Moderato" z16 | z16 | z16 | z8 z4 .GI6J.cI7J.dI8J.eI9J |: %4
!>!dI10J2.cI11JcI12J- cI13J8 .GI14J.cI15J.dI16J.eI17J | %5
!>!dI18J2cI19J.dI20J z2 .eI22J2 !>!eI23J4 .GI24J.cI25J.dI26J.eI27J | %6
!>!dI28J2.cI29JcI30J- cI31J8 .GI32J.cI33J.dI34J.eI35J | %7
!>!dI36J2cI37J.dI38J z2 .gI40J2 !>!eI41J4 eI42J2fI43J2 | %8
!mf! [eI44JgI45J]2[eI46JgI47J][eI48J-gI49J-]- [eI50JgI51J]2gI52J2 gI53J2eI54JcI55J- cI56J2eI57JfI58J | %9
[eI59JgI60J]2[eI61JgI62J][eI63J-gI64J-]- [eI65JgI66J]2[eI67JgI68J]2 gI69J2eI70JcI71J- cI72J2cI73JdI74J | %10
eI75J2eI76JeI77J- eI78J2eI79J2 eI80J2aI81J2 (3!>!dI82JeI83JdI84JcI85J2 |1 %11
!>!dI86J8 z4 .GI88J.cI89J.dI90J.eI91J :|2 !>!cI92J8 z8 || z16 | z16 | z16 | %16
!>![G,I97JCI98JEI99J]16 |] %17
V:2
A,,I100J.E,I101J.A,I102J.E,I103J F,,I104J.C,I105J.F,I106J.C,I107J | %1
G,,I108J.D,I109J.G,I110J.D,I111J C,I112J.G,I113J.CI114J.G,I115J | %2
A,,I116J.E,I117J.A,I118J.E,I119J F,,I120J.C,I121J.F,I122J.C,I123J | %3
G,,I124J.D,I125J.G,I126J.D,I127J C,I128J.G,I129J.CI130J.G,I131J |: %4
A,,I132J.E,I133J.A,I134J.E,I135J F,,I136J.C,I137J.F,I138J.C,I139J | %5
G,,I140J.D,I141J.G,I142J.D,I143J C,I144J.G,I145J.CI146J.G,I147J | %6
A,,I148J.E,I149J.A,I150J.E,I151J F,,I152J.C,I153J.F,I154J.C,I155J | %7
G,,I156J.D,I157J.G,I158J.D,I159J C,I160J.G,I161J.CI162J.G,I163J | %8
G,,I164J.D,I165J.G,I166J.D,I167J F,,I168J.C,I169J.F,I170J.C,I171J | %9
E,,I172J.B,,I173J.E,I174J.B,,I175J A,,I176J.E,I177J.A,I178J.E,I179J | %10
D,,I180J.A,,I181J.D,I182J.A,,I183J F,,I184J2 z2 |1 %11
G,,I186J.D,I187J.G,I188J.D,I189J G,,I190J.D,I191J.G,I192J.D,I193J :|2 %12
C,I194JG,I195JCI196JG,I197J A,,I198JE,I199JA,I200JE,I201J || %13
F,,I202JC,I203JF,I204JC,I205J G,,I206JD,I207JG,I208JD,I209J | %14
C,I210JG,I211JCI212JG,I213J A,,I214JE,I215JA,I216JE,I217J | %15
F,,I218JC,I219JF,I220JC,I221J G,,I222JD,I223JG,I224JD,I225J | !>!C,I226J8 |] %17
`;
ABCJS.renderAbc("abc", abcNotation);
</script>
</body>
</html>
"""
#screenshot_path = html_to_image(html_content, chrome_path)
#print(f"Screenshot saved at: {screenshot_path}")