def computeMeanValues(args):
	
	outfile = open("ImageMeanValues1.txt", 'w')
	
	goodMeans = []
	yellowedMeans = []
	
	for i in range(3118,4116):
		
		image = []
		
		try:
			if i < 10:
				
				image = dia.openImage("DJI0000" + str(i) + ".JPG")
				print "DJI0000" + str(i) + ".JPG"
				
			elif i >= 10 and i < 100:
				
				image = dia.openImage("DJI000" + str(i) + ".JPG")
				print "DJI000" + str(i) + ".JPG"	
				
			elif i >= 100 and i < 1000 :
				
				image = dia.openImage("DJI00" + str(i) + ".JPG")
				print "DJI00" + str(i) + ".JPG"
				
			else:
								
				image = dia.openImage("DJI0" + str(i) + ".JPG")
				print "DJI0" + str(i) + ".JPG"
			
			meanVector = dia.meanVector(image)
			
			if meanVector[2] > 80:
				
				goodMeans.append(meanVector[2])
				
			else:
				
				yellowedMeans.append(meanVector[2])
			
			
			outfile.write("Image Name: DJI00" + str(i) + ".JPG\n")
			outfile.write("Image Mean Vector\n")
			outfile.write(str(meanVector))
			outfile.write('\n\n')
			
		except AttributeError:
			
			print "DJI00" + str(i) + ".JPG not found"

	goodMeans = np.array(goodMeans, dtype='uint64')
	yellowedMeans = np.array(yellowedMeans, dtype='uint64')
			
	goodMeanGlobal = np.sum(1.0*goodMeans/goodMeans.shape[0])
	yellowedMeanGlobal = np.sum(1.0*yellowedMeans/yellowedMeans.shape[0])
def shiftImages(args):
	
	outfile = open("ImageMeanValues1.txt", 'a')
	yellowedMeans = []
	goodMeans = []
	redMeans = []
	redGoodMeans = []
	
	means = []
	
	for i in range(3118,4116):
		
		image = []
		
		try:
			if i < 10:
				
				image = dia.openImage("DJI0000" + str(i) + ".JPG")
				print "DJI0000" + str(i) + ".JPG"
				
			elif i >= 10 and i < 100:
				
				image = dia.openImage("DJI000" + str(i) + ".JPG")
				print "DJI000" + str(i) + ".JPG"	
				
			elif i >= 100 and i < 1000 :
				
				image = dia.openImage("DJI00" + str(i) + ".JPG")
				print "DJI00" + str(i) + ".JPG"
			else:
								
				image = dia.openImage("DJI0" + str(i) + ".JPG")
				print "DJI0" + str(i) + ".JPG"
			
			meanVector = dia.meanVector(image)
			means.append(meanVector)
			
			if meanVector[2] > 80:
				
				goodMeans.append(meanVector[2])
				
			else:
				
				yellowedMeans.append(meanVector[2])
				
			if meanVector[0] < 145:
				
				redGoodMeans.append(meanVector[0])
				
			else:
				
				redMeans.append(meanVector[0])
				
		except AttributeError:
			
			print "DJI00" + str(i) + ".JPG not found"
	
	means = np.array(means)
	
	plt.plot(range(0,means.shape[0]), means[:,0], c='red')
	plt.plot(range(0,means.shape[0]), means[:,1], c='green')
	plt.plot(range(0,means.shape[0]), means[:,2], c='blue')
	plt.title('Image Mean Values')
	plt.xlabel('Image Number')
	plt.ylabel('Mean DN')
	plt.show()
	
	goodMeans = np.array(goodMeans, dtype='uint64')
	yellowedMeans = np.array(yellowedMeans, dtype='uint64')
		
	goodMeanGlobal = np.sum(1.0*goodMeans/goodMeans.shape[0])
	yellowedMeanGlobal = np.sum(1.0*yellowedMeans/yellowedMeans.shape[0])
	meanDifference = goodMeanGlobal - yellowedMeanGlobal
	
	outfile.write("\n\n\n\n These are the global statistics of the blue band using a value of 80 to separate the \"good images\" from the \"yellowed images\". The value was obtained by inspection of the output mean values.\n\n")
	outfile.write('\nGood Mean = ' + str(goodMeanGlobal))
	outfile.write('\nYellow Mean = ' + str(yellowedMeanGlobal))
			
	goodVar = np.sum(np.power(goodMeans - goodMeanGlobal,2))/(goodMeans.shape[0] - 1)
	yellowedVar = np.sum(np.power(yellowedMeans - yellowedMeanGlobal,2))/(yellowedMeans.shape[0] - 1)
			
	outfile.write('\nGood Standard Deviation = ' + str(np.sqrt(goodVar)))
	outfile.write('\nYellowed Standard Deviaiton = ' + str(np.sqrt(yellowedVar)))
	
	outfile.write('\nYellowed Range = ' + str(np.amax(yellowedMeans) - np.amin(yellowedMeans)))
	outfile.write('\nGood Range = ' + str(np.amax(goodMeans) - np.amin(goodMeans)))

	redGoodMeans = np.array(redGoodMeans, dtype='uint64')
	redMeans = np.array(redMeans, dtype='uint64')
		
	redGoodMeanGlobal = np.sum(1.0*redGoodMeans/redGoodMeans.shape[0])
	redMeanGlobal = np.sum(1.0*redMeans/redMeans.shape[0])
	redMeanDifference = redGoodMeanGlobal - redMeanGlobal
	
	outfile.write("\n\n\n\n These are the global statistics of the Red band using a value of 145 to separate the \"good images\" from the \"yellowed images\". The value was obtained by inspection of the output mean values.\n\n")
	outfile.write('\nGood Mean = ' + str(redGoodMeanGlobal))
	outfile.write('\nYellow Mean = ' + str(redMeanGlobal))
			
	goodVar = np.sum(np.power(redGoodMeans - redGoodMeanGlobal,2))/(redGoodMeans.shape[0] - 1)
	yellowedVar = np.sum(np.power(redMeans - redMeanGlobal,2))/(redMeans.shape[0] - 1)
			
	outfile.write('\nGood Standard Deviation = ' + str(np.sqrt(goodVar)))
	outfile.write('\nYellowed Standard Deviaiton = ' + str(np.sqrt(yellowedVar)))
	
	outfile.write('\nYellowed Range = ' + str(np.amax(redMeans) - np.amin(redMeans)))
	outfile.write('\nGood Range = ' + str(np.amax(redGoodMeans) - np.amin(redGoodMeans)))
	
	refImage = dia.openImage("DJI03790.JPG")
	
	for i in range(3118,4116):
		
		image = []
		
		try:
			if i < 10:
				
				image = dia.openImage("DJI0000" + str(i) + ".JPG")
				print "DJI0000" + str(i) + ".JPG"
				
			elif i >= 10 and i < 100:
				
				image = dia.openImage("DJI000" + str(i) + ".JPG")
				print "DJI000" + str(i) + ".JPG"	
				
			elif i >= 100 and i < 1000 :
				
				image = dia.openImage("DJI00" + str(i) + ".JPG")
				print "DJI00" + str(i) + ".JPG"
			else:
								
				image = dia.openImage("DJI0" + str(i) + ".JPG")
				print "DJI0" + str(i) + ".JPG"
			
			print "Processing Image" + str(i)
			
			meanVector = dia.meanVector(image)
			
			if meanVector[2] < 80:
				
				print "Correcting Image" + str(i)
				
				newImage = np.array([image[0] + redMeanDifference,image[1],image[2] + meanDifference],dtype='uint8')
				
				x = newImage[0] > image[0]
				y = newImage[2] < image[2]
				
				b = newImage[0]
				c = newImage[2]
				
				b[x] = 0
				c[y] = 255
				
				correctedImage = np.array([b,image[1],c], dtype='uint8')
				
				if i < 10:
					
					dia.outputImage(correctedImage, "corrected/CorDJI0000" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI0000" + str(i) + ".JPG", "DJI0000" + str(i) + ".JPG")
					
				elif i >= 10 and i < 100:
					
					dia.outputImage(correctedImage, "corrected/CorDJI000" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI000" + str(i) + ".JPG", "DJI000" + str(i) + ".JPG")	
					
				elif i >= 100 and i < 1000 :
					
					dia.outputImage(correctedImage, "corrected/CorDJI00" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI00" + str(i) + ".JPG", "DJI00" + str(i) + ".JPG")
					
				else:
					
					dia.outputImage(correctedImage, "corrected/CorDJI0" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI0" + str(i) + ".JPG", "DJI0" + str(i) + ".JPG")
				
			else:
								
				if i < 10:
					
					dia.outputImage(image, "corrected/CorDJI0000" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI0000" + str(i) + ".JPG", "DJI0000" + str(i) + ".JPG")
					
				elif i >= 10 and i < 100:
					
					dia.outputImage(image, "corrected/CorDJI000" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI000" + str(i) + ".JPG", "DJI000" + str(i) + ".JPG")	
					
				elif i >= 100 and i < 1000 :
					
					dia.outputImage(image, "corrected/CorDJI00" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI00" + str(i) + ".JPG", "DJI00" + str(i) + ".JPG")
					
				else:
					
					dia.outputImage(image, "corrected/CorDJI0" + str(i) + ".JPG")
				
					dia.georefImage("corrected/CorDJI0" + str(i) + ".JPG", "DJI0" + str(i) + ".JPG")
					
		except AttributeError as e:
			
			print e
			print "DJI00" + str(i) + ".JPG not found"