import csv
import os

from gamera.core import *

from gamerafeatures import Gamerafeatures


class GetImages():
    def __init__(self, folder):
        self.files = []
        for f in os.listdir(folder):
            ffull = os.path.join(folder, f)
            if os.path.isdir(ffull):
                for file in os.listdir(ffull):
                    if file.endswith('.png'):
                        fullfile = os.path.join(ffull, file)
                        self.files.append([f, fullfile])

class WriteFile():
    def __init__(self, getter):
        with open('featurerssep.csv', 'wb') as csvfile:
            spamwriter = csv.writer(csvfile, quoting = csv.QUOTE_NONE)
            bgf = Gamerafeatures(Image([0,0], [1,1]))
            bgf.modifyfeature("volume64regions", False)
            bgf.modifyfeature("height", False)
            bgf.modifyfeature("width", False)
            #bgf.disableallfeatures()
            #bgf.modifyfeature("moments", True,7)
            #bgf.modifyfeature("moments", True, 8)
            #bgf.modifyfeature("nholes", True, 1)
            #bgf.modifyfeature("nholes_extended", True, 1)
            #bgf.modifyfeature("nholes_extended", True, 2)
            #bgf.modifyfeature("nholes_extended", True, 7)
            #bgf.modifyfeature("skeleton_features", True, 1)
            #bgf.modifyfeature("top_bottom", True, 1)
            #bgf.modifyfeature("volume16regions", True, 0)
            #bgf.modifyfeature("volume16regions", True, 4)
            #bgf.modifyfeature("volume16regions", True, 15)
            spamwriter.writerow(list(['imagenam'] + bgf.getnames()))
            for att in getter:
                image = load_image(att[1])
                gf = Gamerafeatures(image, enabler=bgf.enabler)
                imaget1 = image.white_speckles(0.02, 2, 2, 2, 11)
                gft1 = Gamerafeatures(imaget1, enabler=bgf.enabler)
                imaget2 = image.wave(2, 15, 0, 3, 0, 0.0, -1)
                gft2 = Gamerafeatures(imaget2, enabler=bgf.enabler)
                imaget3 = image.rotate(1, None, 1)
                gft3 = Gamerafeatures(imaget3, enabler=bgf.enabler)
                imaget4 = image.rotate(-1, None, 1)
                gft4 = Gamerafeatures(imaget4, enabler=bgf.enabler)
                #imaget5 = image.resize(Dim(image.nrows, image.ncols), 0)
                #gft5 = Gamerafeatures(imaget5, enabler=bgf.enabler)

                spamwriter.writerow(list([att[0]] + gf.getdata()))
                spamwriter.writerow(list([att[0]] + gft1.getdata()))
                spamwriter.writerow(list([att[0]] + gft2.getdata()))
                spamwriter.writerow(list([att[0]] + gft3.getdata()))
                spamwriter.writerow(list([att[0]] + gft4.getdata()))
                #spamwriter.writerow(list([att[0]] + gft5.getdata()))
        

f = GetImages('C:/Users/Radim/OneDrive/Documents/Projects/notes/OMRnotes/tests/train/training-set')
featuremaker = WriteFile(f.files)


