#!/usr/bin/ruby -w # Matt Comi # www.bigbucketblog.com # Usage: oscillofy INPUT_IMAGE OUTPUT_PCM # Your input image should be 255x255 and black and white (not grayscale) for # best results. # The resultant PCM will be 8-bit unsigned. # RMagick is required. require 'RMagick' include Magick if ARGV.length != 2 print "Usage: oscillofy.rb INPUT_IMAGE OUTPUT_PCM\n" else image = Image::read(ARGV[0]).first; image.resize!(255, 255) pixels = image.get_pixels(0, 0, image.columns, image.rows) f = File.new(ARGV[1], "w+") arr = Array.new() arrayIndex = 0 image.rows.times do |y| image.columns.times do |x| index = image.columns * y + x pixel = pixels[index] if pixel.red != 255 && pixel.green != 255 && pixel.blue != 255 # flip it vertically arr << 255 - y arr << x end end end data = arr.pack("C*") f.write(data) end