#!/usr/bin/python2.3
# this code is written in 2004 by sesselastronaut@gmail.com & is very basic
# but it's working...
# check the output on http://hasa-labs.org/orbit_en/pixelstudy.php
# it processes a series of singleframes:
# 1. it draws the #hex-values of the pixels relocated in a coordinate system - 
# rgb value = xyz 
# 2. it draws pixels represented as colored arcs - radius due to luminanz & 
# deformation due to differenz of neighbourpixels. though i've tried to 
# comment it a bit i might be the only person who understands what's going on, 
# so please do not hesitate to contact me if you've troubles or questions.  

#import
import os, sys, math
import Image
import ImageDraw
import ImageFont


#get files from this folder
filepath = "/path_to_single_frames_numbered/"
#save files displayed in coordinate system here
savepath = "/path_where_to_save_the_processed_rgb_frames/"
#save files displayed as arcs here
savepath = "/path_where_to_save_the_processed_arc_frames/"
#number of startframe
start = 10
#number of endframe
end = 696
suffix = ".jpg"
#middle of the 'movie' - starting to fade back from displaying hex-values to the regular image
semiseq = ((end - start)/2)
print "das ding brauch", semiseq, "*2 frames"

#converts a point(x,y,z) in 3-dim for 2-dim display(x,y)
def z_2dim(x,y,z):
	        degrees = -30
		angle = degrees * 2 * math.pi /360.0
		x = x+math.cos(angle)*z
		y = y+math.sin(angle)*z
		return x,y
					
def hexdraw():
	for i in range(start, end):
		# %s - string, %08i - integer mit 8 führenden nullen, %s - string
		filename = "%s%06i%s" % (filepath, i, suffix)
		print "File: %s"% file, "Frame=", i

		##image openeer
		img = Image.open (filename)
		print 'image=', img.format, img.size, img.mode
		
		#arc canvas
		arc_canvas = Image.new ( "RGB", (800,240) , (185,200,230) )
		#rgb canvas
		rgb_canvas = Image.new ( "RGB", (800,720) , (185,200,230) )
		print 'canvas=', arc_canvas.format, arc_canvas.size, arc_canvas.mode

		xmax = int(img.size[0])
		ymax = int(img.size[1])
		print xmax, ymax
		x = 0
		y = 0
		#offset
		osx = 730
		osy = 150
		lum2 = 0
		v = -250
		
		#loop n draw
		while x < (xmax): #width/x pixel 
			while y < ymax:
				#print 'ypiksel=', y, 'xpixel =', x
				res = 15 
				rgb = img.getpixel ((x, y))
				lum = int(0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2])
				hex = "#%02X%02X%02X" % rgb
				#path to fonts you would like to be used
				#i used /usr/share/fonts/truetype/freefont/FreeMono.ttf
				font  = ImageFont.truetype('/Path_to_the_font/font.ttf', lum/4)
		
				draw2  =  ImageDraw.Draw (arc_canvas)
				draw =  ImageDraw.Draw (rgb_canvas)
				draw =  ImageDraw.Draw (arc_canvas)
				xy = z_2dim(rgb[0],rgb[1],rgb[2])
				#print "xyz2xy=", xy
				#luminance diffrence between this and pixel before
				lum_diff = lum-lum2
				draw2.text ((xy[0]+10,xy[1]+10), hex[:6], font=font, fill=rgb)
				draw2.line (((10,10),(xy[0]+10,xy[1]+10)), fill=rgb)
				
				x1=(osx+x-((xy[0]+lum-lum_diff)/2))/2
				y1=(osy+y-((xy[1]+lum)/2))/2
				x2=(osx+x+((xy[0]+lum-lum_diff)/2))/2
				y2=(osy+y+((xy[1]+lum)/2))/2
				draw.arc((x1,y1,x2,y2), 0, 360, fill=rgb)
				lum2 = lum
								
				y = y+res
			y = 0
			x = x+res
		
		#im = img.convert("RGBA")
		arc_canvas.save(("%s%06i%s" % (savepath, i , suffix)),"JPEG")
		rgb_canvas.save(("%s%06i%s" % (savepath2, i , suffix)),"JPEG")
		
		#canvas1.show ()
hexdraw()
