# Jonathan Frech; 13th of August, 22nd of September 2017

# import
from PIL import Image
import sys, os

# convert a given hsl color intor rgb
# (function written on the 11th of July 2017)
def hsl(h, s, l):
	# value range
	if not (0<=h<360 and 0<=s<=1  and 0<=l<=1): return
	
	c = (1-abs(2*l-1))*s
	hh = h/60.
	x = c*(1-abs(hh%2-1))
	r = g = b = 0
	if 0 <= hh < 1: r, g = c, x
	if 1 <= hh < 2: r, g = x, c
	if 2 <= hh < 3: g, b = c, x
	if 3 <= hh < 4: g, b = x, c
	if 4 <= hh < 5: r, b = x, c
	if 5 <= hh < 6: r, b = c, x
	m = l-c/2.
	return int((r+m)*255.), int((g+m)*255.), int((b+m)*255.)

# main function
def main():
	# input length
	if len(sys.argv) < 2: print "Please specify an image."; return
	
	# image
	try: img = Image.open(sys.argv[1])
	except: print "Could not open image '%s'." % sys.argv[1]; return
	w, h = img.size
	pix, j = img.load(), 0
	
	# rainbowify image
	for y in range(h):
		for x in range(w):
			v = sum(pix[x, y])/3./255.
			pix[x, y] = hsl((v*360+j)%360, 1, .5)
			j += 1./w/h*360
	
	# save image
	f, n = sys.argv[1].split("."), 1
	F = ".".join(f[:-1]) + "_rainbowified." + f[-1]
	while os.path.exists(F): n += 1; F = ".".join(f[:-1]) + "_rainbowified%d." % n + f[-1]
	try: img.save(F)
	except: print "Could not save image as '%s'." % F
	print "Successfully saved image as '%s'." % F

# run if main
if __name__ == "__main__": main()