def image_masker(self):
        raster_layers = []

        for i, stencilpath in enumerate(self.stencilpaths):
            image = Image.open(stencilpath)
            dim = min(image.size)
            image = image.crop((0, 0, dim, dim))

            layer = Raster.from_image(image, "RGBA")
            if self.colorize[i]:
                layer = colorize(layer, self.hues[i], 1, .5, 1, .7, .5)

            raster_layers.append(layer)

        return composite(raster_layers).get_image().resize((400, 400), Image.NEAREST)
    def load_stencil(self):
        stencil_data = json.load(open(os.path.normpath(self.stencildir + "\\" + self.stencilname + ".json")))
        masks = stencil_data['mask']
        self.layer_quantity = len(masks)
        self.relative_path = stencil_data['path']
        self.imagestencil = Image.open(self.default_patches + '\\' + self.relative_path)
        self.stencilname = stencil_data['name']
        self.master.title("Editing stencil: " + self.stencilname)
        self.colorize = stencil_data['colorize']

        self.stencilpaths.clear()
        for index, mask in enumerate(masks):
            layer = Raster.from_image(self.imagestencil)
            layer.mask = np.array(mask)
            stencilpath = self.stenciledit + '\\' + self.stencilname + '_' + str(index+1) + '.png'
            layer.save(stencilpath)
            self.stencilpaths.append(stencilpath)
    def click_stencil_select(self, event):
        if self.Frame3.curselection()[0] == 0:
            return

        stencil_name = self.Frame3.get(self.Frame3.curselection()[0])
        stencil_data = json.load(open(os.path.normpath(self.settings.stencil_metadata + "\\" + stencil_name + ".json")))
        masks = stencil_data['mask']

        for selection in self.Listbox.curselection():
            relative_path = self.Listbox.get(selection)
            image = Raster.from_image(Image.open(
                self.settings.default_patches + '//' + relative_path).resize(stencil_data['shape'], Image.NEAREST), "RGBA")


            segment_metalist = []
            for ident, mask in enumerate(masks):
                if stencil_data['colorize'][ident]:
                    image.mask = mask
                    segment_data = analyze_image(image)
                    segment_data['id'] = ident
                    segment_data['colorize'] = True
                    segment_metalist.append(segment_data)
                else:
                    segment_data = {}
                    segment_data['id'] = ident
                    segment_data['colorize'] = False
                    segment_metalist.append(segment_data)

            meta_dict = {
                'group_name': stencil_name,
                'segment_dicts': segment_metalist
            }

            output_path = os.path.split(self.settings.mappings_metadata_custom + relative_path)[0]

            # Create folder structure
            if not os.path.exists(output_path):
                os.makedirs(output_path)

            # Save json file
            with open(output_path + "\\" + os.path.basename(relative_path) + ".json", 'w') as output_file:
                json.dump(meta_dict, output_file, sort_keys=True, indent=2)
        self.click_search_item(None)