'Put Tesseract to work, actual OCRing will be done here'

def __ocrIt(self, image, form_uuid, resourcename, linenum, content_type='textbox', **kwargs):

    db = current.db
    ocr_field_crops = 'ocr_field_crops'
    import uuid
    uniqueuuid = uuid.uuid1()
    resource_table = kwargs.get('resource_table')
    field_name = kwargs.get('field_name')
    inputfilename = ('%s_%s_%s_%s.tif' % (uniqueuuid, form_uuid, resourcename, linenum))
    outputfilename = ('%s_%s_%s_%s_text' % (uniqueuuid, form_uuid, resourcename, linenum))
    ocr_temp_dir = os.path.join(self.r.folder, 'uploads', 'ocr_temp')
    try:
        os.mkdir(ocr_temp_dir)
    except OSError:
        pass
    if (content_type == 'optionbox'):
        field_value = kwargs.get('field_value')
        imgfilename = ('%s.png' % inputfilename[:(-3)])
        imgpath = os.path.join(ocr_temp_dir, imgfilename)
        image.save(imgpath)
        imgfile = open(imgpath, 'r')
        db[ocr_field_crops].insert(image_set_uuid=self.set_uuid, resource_table=resource_table, field_name=field_name, image_file=db[ocr_field_crops]['image_file'].store(imgfile, imgfilename), value=field_value)
        imgfile.close()
        os.remove(imgpath)
        stat = ImageStat.Stat(image)
        if (stat.mean[0] < 96):
            return True
        else:
            return None
    elif (content_type == 'textbox'):
        field_seq = kwargs.get('field_seq')
        inputpath = os.path.join(ocr_temp_dir, inputfilename)
        image.save(inputpath)
        success = subprocess.call(['tesseract', inputpath, os.path.join(ocr_temp_dir, outputfilename)])
        if (success != 0):
            self.r.error(501, ERROR.TESSERACT_ERROR)
        outputpath = os.path.join(ocr_temp_dir, ('%s.txt' % outputfilename))
        outputfile = open(outputpath)
        outputtext = outputfile.read()
        outputfile.close()
        output = outputtext.replace('\n', '    ')
        os.remove(outputpath)
        imgfilename = ('%s.png' % inputfilename[:(-3)])
        imgpath = os.path.join(ocr_temp_dir, imgfilename)
        image.save(imgpath)
        imgfile = open(imgpath, 'r')
        db[ocr_field_crops].insert(image_set_uuid=self.set_uuid, resource_table=resource_table, field_name=field_name, image_file=db[ocr_field_crops]['image_file'].store(imgfile, imgfilename), sequence=field_seq)
        imgfile.close()
        os.remove(imgpath)
        os.remove(inputpath)
        try:
            os.rmdir(ocr_temp_dir)
        except OSError:
            import shutil
            shutil.rmtree(ocr_temp_dir)
        return output
