5

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

Alex S
  • 4,726
  • 7
  • 39
  • 67
  • try `fmt = 'musicxml.png'` -- you need to convert it to musicxml and then have the helper-converter convert it from there to png, using musescore. Or with lilypond (`fmt='lily.png'`) – Michael Scott Asato Cuthbert Dec 26 '18 at 21:04

2 Answers2

1

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.

  • This worked for me. the write method returned the file path for the png when using the musicxml.png argument – DavidD003 Jan 02 '23 at 21:26
0

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.

  • It does help, try to include some example of it working next time I am up voting it anyway welcome to SO – Bruno Jan 07 '20 at 05:44
  • 1
    `n.show('lily.png')` throws the same error that Op gets. – Cerin May 10 '20 at 19:16
  • In general, you'll get much better results with MuseScore and the musicxml.png format. The main music21 dev team hasn't worked on the Lilypond code for over a decade (none of us are fluent in Scheme) – Michael Scott Asato Cuthbert Jan 08 '23 at 20:33