File size: 1,211 Bytes
401e785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import subprocess
from ms import MSCORE
import fitz
from PIL import Image


def abc2xml(filename_base):

    abc_filename = f"{filename_base}.abc"
    subprocess.run(
        ["python", "abc2xml.py", '-o', '.', abc_filename, ],
        check=True,
        capture_output=True,
        text=True
    )


def xml2(filename_base, target_fmt):

    xml_file = filename_base + '.xml'
    if not "." in target_fmt:
        target_fmt = "." + target_fmt

    target_file = filename_base + target_fmt
    command = [MSCORE, "-o", target_file, xml_file]
    result = subprocess.run(command)
    return target_file


def pdf2img(filename_base, dpi=300):

    pdf_path = f"{filename_base}.pdf"
    doc = fitz.open(pdf_path)
    img_list = []
    
    for page_num in range(len(doc)):
        page = doc.load_page(page_num)
        # 创建高分辨率矩阵
        matrix = fitz.Matrix(dpi/72, dpi/72)
        pix = page.get_pixmap(matrix=matrix)
        
        # 转换为PIL Image
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        img_list.append(img)

    return img_list


# if __name__ == '__main__':
#     pdf2img('20250304_200811_Baroque_Bach, Johann Sebastian_Choral')