Code Monkey home page Code Monkey logo

img2midi's Introduction

img2midi

You too can be a synesthete! With this python script You can create simple melodies, or cacophonies, from images. Yes, it means You can actualy hear the images.

Requirements

Usage

  1. Clone or download
  2. In cmd type: python img2midi.py input.jpg song.midi --play
  3. Enjoy

input and output are required to run script, play flag is optional. Script will create midi file. Please note: The larger the input image file the longer final song will be.

Math behind the conversion

Converter uses naive way of converting pixel colors to midi notes. Each pixel's color consists of RGB components and each of those components has a value in range [0; 255]. Midi notes can take any value from 21 - A0 note(low A, 0th octave), to 108 - C8 (high C, 8th octave) note, therefore conversion between those ranges must be made. For simplicity I arbitrarily decided that, final song's tempo will be 240 BPM.

Color to pitch conversion is made using following algorithm for every pixel:

  1. Take average of RGB color components of a pixel. This will be value in range (0; 255)
  2. Divide it by 2, this value was arbitrarily decided, so that final notes will be less spread out on a scale
  3. Normalize it by dividing by 255
  4. Using linear interpolation convert normalized pixel value to note.
  5. This is a final midi note value in range [21; 108]

Base note duration was chosen to be 1 beat. However if the same value is repeated in the final song data 1 beat is added for each repetition.

Example:

[50, 50, 50, 80, 40, 40, 50, 50] will result in 4 notes:

  • D3(50) with duration of 3 beats
  • G#5(80) with duration of 1 beat
  • E2(40) with duration of 2 beats
  • D3(50) with duration of 2 beats

img2midi's People

Contributors

vooydzig avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.