import os import sys import subprocess from PIL import Image from pdf2image import convert_from_path from utils import download def add_path(): # """ # 将指定目录添加到 LD_LIBRARY_PATH 环境变量中,并在当前 Python 进程中生效。 # Parameters: # - directory_path (str): 要添加的目录路径。 # """ # dir_path = os.path.join(os.getcwd(), 'lib') # # 获取当前环境变量的值 # current_path = os.environ.get("LD_LIBRARY_PATH", "") # # 将目录路径添加到 LD_LIBRARY_PATH 中 # new_path = f"{current_path}:{dir_path}" # # 设置 LD_LIBRARY_PATH 环境变量,以便在当前 Python 进程中生效 # os.environ["LD_LIBRARY_PATH"] = new_path os.environ['QT_QPA_PLATFORM'] = 'offscreen' if sys.platform.startswith('linux'): apkname = 'MuseScore.AppImage' extra_dir = 'squashfs-root' download( filename=apkname, url='https://cdn.jsdelivr.net/musescore/v4.2.0/MuseScore-4.2.0.233521125-x86_64.AppImage' ) if not os.path.exists(extra_dir): subprocess.run(['chmod', '+x', f'./{apkname}']) subprocess.run([f'./{apkname}', '--appimage-extract']) mscore = f'./{extra_dir}/AppRun' else: mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe" def midi2wav(mid_file: str): wav_file = mid_file.replace('.mid', '.wav') command = [mscore, "-o", wav_file, mid_file] result = subprocess.run(command) print(result) return wav_file def pdf_to_img(pdf_path: str): output_path = pdf_path.replace('.pdf', '.jpg') images = convert_from_path(pdf_path) combined_image = Image.new( 'RGB', (images[0].width, sum(image.height for image in images)) ) y_offset = 0 for image in images: combined_image.paste(image, (0, y_offset)) y_offset += image.height combined_image.save(output_path) return output_path def midi2png(mid_file: str): pdf_score = mid_file.replace('.mid', '.pdf') command = [mscore, "-o", pdf_score, mid_file] result = subprocess.run(command) print(result) return pdf_to_img(pdf_score)