midi2img
midi2img copied to clipboard
MIDI to image and image to MIDI conversion scripts
MIDI to Image conversion
Convert MIDI files into piano roll images and back with this Python script.
Demo
Here is a video demonstration using the program to generate midis and images, then we use it for an artistic performance.
Implementation Details
This script relies on the music21 library to parse and create MIDI files. The expected images are black and white and have a fixed size of 106 rows and 100 columns. 106 is the number of different notes (From 21 (A0
) to 127 (G9
)) and 100 is the length of the song, if a song is longer than that the script splits the song and generates several images. Each image represents one instrument, if a song has several instruments different images are generated for each. Each pixel represents a note played for 1/4 beats, this can be changed with the resolution
variable.
Requirements
- python 3
- python packages (Try something like:
pip install -r requirements.txt
)- music21
- imageio
- numpy
- Pillow
Usage
There are two scripts, one for converting a MIDI into images and the other one to convert an image into a MIDI.
MIDI to Image
REPETITIONS is an optional parameter which indicates, in case the song is too long, how many parts of the song to convert into an image. If it is 1 it only extracts the first part of the song. If you don't specify it then it generates as many images as needed to cover all the song.
python midi2img.py MIDI_FILE_PATH REPETITIONS
python midi2img.py samples/zelda.mid 1
Using the sample zelda midi the generated image for the first part of the song looks like this

Image to MIDI
Converts image of size (106,100) into a midi file.
python img2midi.py IMAGE_PATH
python img2midi.py samples/image.png
Using the image from the sample folder it generates as expected a midi file with decreasing and increasing notes.
