Skip to content

Instantly share code, notes, and snippets.

@hhsprings
Created January 2, 2019 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhsprings/ba91e293ce51063c6dc1b1e4df0e0a3d to your computer and use it in GitHub Desktop.
Save hhsprings/ba91e293ce51063c6dc1b1e4df0e0a3d to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import math
from PIL import Image, ImageDraw, ImageFont
#
# see details: https://en.wikipedia.org/wiki/MIDI_tuning_standard
#
_KTAB = [
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
]
def _nn2scale(nn):
return _KTAB[int(nn) % 12], int(nn / 12) - 1
def _ok2nn(k, oct):
return (oct + 1) * 12 + _KTAB.index(k)
def _nn2freq(nn):
return math.pow(2, (nn - 69) / 12.) * 440
#def _freq2nn(f):
# return 69 + 12 * math.log(f / 440., 2)
def _img_hstack(img_l, img_r):
newimg = Image.new("RGB", (img_l.width + img_r.width, img_l.height))
newimg.paste(img_l, box=(0, 0, img_l.width, img_l.height))
newimg.paste(img_r, box=(img_l.width, 0, img_r.width + img_l.width, img_r.height))
return newimg
if __name__ == '__main__':
# ticks from A0(21) to C8(108)
ticks_info = []
for nn in range(21, 109):
k, oct = _nn2scale(nn)
freq = _nn2freq(nn)
ticks_info.append(
(k, oct, nn, freq))
#
fnt = ImageFont.truetype('msgothic.ttc', 80)
result = None
imh = 120
for i in range(len(ticks_info) - 1):
k, oct, nn, freq = ticks_info[i]
w = 30
if k.endswith("#"):
w *= 2
img = Image.new("RGB", (w, imh), "#ffffff")
dctx = ImageDraw.Draw(img)
dctx.line([
(0, 0), (w, 0),
], fill="#000000", width=10)
dctx.line([
(0, imh), (w, imh),
], fill="#000000", width=10)
if k.endswith("#"):
dctx.rectangle([
(0, 0),
(w / 2, imh / 3 * 2),
], fill="#000000", outline="#222222")
else:
txt = k
txtsz = dctx.textsize(txt, fnt)
dctx.text(
((img.width - txtsz[0]) / 2, img.height - txtsz[1] - 20),
txt,
font=fnt,
fill="#4444ff"
)
if k == "C":
txt = "%d" % oct
txtsz = dctx.textsize(txt, fnt)
dctx.text(
((img.width - txtsz[0]) / 2, img.height - txtsz[1] - 60),
txt,
font=fnt,
fill="#ff4444"
)
del dctx
if result is None:
result = img
else:
result = _img_hstack(result, img)
result = result.resize((1920, result.height))
result.save("myaxis.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment