diff --git a/page_dewarp.py b/page_dewarp.py index 6ef5b33..88e2791 100755 --- a/page_dewarp.py +++ b/page_dewarp.py @@ -446,7 +446,7 @@ def get_contours(name, small, pagemask, masktype): mask = get_mask(name, small, pagemask, masktype) - _, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, + contours,_ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) contours_out = [] @@ -554,7 +554,7 @@ def sample_spans(shape, spans): xmin, ymin = cinfo.rect[:2] step = SPAN_PX_PER_STEP - start = ((len(means)-1) % step) / 2 + start = int(((len(means)-1) % step) / 2) contour_points += [(x+xmin, means[x]+ymin) for x in range(start, len(means), step)] @@ -736,20 +736,20 @@ def objective(pvec): ppts = project_keypoints(pvec, keypoint_index) return np.sum((dstpoints - ppts)**2) - print ' initial objective is', objective(params) + print( ' initial objective is', objective(params)) if DEBUG_LEVEL >= 1: projpts = project_keypoints(params, keypoint_index) display = draw_correspondences(small, dstpoints, projpts) debug_show(name, 4, 'keypoints before', display) - print ' optimizing', len(params), 'parameters...' + print( ' optimizing', len(params), 'parameters...') start = datetime.datetime.now() res = scipy.optimize.minimize(objective, params, method='Powell') end = datetime.datetime.now() - print ' optimization took', round((end-start).total_seconds(), 2), 'sec.' - print ' final objective is', res.fun + print( ' optimization took', round((end-start).total_seconds(), 2), 'sec.') + print( ' final objective is', res.fun) params = res.x if DEBUG_LEVEL >= 1: @@ -773,7 +773,7 @@ def objective(dims): res = scipy.optimize.minimize(objective, dims, method='Powell') dims = res.x - print ' got page dims', dims[0], 'x', dims[1] + print( ' got page dims', dims[0], 'x', dims[1]) return dims @@ -786,7 +786,7 @@ def remap_image(name, img, small, page_dims, params): width = round_nearest_multiple(height * page_dims[0] / page_dims[1], REMAP_DECIMATE) - print ' output will be {}x{}'.format(width, height) + print( ' output will be {}x{}'.format(width, height)) height_small = height / REMAP_DECIMATE width_small = width / REMAP_DECIMATE @@ -841,7 +841,7 @@ def remap_image(name, img, small, page_dims, params): def main(): if len(sys.argv) < 2: - print 'usage:', sys.argv[0], 'IMAGE1 [IMAGE2 ...]' + print( 'usage:', sys.argv[0], 'IMAGE1 [IMAGE2 ...]') sys.exit(0) if DEBUG_LEVEL > 0 and DEBUG_OUTPUT != 'file': @@ -856,8 +856,8 @@ def main(): basename = os.path.basename(imgfile) name, _ = os.path.splitext(basename) - print 'loaded', basename, 'with size', imgsize(img), - print 'and resized to', imgsize(small) + print( 'loaded', basename, 'with size', imgsize(img),) + print( 'and resized to', imgsize(small)) if DEBUG_LEVEL >= 3: debug_show(name, 0.0, 'original', small) @@ -868,20 +868,20 @@ def main(): spans = assemble_spans(name, small, pagemask, cinfo_list) if len(spans) < 3: - print ' detecting lines because only', len(spans), 'text spans' + print( ' detecting lines because only', len(spans), 'text spans') cinfo_list = get_contours(name, small, pagemask, 'line') spans2 = assemble_spans(name, small, pagemask, cinfo_list) if len(spans2) > len(spans): spans = spans2 if len(spans) < 1: - print 'skipping', name, 'because only', len(spans), 'spans' + print( 'skipping', name, 'because only', len(spans), 'spans') continue span_points = sample_spans(small.shape, spans) - print ' got', len(spans), 'spans', - print 'with', sum([len(pts) for pts in span_points]), 'points.' + print( ' got', len(spans), 'spans',) + print( 'with', sum([len(pts) for pts in span_points]), 'points.') corners, ycoords, xcoords = keypoints_from_samples(name, small, pagemask, @@ -904,11 +904,11 @@ def main(): outfiles.append(outfile) - print ' wrote', outfile - print + print( ' wrote', outfile) + print() - print 'to convert to PDF (requires ImageMagick):' - print ' convert -compress Group4 ' + ' '.join(outfiles) + ' output.pdf' + print( 'to convert to PDF (requires ImageMagick):') + print( ' convert -compress Group4 ' + ' '.join(outfiles) + ' output.pdf') if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt index c03f9c9..3ff7e8d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -page_dewarp numpy scipy -Image -cv2>=3.0 +Pillow +opencv-python \ No newline at end of file