#HOW TO USE:
#First, ensure that all the HPC modules are loaded. This can be done with the following two commands from the command line (once you're on falcon1)
#module load Python/3.5.2-gmvolf-5.5.7
#module load Pillow/3.4.2-GCC-4.9.3-Python-3.5.2
#Then, make sure your image is in PNG format, and name it 'original_image.png'
#Place your original image in the same directory as this script
#Run this script with the following command:
#threshold.py
#The black and white PNG image will then appear in this same directory, called 'thresholded_image.png'
#This script thresholds based on the red color channel using the threshold value 124 
#You can make minor changes to this script if you want to threshold based on r, g, or b using another threshold value

from PIL import Image
import numpy as np
import math
import random

value = 180

K = Image.open('matrix_pores2.tif').convert('L')
pix = K.load()

print(K)
print(pix)

for i in range(K.size[0]):
	for j in range(K.size[1]):
		#print(pix[i,j])
		r = pix[i,j]
		if r < value and r > 160:       #Change r to b or g if desired. You may also change >= to < or any other comparison you 
									   #want to use to perform the threshold
			pix[i,j] = 0
		else:
			pix[i,j] = 255
K.save('/projects/USU/melter/Synthesis/Latest_Versions_of_Code/Final_Code/matrix_pores2_threshold.tif')
