# -*- coding: utf-8 -*-
"""
Created on Sat Jul 23 22:21:47 2016

@author: nebula
"""
import sys
import os
#import Image, ImageDraw
from PIL import Image, ImageDraw
import numpy as np
import matplotlib.pyplot as plt

def makeHist(inpath, outfile):
    img = Image.open(inpath)
    print img.size
    rawimage = img.getdata()
    
    hist = {}
    hist['red'] = []
    hist['green'] = []
    hist['blue'] = []
    hist['sum'] = []
    hist['processed'] = []
    for pix in rawimage:
        hist['red'].append(pix[0])
        hist['green'].append(pix[1])
        hist['blue'].append(pix[2])
        hist['sum'].append((pix[0]+pix[1]+pix[2])/3)
        
        processed_pix = (pix[2]*2 - (pix[0] + pix[1]))*2
        #if processed_pix < 0:
        #    processed_pix = 0
        hist['processed'].append(processed_pix)
    #hist = img.histogram()
    #print hist
    
    plt.hist(hist['red'], histtype='step', bins=128, alpha=0.5, color='red', range=(1,255))
    plt.hist(hist['green'], histtype='step', bins=128, alpha=0.5, color='green', range=(1,255))
    plt.hist(hist['blue'], histtype='step', bins=128, alpha=0.5, color='blue', range=(1,255))
    plt.hist(hist['sum'], histtype='step', bins=128, alpha=0.5, color='gray', range=(1,255))
    plt.savefig(outfile)
    plt.show()
    
    out_img = Image.new('L', img.size)
    out_img.putdata(hist['processed'])
    #out_img.save(outfile)
    
    #plt.hist(hist['processed'], histtype='step', bins=128, alpha=0.5, color='gray', range=(1,255))
    #plt.show()
    
    

if __name__ == '__main__':
    workdir = os.path.join('..', 'private', 'work3')
    outdir  = os.path.join('..', 'private', 'processed')
    
    files = os.listdir(workdir)
    for filename in files:
        infile = os.path.join(workdir, filename)
        root, ext = os.path.splitext(filename)
        outfile = os.path.join(outdir, root+'.png')
        
        print infile, outfile
        makeHist(infile, outfile)
        
        
    #samplepath = os.path.join('..', 'private', 'work3', 'CD00002-IS-BR-21.tif')
    #makeHist(samplepath, 'result.png')
    
    
@@ -108,16 +108,17 @@
 rt os%0D%0A#
+ 
 import I
@@ -223,16 +223,18 @@
  plt%0D%0A%0D%0A
+%0D%0A
 def make
@@ -333,28 +333,24 @@
 .getdata()%0D%0A
-    
 %0D%0A    hist =
@@ -648,36 +648,34 @@
 x%5B0%5D
-+
+ + 
 pix%5B1%5D
-+
+ + 
 pix%5B2%5D)
-/
+ / 
 3)%0D%0A
-        
 %0D%0A  
@@ -703,17 +703,19 @@
  (pix%5B2%5D
-*
+ * 
 2 - (pix
@@ -728,17 +728,19 @@
 pix%5B1%5D))
-*
+ * 
 2%0D%0A     
@@ -743,16 +743,17 @@
        #
+ 
 if proce
@@ -853,16 +853,17 @@
 )%0D%0A    #
+ 
 hist = i
@@ -883,16 +883,17 @@
 )%0D%0A    #
+ 
 print hi
@@ -896,20 +896,16 @@
 t hist%0D%0A
-    
 %0D%0A    pl
@@ -976,32 +976,33 @@
 'red', range=(1,
+ 
 255))%0D%0A    plt.h
@@ -1074,32 +1074,33 @@
 reen', range=(1,
+ 
 255))%0D%0A    plt.h
@@ -1170,32 +1170,33 @@
 blue', range=(1,
+ 
 255))%0D%0A    plt.h
@@ -1265,32 +1265,33 @@
 gray', range=(1,
+ 
 255))%0D%0A    plt.s
@@ -1319,28 +1319,24 @@
 plt.show()%0D%0A
-    
 %0D%0A    out_im
@@ -1410,16 +1410,17 @@
 )%0D%0A    #
+ 
 out_img.
@@ -1434,27 +1434,24 @@
 tfile)%0D%0A
-    
 %0D%0A    #
+ 
 plt.hist
@@ -1543,16 +1543,17 @@
 )%0D%0A    #
+ 
 plt.show
@@ -1556,26 +1556,16 @@
 show()%0D%0A
-    %0D%0A    
 %0D%0A%0D%0Aif _
@@ -1653,17 +1653,16 @@
  outdir 
- 
 = os.pat
@@ -1695,28 +1695,24 @@
 rocessed')%0D%0A
-    
 %0D%0A    files 
@@ -1903,17 +1903,19 @@
 ir, root
-+
+ + 
 '.png')%0D
@@ -1915,24 +1915,16 @@
 .png')%0D%0A
-        
 %0D%0A      
@@ -1983,24 +1983,18 @@
 tfile)%0D%0A
-        
+%0D%0A
 %0D%0A      
@@ -1995,23 +1995,18 @@
         
-%0D%0A    
 #
+ 
 samplepa
@@ -2078,17 +2078,22 @@
 ')%0D%0A    
-#
+    # 
 makeHist
@@ -2124,14 +2124,4 @@
 ')%0D%0A
-    %0D%0A    

reformat makehistgram.py