#!/usr/bin/python
#
# moodfiles-to-image.py
# Creates images of .mood files in a specified directory
# 
# Copyright 2008 Damian Frick <mozork@mozork.de>
# based on a script by Thomas Perl <thp@perli.net>,
# that can be found here: http://lists.kde.org/?l=amarok&m=119107376216048&w=2
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# For more information about the GNU General Public License,
# see <http://www.gnu.org/licenses/>.
#
# This program reads in ".mood" files that have been
# created with the "moodbar" utility from
#
#   http://amarok.kde.org/wiki/Moodbar
#
# The moodbar file is a simple binary file with 
# three bytes per frame: red, green and blue.
# 
# USAGE: modfiles-to-image.py my-moodfiles-directory width height
# Creates .jpg images of all .mood files in the "my-moodfiles-directory" 
# width specifies the width of the images to be created, height accordingly.
# width and height are optional and by default 1000 respectively width/10.
# The image files will be placed in the directory the .mood files are in.
#

import sys
import os.path
import Image, ImageDraw

USAGE = 'Usage: modfiles-to-image.py my-moodfiles-directory width height\nwidth and height are optional, the default values are 1000 respectively width/10.'

if len(sys.argv) == 2 and os.path.exists(sys.argv[-1]):
  dn = sys.argv[-1]
  width = 1000
  height = 100
elif len(sys.argv) == 3 and os.path.exists(sys.argv[-2]):
  dn = sys.argv[-2]
  width = int(sys.argv[-1])
  if width < 1:
    print USAGE
    print 'width must be a positive value.'
    sys.exit(-1)
  height = width/10
elif len(sys.argv) == 4 and os.path.exists(sys.argv[-3]):
  dn = sys.argv[-3]
  width = int(sys.argv[-2])
  height = int(sys.argv[-1])
  if width < 1 or height < 1:
    print USAGE
    if width < 1:
      print 'width must be a positive value.'
    if height < 1:
      print 'height must be a positive value.'
    sys.exit(-1)
else:
  print USAGE
  sys.exit( -1)

tempHeight = (1000*height)/width

def moodbar_from_file(fn):
  fp = open(fn, 'rb')
  result = []
  s = fp.read(3)
  while s:
    result.append((ord(s[0]),ord(s[1]),ord( s[2])))
    s = fp.read(3)
  fp.close()
  return result

def moodfile_to_image(fn):

  bars = moodbar_from_file(fn)

  image = Image.new("RGB", (len(bars), tempHeight), "#FFFFFF")
  draw =  ImageDraw.Draw(image)

  for x in range(len(bars)):
    draw.line((x, 0, x, tempHeight), fill=bars[x])
    
  image.thumbnail((width, height), Image.ANTIALIAS)
  path = fn.replace(".mood", "")
  path += ".jpg"

  image.save(path)
  return

if not dn.endswith("/"):
  dn += "/"

print 'Starting...'

counter = 0
for infile in os.listdir(dn):
  if infile.endswith(".mood"):
    counter += 1
    print infile
    moodfile_to_image(dn + infile)
    
print 'Done.'
print '\n' + str(counter) + ' images created.'

