Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifications that make it work in Python 3.8. #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions page_dewarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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':
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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__':
Expand Down
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
page_dewarp
numpy
scipy
Image
cv2>=3.0
Pillow
opencv-python