-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.py
80 lines (64 loc) · 2.91 KB
/
renderer.py
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
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
import verovio, os
from cairosvg import svg2png
from data import Data
from PIL import Image, ImageOps
class Renderer:
def renderScoreWithOtherFont(path):
os.system(f"verovio -r verovio/data --handwritten-font petaluma --font petaluma {path}")
def renderScore(path, output):
width = Renderer.krnToSvg(path, output)
Renderer.svgtopng(output, "example.png", width)
def countLines(path):
with open(path, 'r') as fp:
n_lines = len(fp.readlines())
return n_lines
def krnToSvg(path, output):
tk = verovio.toolkit()
# if multivoice
#options = { "pageHeight": 400, "pageWidth": 2100 }
# if solo voice
n_lines = Renderer.countLines(path)
width = n_lines * 35
# if width > 3000:
# width = width - (width//5)
# options = { "pageWidth": width , "scale": 65 }
# else:
if width < 2100:
width = 2100
#options = { "pageWidth": width, 'font': 'bravura', 'smuflTextFont': 'bravura' }
options = {"pageWidth": width}
tk.setOptions(options)
tk.loadFile(path)
tk.getPageCount()
tk.renderToSVGFile(output, 1)
return width
def svgtopng(input, output, width):
# Código maría
# with open(input, mode="r") as input_file:
# svg_stream = input_file.read()
# # See https://github.com/Kozea/CairoSVG/issues/300 for the reason why we have to replace inherit with visible here
# svg_stream = svg_stream.replace("overflow=\"inherit\"", "overflow=\"visible\"")
# svg2png(bytestring=svg_stream, background_color="transparent", write_to=output)
with open(input, mode="r") as input_file:
svg_stream = input_file.read()
svg_stream = svg_stream.replace("overflow=\"inherit\"", "overflow=\"visible\"")
#svg2png(bytestring=svg_stream, background_color="transparent", write_to=output)
svg2png(bytestring=svg_stream, background_color="white", write_to=output)
img = Image.open(output)
# First we crop to 300 because of the watermark of verovio engraving in the page's bottom
img = img.crop((0, 0,width,300))
img = ImageOps.invert(img)
area = img.getbbox()
img = img.crop(area)
img = ImageOps.invert(img)
img.save(output)
# change background and crop with the img.getbbox coordinates
if Data.change_background:
other_bg_output = "pruebaFondo.png"
Data.change_color()
svg2png(bytestring=svg_stream, background_color=Data.bg_color, write_to=other_bg_output)
img = Image.open(other_bg_output)
img = img.crop(area)
img.save(other_bg_output)
if __name__ == "__main__":
Renderer.svgtopng()