-
Notifications
You must be signed in to change notification settings - Fork 3
/
midi.py
52 lines (40 loc) · 1.32 KB
/
midi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
import sys
from PIL import Image
from old_midiutil.MidiGenerator import MidiGenerator
from old_midiutil.TrackGen import LoopingArray
filename = sys.argv[1]
midifilename = filename[0:filename.find('.')]+'.midi'
#load the image and get it's dimensions
im = Image.open(filename)
pix = im.load()
width, height = im.size
#one list for each color channel rgb
red = []
green = []
blue = []
last_g = 50
#go over the image pixel by pixel
for y in range(0, height):
for x in range(0, width):
r, g, b = pix[x, y]
red.append([r, g, b]) #get the notes
green.append(((g+16)/255.0, last_g/255.0)) #get the duration of each note
last_g = b+16
blue.append(b) #velocities
duration = 3000
from_note = int(len(red)/2-duration)
to_note = from_note+duration
le_red = red[from_note:to_note]
le_green = green[from_note:to_note]
le_blue = blue[from_note:to_note]
#create the midi file
midiGenerator = MidiGenerator(filename=midifilename, tempo=150)
notes = LoopingArray(le_red)
beats = LoopingArray(le_green)
velocities = LoopingArray(le_blue)
#print(notes.arr)
#raise
#use the midigenerator to create the track with the image's pixel data
midiGenerator.add_track(0, 0, beat=beats, notes=notes, velocities=velocities, length=duration)
midiGenerator.write()