Obtaining x-y coordenates of notes

62 views
Skip to first unread message

Enrique Gutierrez

unread,
Jan 23, 2023, 1:12:57 PM1/23/23
to music21
Could someone help me with an example of how to get the x-y coordinates of the notes of a score within the layout? In the documentation (here) I have seen that the `LayoutScore` class can be useful for this, but I have not come up with a way to do it.

I want to do this because I then plan to use some external pdf annotation library to highlight with colored rectangles certain fragments.

Michael Scott Cuthbert

unread,
Jan 23, 2023, 7:47:31 PM1/23/23
to music21list
Hi Enrique,

You’re right that the layout module should be the place to do this and you’re also right that you can’t find it.

Most of layout.py was written in a 36-hour sprint for a specific project at the 2013 Classical Music Hack Day in Vienna where Vladimir Viro and I created a tool Particellissima (sadly not online anymore) that let you give a musicxml file and it would render a short-score with only the 5 most relevant staves shown at a time (a particell is another name for a short score).  And it could do the same with a scanned sheet of music.  Since we only needed to know precise X and Y coordinates on a per-measure basis, the tool only works up to that level of granularity.  (Getting the precise bounding box areas for musical notes is a major challenge; things like beams and articulations certainly don’t help; and there’s no MusicXML to image converter that respects exact placement of notes from an xml file, at least none that I know of.  I wish the SmartMusic .enf format were public).  

But for your annotation library, if working at a measure-sized level is sufficient, then it should be possible.

In case looking at a bunch of undocumented code from 2013 (written in Python 2 for music21 v1 or 2) is helpful — there’s link to the scripts that we used to generate json data to annotate scores with.  Beyond that, I can’t really help — I don’t understand the code myself, and a lot of it was written at 4am while jetlagged.

Best,
Myke


On Jan 23, 2023, at 08:12, Enrique Gutierrez <enrique.gu...@gmail.com> wrote:

Could someone help me with an example of how to get the x-y coordinates of the notes of a score within the layout? In the documentation (here) I have seen that the `LayoutScore` class can be useful for this, but I have not come up with a way to do it.

I want to do this because I then plan to use some external pdf annotation library to highlight with colored rectangles certain fragments.

--
--
To Post: email music...@googlegroups.com
To Unsubscribe: email music21list...@googlegroups.com
Archives, Options, etc: http://groups.google.com/group/music21list

---
You received this message because you are subscribed to the Google Groups "music21" group.
To unsubscribe from this group and stop receiving emails from it, send an email to music21list...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/music21list/4a1084f4-f5fd-45e7-b109-b1a41ff63405n%40googlegroups.com.

Enrique Gutierrez

unread,
Jan 24, 2023, 8:38:04 AM1/24/23
to music21
funny story :) Thanks for the detailed clarification. I will take a look at the files you sent me. Although first I think I'll try a workaround I came up with yesterday: (1) write certain lyrics at the beginning and end of the fragment to highlight, (2) convert to pdf, (3) detect the position of those lyrics with some OCR library, (4) remove them and replace them with a colored rectangle.

Best,

Michael Scott Cuthbert

unread,
Jan 24, 2023, 1:23:37 PM1/24/23
to music21list
That’s a brilliant solution to the problem of not getting accurate X-Y coordinates mapped to notes; I’ll have to use it some time.

Also spurring me to make better music21 (python) and music21j (Javascript) integration a priority in v.10 — music21j uses Vexflow as an output format which does give pixel-perfect X-Y location for all musical elements.

Best,
Myke


Andrew Hankinson

unread,
Jan 25, 2023, 3:27:58 AM1/25/23
to music...@googlegroups.com
It might also be worth mentioning that Verovio will output SVG directly from MusicXML, so you can build a pipeline of music21 -> MusicXML -> Verovio -> SVG to JPEG or PNG -> x/y co-ordinates.

If you're happy working within the SVG co-ordinate space (since it's a vector, not a raster, format) then you can probably skip that last step. 

There is a Python module for Verovio. "pip install verovio" should work. 

Laurent Pugin

unread,
Jan 25, 2023, 7:13:23 AM1/25/23
to music...@googlegroups.com
For people interested to see how that could work I created a very basic Jupyter notebook that passes the music21 output to Verovio: https://github.com/lpugin/nb-test/blob/main/example.ipynb

It directly displays the SVG, but as Andrew said, you can very well convert it to an image if you need that (e.g., with cairosvg)

You should be able to see it running here https://mybinder.org/v2/gh/lpugin/nb-test/main

Laurent

Enrique Gutierrez

unread,
Jan 30, 2023, 9:29:34 AM1/30/23
to music21
Thanks guys. In the end I went for the solution I outlined in my previous post: it works like a charm. Attached is a sample document. I used the package "PyMuPDF". As you can see, in the end I decided to highlight the different patterns under the score, instead of on top of it. I thought it would be less invasive, especially when there are several overlapping patterns. However, highlighting the patterns on the score shouldn't require too much extra work as long as you have the width of the staff.

Despite all this, I think the "professional" option would indeed be to go to JavaScript, where you could get selective highlighting for certain patterns and other interesting interactions. But well, for my particular application, what I have is more than enough.

Thanks for your insights!

test2.pdf
Reply all
Reply to author
Forward
0 new messages