#!/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 starting with the regular image 
# fading into the display of the #hex values of the displayed pixels at the 
# middle of the series back into the regular image - 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
import Image
import ImageDraw
import ImageFont


#get files from this folder
filepath = "/path_to_single_frames_numbered/"
#save files here
savepath = "/path_where_to_save_the_processed_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 "there are", 2*semiseq, "frames to decode"
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
		
		canvas1  =  Image.new ( "RGBA", img.size , (0,0,0,0))
		print 'canvas=', canvas1.format, canvas1.size, canvas1.mode

		xmax = int(img.size[0])
		ymax = int(img.size[1])
		print xmax, ymax
		x = 0
		y = 0
		#loop n draw 
		while x < (xmax): #width/x pixel 
			while y < ymax:
				#print 'ypiksel=', y, 'xpixel =', x
				res = 10 
				rgb = img.getpixel ((x, y))
				lum = int(0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2])+72
				hex = "#%02X%02X%02X" % rgb
				#path to fonts you would like to be used
				#i used /usr/share/fonts/truetype/freefont/FreeMono.ttf
				font1  = ImageFont.truetype('/Path_to_the_font/font.ttf', lum)
				font2  = ImageFont.truetype('/Path_to_the_font/font.ttf', lum/2)
				font3  = ImageFont.truetype('/Path_to_the_font/font.ttf', lum/3)
				font4  = ImageFont.truetype('/Path_to_the_font/font.ttf', lum/4)
				draw  =  ImageDraw.Draw (canvas1)
				draw.text ((x-1, y-12) , hex[1:], font=font4, fill=rgb)
								
				y = y+res
			y = 0
			x = x+res
		
		im = img.convert("RGBA")
		mix = float(1)/semiseq
		middle = start + semiseq
		if i < middle:
			f = i-start
			mixed_canv = Image.blend (im, canvas1, f*mix)
		else:
			f = end-i
			mixed_canv = Image.blend (im,canvas1, f*mix)
			#f = i-(2*semiseq-start)
		print "there are ",end-i,"frames left"
		#mixed_canv = Image.blend (im, canvas1, 0.1)
		#mixed_canv.show()
		mixed_canv.save(("%s%06i%s" % (savepath, i , suffix)),"JPEG")
		
hexdraw()
