Spaces:
Running
Running
Monan Zhou
commited on
Commit
•
afd60f8
1
Parent(s):
f953ce3
Update convert.py
Browse files- convert.py +79 -88
convert.py
CHANGED
@@ -1,88 +1,79 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
import fitz
|
4 |
-
import subprocess
|
5 |
-
from PIL import Image
|
6 |
-
from music21 import converter
|
7 |
-
from utils import download
|
8 |
-
|
9 |
-
if sys.platform.startswith("linux"):
|
10 |
-
apkname = "MuseScore.AppImage"
|
11 |
-
extra_dir = "squashfs-root"
|
12 |
-
download(
|
13 |
-
filename=apkname,
|
14 |
-
url="https://master.dl.sourceforge.net/project/musescore.mirror/v4.2.0/MuseScore-4.2.0.233521125-x86_64.AppImage?viasf=1",
|
15 |
-
)
|
16 |
-
if not os.path.exists(extra_dir):
|
17 |
-
subprocess.run(["chmod", "+x", f"./{apkname}"])
|
18 |
-
subprocess.run([f"./{apkname}", "--appimage-extract"])
|
19 |
-
|
20 |
-
MSCORE = f"./{extra_dir}/AppRun"
|
21 |
-
os.environ["QT_QPA_PLATFORM"] = "offscreen"
|
22 |
-
|
23 |
-
else:
|
24 |
-
MSCORE = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
|
25 |
-
|
26 |
-
|
27 |
-
def
|
28 |
-
score = converter.parse(abc_content, format="abc")
|
29 |
-
score.write("
|
30 |
-
return
|
31 |
-
|
32 |
-
|
33 |
-
def
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
#
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
return output_path
|
81 |
-
|
82 |
-
|
83 |
-
def mxl2jpg(mxl_file: str):
|
84 |
-
pdf_score = mxl_file.replace(".mxl", ".pdf")
|
85 |
-
command = [MSCORE, "-o", pdf_score, mxl_file]
|
86 |
-
result = subprocess.run(command)
|
87 |
-
print(result)
|
88 |
-
return pdf_score, pdf2img(pdf_score)
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import fitz
|
4 |
+
import subprocess
|
5 |
+
from PIL import Image
|
6 |
+
from music21 import converter
|
7 |
+
from utils import download
|
8 |
+
|
9 |
+
if sys.platform.startswith("linux"):
|
10 |
+
apkname = "MuseScore.AppImage"
|
11 |
+
extra_dir = "squashfs-root"
|
12 |
+
download(
|
13 |
+
filename=apkname,
|
14 |
+
url="https://master.dl.sourceforge.net/project/musescore.mirror/v4.2.0/MuseScore-4.2.0.233521125-x86_64.AppImage?viasf=1",
|
15 |
+
)
|
16 |
+
if not os.path.exists(extra_dir):
|
17 |
+
subprocess.run(["chmod", "+x", f"./{apkname}"])
|
18 |
+
subprocess.run([f"./{apkname}", "--appimage-extract"])
|
19 |
+
|
20 |
+
MSCORE = f"./{extra_dir}/AppRun"
|
21 |
+
os.environ["QT_QPA_PLATFORM"] = "offscreen"
|
22 |
+
|
23 |
+
else:
|
24 |
+
MSCORE = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
|
25 |
+
|
26 |
+
|
27 |
+
def abc2xml(abc_content, output_xml_path):
|
28 |
+
score = converter.parse(abc_content, format="abc")
|
29 |
+
score.write("musicxml", fp=output_xml_path, encoding="utf-8")
|
30 |
+
return output_xml_path
|
31 |
+
|
32 |
+
|
33 |
+
def xml2(xml_path: str, target_fmt: str):
|
34 |
+
src_fmt = os.path.basename(xml_path).split(".")[-1]
|
35 |
+
if not "." in target_fmt:
|
36 |
+
target_fmt = "." + target_fmt
|
37 |
+
|
38 |
+
target_file = xml_path.replace(f".{src_fmt}", target_fmt)
|
39 |
+
command = [MSCORE, "-o", target_file, xml_path]
|
40 |
+
result = subprocess.run(command)
|
41 |
+
print(result)
|
42 |
+
return target_file
|
43 |
+
|
44 |
+
|
45 |
+
def pdf2img(pdf_path: str):
|
46 |
+
output_path = pdf_path.replace(".pdf", ".jpg")
|
47 |
+
doc = fitz.open(pdf_path)
|
48 |
+
# 创建一个图像列表
|
49 |
+
images = []
|
50 |
+
for page_number in range(doc.page_count):
|
51 |
+
page = doc[page_number]
|
52 |
+
# 将页面渲染为图像
|
53 |
+
image = page.get_pixmap()
|
54 |
+
# 将图像添加到列表
|
55 |
+
images.append(
|
56 |
+
Image.frombytes("RGB", [image.width, image.height], image.samples)
|
57 |
+
)
|
58 |
+
# 竖向合并图像
|
59 |
+
merged_image = Image.new(
|
60 |
+
"RGB", (images[0].width, sum(image.height for image in images))
|
61 |
+
)
|
62 |
+
y_offset = 0
|
63 |
+
for image in images:
|
64 |
+
merged_image.paste(image, (0, y_offset))
|
65 |
+
y_offset += image.height
|
66 |
+
# 保存合并后的图像为JPG
|
67 |
+
merged_image.save(output_path, "JPEG")
|
68 |
+
# 关闭PDF文档
|
69 |
+
doc.close()
|
70 |
+
return output_path
|
71 |
+
|
72 |
+
|
73 |
+
def xml2img(xml_file: str):
|
74 |
+
ext = os.path.basename(xml_file).split(".")[-1]
|
75 |
+
pdf_score = xml_file.replace(f".{ext}", ".pdf")
|
76 |
+
command = [MSCORE, "-o", pdf_score, xml_file]
|
77 |
+
result = subprocess.run(command)
|
78 |
+
print(result)
|
79 |
+
return pdf_score, pdf2img(pdf_score)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|