Search code examples
pythonmusic21music-notation

Python music21 library create png from stream


I have a problem with generate png file from stream object in music21. I read documentation and I use ConverterLilypond to do this.

  chords = stream.Stream()

  d7 = chord.Chord(['D4', 'F4', 'A4', 'C5'])
  dmin7 = chord.Chord(['D4', 'F-4', 'A4', 'C5'])
  dmaj7 = chord.Chord(['D4', 'F#4', 'A4', 'C#5'])
  chords.append(d7)
  chords.append(dmin7)
  chords.append(dmaj7)
  conv = converter.subConverters.ConverterLilypond()
  conv.write(chords, fmt='lilypond', fp='file', subformats=['png'])

This code will generate png file, eps file count file, tex and texi file. Why? How can I generate only one file, PNG file?

When I try run this code in jupyter enough that I would use chords.show() to display image, but normally in the script the show generates a file, not a graphic.

EDIT:

Code with environment var

from music21 import *

us = environment.UserSettings()
us['lilypondPath'] = 'C:/Program Files (x86)/LilyPond/usr/bin/lilypond.exe'
us['musescoreDirectPNGPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'
us['musicxmlPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'

d7 = chord.Chord(['D4', 'F4', 'A4', 'C5'])

stream = stream.Stream()
stream.show('musicxml.png')

Solution

  • The best interface is by calling:

    chords.show('lily.png')

    which will run everything. But the Lilypond interface is not so strong, so better would be to install MuseScore and set the MusescoreDirectPNGPath in .music21rc to point to the mscore executable and the just call chords.show('musicxml.png') and it will do a much better translation via MusicXML.