Monan Zhou commited on
Commit
afd60f8
1 Parent(s): f953ce3

Update convert.py

Browse files
Files changed (1) hide show
  1. 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 abc_to_midi(abc_content, output_midi_path):
28
- score = converter.parse(abc_content, format="abc")
29
- score.write("midi", fp=output_midi_path)
30
- return output_midi_path
31
-
32
-
33
- def abc_to_musicxml(abc_content, output_xml_path):
34
- score = converter.parse(abc_content, format="abc")
35
- score.write("musicxml", fp=output_xml_path)
36
- return output_xml_path
37
-
38
-
39
- def musicxml_to_mxl(xml_path):
40
- mxl_file = xml_path.replace(".musicxml", ".mxl")
41
- command = [MSCORE, "-o", mxl_file, xml_path]
42
- result = subprocess.run(command)
43
- print(result)
44
- return mxl_file
45
-
46
-
47
- def midi2wav(mid_file: str):
48
- wav_file = mid_file.replace(".mid", ".wav")
49
- command = [MSCORE, "-o", wav_file, mid_file]
50
- result = subprocess.run(command)
51
- print(result)
52
- return wav_file
53
-
54
-
55
- def pdf2img(pdf_path: str):
56
- output_path = pdf_path.replace(".pdf", ".jpg")
57
- doc = fitz.open(pdf_path)
58
- # 创建一个图像列表
59
- images = []
60
- for page_number in range(doc.page_count):
61
- page = doc[page_number]
62
- # 将页面渲染为图像
63
- image = page.get_pixmap()
64
- # 将图像添加到列表
65
- images.append(
66
- Image.frombytes("RGB", [image.width, image.height], image.samples)
67
- )
68
- # 竖向合并图像
69
- merged_image = Image.new(
70
- "RGB", (images[0].width, sum(image.height for image in images))
71
- )
72
- y_offset = 0
73
- for image in images:
74
- merged_image.paste(image, (0, y_offset))
75
- y_offset += image.height
76
- # 保存合并后的图像为JPG
77
- merged_image.save(output_path, "JPEG")
78
- # 关闭PDF文档
79
- doc.close()
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)