def go(argv): im = grayscale(lib.imread(argv[1])) lib.debug = True lib.debug_prefix = ['binarize'] # lib.debug_imwrite('gradient2.png', gradient2(im)) lib.debug_imwrite('sauvola_noisy.png', sauvola_noisy(im, k=0.1)) # lib.debug_imwrite('adaptive_otsu.png', binarize(im, algorithm=adaptive_otsu)) # lib.debug_imwrite('ng2014.png', binarize(im, algorithm=ntirogiannis2014)) # lib.debug_imwrite('yan.png', binarize(im, algorithm=yan)) lib.debug_imwrite('sauvola.png', sauvola(im, k=0.1))
def run(args): if args.single_file: lib.debug = True im = lib.imread(args.single_file) _, out_images = process_image(im, dpi=args.dpi) for idx, outimg in enumerate(out_images): cv2.imwrite('out{}.png'.format(idx), outimg) return if args.concurrent: pool = Pool(cpu_count()) map_fn = pool.map else: map_fn = map files = [] accumulate_paths(args.indirs, files) files = sorted_numeric(list(set(files))) print('Files:', files) for p in files: d = os.path.dirname(p) if not os.path.isdir(join(args.outdir, d)): os.makedirs(join(args.outdir, d)) outfiles = map_fn( process_file, list(zip(files, [args.outdir] * len(files), [args.dpi] * len(files)))) outfiles = sum(outfiles, []) outfiles.sort(key=lambda f: list(map(int, re.findall('[0-9]+', f)))) # outtif = join(args.outdir, 'out.tif') outpdfpath = join(args.outdir, 'out.pdf') if not isfile(outpdfpath): print('making pdf:', outpdfpath) pdf = FPDF(unit='in', format='Letter') pdf.set_margins(0, 0, 0) for outfile in outfiles: print('.', end='') sys.stdout.flush() pdf.add_page() im = cv2.imread(outfile) inches_w = float(im.shape[1]) / args.dpi inches_h = float(im.shape[0]) / args.dpi x = (8.5 - inches_w) / 2 y = (11.0 - inches_h) / 2 pdf.image(outfile, w=inches_w, x=x, y=y) print() pdf.output(name=outpdfpath)
def process_file(file_args): (inpath, outdir, dpi) = file_args outfiles = glob.glob('{}/{}_*{}'.format(outdir, inpath[:-4], extension)) if outfiles: print('skipping', inpath) return outfiles else: print('processing', inpath) original = lib.imread(inpath) dpi, out_images = process_image(original, dpi=dpi) for idx, outimg in enumerate(out_images): outfile = '{}/{}_{}{}'.format(outdir, inpath[:-4], idx, extension) print(' writing', outfile) cv2.imwrite(outfile, outimg) outfiles.append(outfile) return outfiles
def run(args): if args.single_file: lib.debug = True im = lib.imread(args.single_file) _, out_images = process_image(im, dpi=args.dpi) for idx, outimg in enumerate(out_images): cv2.imwrite('out{}.png'.format(idx), outimg) return if args.concurrent: pool = Pool(cpu_count()) map_fn = pool.map else: map_fn = map files = [] accumulate_paths(args.indirs, files) files = sorted_numeric(list(set(files))) print('Files:', files) for p in files: d = os.path.dirname(p) if not os.path.isdir(join(args.outdir, d)): os.makedirs(join(args.outdir, d)) outfiles = map_fn(process_file, list(zip(files, [args.outdir] * len(files), [args.dpi] * len(files)))) outfiles = sum(outfiles, []) outfiles.sort(key=lambda f: list(map(int, re.findall('[0-9]+', f)))) outtif = join(args.outdir, 'out.tif') outpdf = join(args.outdir, 'out.pdf') if not isfile(outpdf): if not isfile(outtif): print('making tif:', outtif) check_call(['tiffcp'] + outfiles + [outtif]) print('making pdf:', outpdf) check_call([ 'tiff2pdf', '-z', '-p', 'letter', '-o', outpdf, outtif ])
def process_file(file_args): (inpath, outdir, dpi) = file_args outfiles = glob.glob('{}/{}_*{}'.format(outdir, inpath[:-4], extension)) if outfiles: print('skipping', inpath) if dpi is not None: for outfile in outfiles: check_call(['tiffset', '-s', '282', str(dpi), outfile]) check_call(['tiffset', '-s', '283', str(dpi), outfile]) return outfiles else: print('processing', inpath) original = lib.imread(inpath) dpi, out_images = process_image(original, dpi=dpi) for idx, outimg in enumerate(out_images): outfile = '{}/{}_{}{}'.format(outdir, inpath[:-4], idx, extension) print(' writing', outfile) cv2.imwrite(outfile, outimg) check_call(['tiffset', '-s', '282', str(dpi), outfile]) check_call(['tiffset', '-s', '283', str(dpi), outfile]) outfiles.append(outfile) return outfiles
def add_text(img, H=100, D=80, Min=0, Max=100): if D > Max: color = (0, 0, 255) elif D < Min: color = (0, 255, 0) else: color = (0, 255 * (Max - D) / (Max - Min), 255 - 255 * (Max - D) / (Max - Min)) text = 'Height: ' + str(H) cv2.putText(img, text, (75, 120), cv2.FONT_HERSHEY_DUPLEX, 0.3, color, 1) text = 'Distance: ' + str(D) cv2.putText(img, text, (75, 130), cv2.FONT_HERSHEY_DUPLEX, 0.3, color, 1) return img img_path = raw_input("image name:") img_path = '../img/' + img_path + '.jpg' img = imread(img_path) # detect circles img_ = rgb2blue(img) circles = detect_circles(img_, 0) circle = best_circle(circles, img_) img = plot_arrow(img, (circle[0], circle[1])) img = add_text(img) img = cv2.resize(img, (750, 750)) cv2.imshow('haha', img) cv2.waitKey(0)