Alex S
Alex S

Reputation: 4884

Saving image files from Music21

I've downloaded a bunch of .krn files, and I'd like to convert them into images - either pngs or jpgs - using music21. I've tried this:

When I do this:

from music21 import *   
op = krnfile
s = converter.parse(op)
s.show()

I see a great image file in the Jupyter Notebook I'm using, but when I try to save that file programatically like this:

s.write(fp = 'outputfile.png', fmt = 'png')

It says:

Music21ObjectException: cannot support showing in this format yet: png

Which seems a little weird since it obviously manages to make an image for display in the notebook.

It looks like maybe I could use LilypondConverter.createPNG(fileName=None) from this, but is installing Lilypond required? I already have MuseScore2 installed, which opens when I call s.show().

Thanks a lot! Alex

Upvotes: 5

Views: 1418

Answers (2)

Install musescore on your computer, re-run python -m music21.configure to help it find it and then do:

from music21 import *   
op = 'krnfile.krn'
s = converter.parse(op)
fp = s.write('musicxml.png')
# or just s.show('musicxml.png') to test that it works.

If it's a multi-page file, fp will be the path to the first page. It will end in -1 or -01 or -001 etc. You can read through the directory to find other files with the same name until there are no more to get all the images.

Upvotes: 1

Luiz Henrique
Luiz Henrique

Reputation: 9

If you use n.show('lily.png'), it should create a temporary png file somewhere. Try to use it and an image may open.

Sorry i don't know much yet, I hope it helps.

Upvotes: 0

Related Questions