MuGeminorum commited on
Commit
5265c7c
1 Parent(s): 653dc95

not use pdf2image

Browse files
Files changed (3) hide show
  1. app.py +0 -1
  2. render.py +24 -27
  3. requirements.txt +1 -1
app.py CHANGED
@@ -172,7 +172,6 @@ def generate_abc(args, region):
172
  create_dir('./tmp')
173
  timestamp = time.strftime("%a_%d_%b_%Y_%H_%M_%S", time.localtime())
174
  out_midi = abc_to_midi(tunes, f'./tmp/[{region}]{timestamp}.mid')
175
- add_path()
176
  png_file = midi2png(out_midi)
177
  wav_file = midi2wav(out_midi)
178
 
 
172
  create_dir('./tmp')
173
  timestamp = time.strftime("%a_%d_%b_%Y_%H_%M_%S", time.localtime())
174
  out_midi = abc_to_midi(tunes, f'./tmp/[{region}]{timestamp}.mid')
 
175
  png_file = midi2png(out_midi)
176
  wav_file = midi2wav(out_midi)
177
 
render.py CHANGED
@@ -1,30 +1,10 @@
1
  import os
2
  import sys
 
3
  import subprocess
4
  from PIL import Image
5
- from pdf2image import convert_from_path
6
  from utils import download
7
 
8
-
9
- def add_path():
10
- # """
11
- # 将指定目录添加到 LD_LIBRARY_PATH 环境变量中,并在当前 Python 进程中生效。
12
-
13
- # Parameters:
14
- # - directory_path (str): 要添加的目录路径。
15
- # """
16
- # dir_path = os.path.join(os.getcwd(), 'lib')
17
- # # 获取当前环境变量的值
18
- # current_path = os.environ.get("LD_LIBRARY_PATH", "")
19
-
20
- # # 将目录路径添加到 LD_LIBRARY_PATH 中
21
- # new_path = f"{current_path}:{dir_path}"
22
-
23
- # # 设置 LD_LIBRARY_PATH 环境变量,以便在当前 Python 进程中生效
24
- # os.environ["LD_LIBRARY_PATH"] = new_path
25
- os.environ['QT_QPA_PLATFORM'] = 'offscreen'
26
-
27
-
28
  if sys.platform.startswith('linux'):
29
  apkname = 'MuseScore.AppImage'
30
  extra_dir = 'squashfs-root'
@@ -37,6 +17,7 @@ if sys.platform.startswith('linux'):
37
  subprocess.run([f'./{apkname}', '--appimage-extract'])
38
 
39
  mscore = f'./{extra_dir}/AppRun'
 
40
 
41
  else:
42
  mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
@@ -52,16 +33,32 @@ def midi2wav(mid_file: str):
52
 
53
  def pdf_to_img(pdf_path: str):
54
  output_path = pdf_path.replace('.pdf', '.jpg')
55
- images = convert_from_path(pdf_path)
56
- combined_image = Image.new(
57
- 'RGB', (images[0].width, sum(image.height for image in images))
58
- )
 
 
 
 
 
 
 
 
 
 
 
 
59
  y_offset = 0
60
  for image in images:
61
- combined_image.paste(image, (0, y_offset))
62
  y_offset += image.height
63
 
64
- combined_image.save(output_path)
 
 
 
 
65
  return output_path
66
 
67
 
 
1
  import os
2
  import sys
3
+ import fitz
4
  import subprocess
5
  from PIL import Image
 
6
  from utils import download
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  if sys.platform.startswith('linux'):
9
  apkname = 'MuseScore.AppImage'
10
  extra_dir = 'squashfs-root'
 
17
  subprocess.run([f'./{apkname}', '--appimage-extract'])
18
 
19
  mscore = f'./{extra_dir}/AppRun'
20
+ os.environ['QT_QPA_PLATFORM'] = 'offscreen'
21
 
22
  else:
23
  mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
 
33
 
34
  def pdf_to_img(pdf_path: str):
35
  output_path = pdf_path.replace('.pdf', '.jpg')
36
+ doc = fitz.open(pdf_path)
37
+
38
+ # 创建一个图像列表
39
+ images = []
40
+
41
+ for page_number in range(doc.page_count):
42
+ page = doc[page_number]
43
+ # 将页面渲染为图像
44
+ image = page.get_pixmap()
45
+ # 将图像添加到列表
46
+ images.append(Image.frombytes(
47
+ "RGB", [image.width, image.height], image.samples))
48
+
49
+ # 竖向合并图像
50
+ merged_image = Image.new(
51
+ "RGB", (images[0].width, sum(image.height for image in images)))
52
  y_offset = 0
53
  for image in images:
54
+ merged_image.paste(image, (0, y_offset))
55
  y_offset += image.height
56
 
57
+ # 保存合并后的图像为JPG
58
+ merged_image.save(output_path, "JPEG")
59
+
60
+ # 关闭PDF文档
61
+ doc.close()
62
  return output_path
63
 
64
 
requirements.txt CHANGED
@@ -5,5 +5,5 @@ music21
5
  autopep8
6
  pillow==9.4.0
7
  gradio
8
- pdf2image
9
  torch
 
5
  autopep8
6
  pillow==9.4.0
7
  gradio
8
+ pymupdf
9
  torch