def add(cmdline): import sys from sdaps.add import add_image, check_image from sdaps import image error = False survey = model.survey.Survey.load(cmdline['project']) filelist = [] deletelist = [] if not cmdline['convert']: for file in cmdline['images']: filelist.append(file) if not check_image(survey, file, cmdline['duplex'], cmdline['force'], message=True): error = True if error: return else: if not cmdline['copy']: log.error( _("The --no-copy option is not compatible with --convert!")) return 1 try: from sdaps.convert import convert_images except: log.error( "Need to convert the images to monochrome TIFF, however the conversion module cannot be imported. You are likely missing the OpenCV dependency." ) return 1 print _("Converting input files into a single temporary file.") tmp = tempfile.mktemp(suffix='.tif', prefix='sdaps-convert-') deletelist.append(tmp) filelist.append(tmp) # Run conversion # TODO: Allow 3D transformation here! try: convert_images(cmdline['images'], tmp, survey.defs.paper_width, survey.defs.paper_height, cmdline['transform']) if not check_image(survey, tmp, cmdline['duplex'], cmdline['force']): log.error( _("The page count of the created temporary file does not work with this survey." )) raise AssertionError() except Exception, e: log.error(str(e)) log.error(_("Running the conversion failed.")) error = True
def convert(cmdline): from sdaps.convert import convert_images if cmdline['output'] is None: log.error(_("No output filename specified!")) sys.exit(1) # We need a survey only for the paper size! survey = model.survey.Survey.load(cmdline['project']) convert_images(cmdline['images'], cmdline['output'], survey.defs.paper_width, survey.defs.paper_height, cmdline['transform'])
def watch(cmdline): # We need a survey that has the correct definitions (paper size, duplex mode) # Assume the first argument is a survey if os.path.exists('./WATCH/info'): print('WATCH project found') pass else: print('Creating WATCH project') subprocess.call(['sdaps', 'setup', 'WATCH', './watch.tex']) watchtexpath = (os.path.dirname(os.path.abspath(__file__))) #loading dummy survey print('Loading WATCH project') survey = model.survey.Survey.load('WATCH') # A sheet object to attach the images to sheet = model.sheet.Sheet() survey.add_sheet(sheet) print('Listing all projects in ProjectsFolder') #creating project dictionnary surveyIdList = {} #list of all subfolders containing 'info' for file in Path(cmdline['projectsFolder']).walkfiles('info'): s = file.dirname() with open(s + '/info', "r") as infoFile: #looking for survey id and add it to the dictionnary lines = infoFile.read() line = lines.split('\n') for l in line: words = l.split(' = ') if words[0] == 'survey_id': print('DETECT ! : ' + words[1]) surveyIdList[words[1]] = s with open('surveyList.csv', 'w') as f: for key in surveyIdList.keys(): f.write("%s,%s\n" % (key, surveyIdList[key])) #file retrieval print('Listing scanned files') scans = os.listdir(cmdline['scanFolder']) print(scans) #temp folder creation tempd = tempfile.mkdtemp() print('Temp folder :' + tempd) #folder with alreay processed scans renamedFolder = cmdline['renamedFolder'] def is_tiff(scanned): scan_title, scan_extension = os.path.splitext(scanned) if scan_extension == '.tif' or scan_extension == '.tiff': return True else: return False def is_pdf(scanned): scan_title, scan_extension = os.path.splitext(scanned) if scan_extension == '.pdf': return True else: return False #convert and copy for scan in scans: scan_title, scan_extension = os.path.splitext(scan) print(scan_title, scan_extension) if is_pdf(scan): print('PDF file found') print('Scan title ' + scan_title, 'Scan extension ' + scan_extension) tempscanpdf = tempfile.mktemp(suffix='.pdf', dir=tempd) tempscantif = tempfile.mktemp(suffix='.tif', dir=tempd) print('File', str(cmdline['scanFolder'] + '/' + scan), 'found, trying to convert to ' + tempscantif) subprocess.call( ['cp', cmdline['scanFolder'] + '/' + scan, tempscanpdf]) print('Copied' + str(cmdline['scanFolder'] + '/' + scan) + 'to ' + tempscanpdf) #subprocess.call(['sdaps', 'add', "WATCH", tempscanpdf, '--convert']) # for i, (img, filename, page) in enumerate(opencv.iter_images_and_pages(tempscanpdf)): # print(img) # print(filename) # print(page) scantoconvert = [] scantoconvert.append(tempscanpdf) convert.convert_images(scantoconvert, tempscantif, survey.defs.paper_width, survey.defs.paper_height) #subprocess.call(['pdfimages', '-tiff', cmdline['scanFolder']+'/'+scan, tempd+'/'+scan_title]) elif is_tiff(scan): print('TIFF file found') tempscantif = tempfile.mktemp(suffix='.tif', dir=tempd) subprocess.call( ['cp', cmdline['scanFolder'] + '/' + scan, tempscantif]) else: print('Wrong image format for file ' + scan) #we retrieve all tiff to be processed tiffscans = filter(is_tiff, os.listdir(tempd)) images = [] print('Files to be processed :' + str(tiffscans)) for file in tiffscans: num_pages = image.get_tiff_page_count(tempd + '/' + file) print(num_pages) for page in range(num_pages): images.append((tempd + "/" + file, page)) if len(images) == 0: # No images, simply exit again. sys.exit(1) def add_image(survey, tiff, page): img = model.sheet.Image() survey.sheet.add_image(img) # SDAPS assumes a relative path from the survey directory img.filename = os.path.relpath(os.path.abspath(tiff), survey.survey_dir) img.orig_name = tiff img.tiff_page = page #print('Images added :'+str(img.filename)+str(img.orig_name)+str(img.tiff_page)) imgdummy = model.sheet.Image() survey.sheet.add_image(imgdummy) imgdummy.orig_name = "DUMMY" imgdummy.filename = "DUMMY" imgdummy.tiff_page = -1 imgdummy.ignored = True #print('Images added :'+str(imgdummy.filename)+str(img.orig_name)+str(imgdummy.tiff_page)) while images: # Simply drop the list of images again. sheet.images = [] add_image(survey, *images.pop(0)) print('Adding image simplex mode') if survey.defs.duplex: print('Adding image duplex mode') add_image(survey, *images.pop(0)) #print(images) sheet.recognize.recognize() # for img in sheet.images: if img.tiff_page != -1: print(img.orig_name, img.tiff_page) print('\tPage:', img.page_number) print('\tRotated:', img.rotated) print('\tMatrix (px to mm):', img.raw_matrix) print('\tSurvey-ID:', sheet.survey_id) print('\tGlobal-ID:', sheet.global_id) print('\tBarcode-ID:', sheet.barcode_id) print('\tQuestionnaire-ID:', sheet.questionnaire_id) now = datetime.datetime.now() datestamp = now.strftime('%Y%m%d%H%M%S%f') tiffname = str(datestamp) + str( sheet.questionnaire_id) + '_' + str( sheet.survey_id) + '_' + str(sheet.barcode_id) subprocess.call(['cp', img.orig_name, tiffname + ".tif"])
def add(cmdline): import sys from sdaps.add import add_image, check_image from sdaps import image error = False survey = model.survey.Survey.load(cmdline['project']) filelist = [] deletelist = [] if not cmdline['convert']: for file in cmdline['images']: filelist.append(file) if not check_image(survey, file, cmdline['duplex'], cmdline['force'], message=True): error=True if error: return else: if not cmdline['copy']: log.error(_("The --no-copy option is not compatible with --convert!")) return 1 try: from sdaps.convert import convert_images except: log.error("Need to convert the images to monochrome TIFF, however the conversion module cannot be imported. You are likely missing the OpenCV dependency.") return 1 print(_("Converting input files into a single temporary file.")) tmp = tempfile.mktemp(suffix='.tif', prefix='sdaps-convert-') deletelist.append(tmp) filelist.append(tmp) # Run conversion # TODO: Allow 3D transformation here! try: convert_images(cmdline['images'], tmp, survey.defs.paper_width, survey.defs.paper_height, cmdline['transform']) if not check_image(survey, tmp, cmdline['duplex'], cmdline['force']): log.error(_("The page count of the created temporary file does not work with this survey.")) raise AssertionError() except Exception as e: log.error(str(e)) log.error(_("Running the conversion failed.")) error = True raise if not error: for file in filelist: print(_('Processing %s') % file) add_image(survey, file, cmdline['duplex'], cmdline['force'], cmdline['copy']) print(_('Done')) for file in deletelist: try: os.unlink(file) except OSError: pass if error: return 1 else: survey.save() return 0