from gamera.core import *
import random
from OMRnotes.scripts.utils.utils import findfiles, createfilepath, makedir, cleandir

def separate(folder):
    init_gamera()
    subfolder = createfilepath(folder,'separate')
    makedir(subfolder)
    cleandir(subfolder)
    for file in findfiles(folder,".png"):
        img = load_image(file)
        img = img.to_onebit()
        ccs0 = img.cc_analysis()
        for seg in ccs0:
            seg.save_PNG(createfilepath(subfolder,(str(random.getrandbits(128)) + '.png')))