-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This branch adds a few files to deal with parsing scanned pages. Not everything's working yet. Work to be done still: - Scanning of pages using SANE or something similar - Parsing of scanned pages - Generating test pages to verify the code
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python2 | ||
|
||
import os | ||
import sys | ||
from argparse import ArgumentParser | ||
from scantron import ScantronParser | ||
|
||
# Make this file easier to use by adding nice arguments | ||
parser = ArgumentParser(description='Parse scanned scantron sheets.') | ||
parser.add_argument( | ||
'data', | ||
metavar='input_data', | ||
help='File where the field data is stored. ' + | ||
'This must be a python script with an array called "data" ' + | ||
'of Field entries.') | ||
|
||
args = parser.parse_args() | ||
|
||
# If the file has a .py extension, we should still accept it | ||
if args.data.endswith('.py'): | ||
args.data = os.path.splitext(args.data)[0] | ||
|
||
# Import the file specified on the command line | ||
try: | ||
__import__(args.data) | ||
data = sys.modules[args.data].data | ||
except ImportError: | ||
print('Failed to import %s.' % args.data) | ||
quit(1) | ||
except AttributeError: | ||
print('Could not find data array.') | ||
quit(1) | ||
|
||
# If everything went well, proceed to parse the filled scantron | ||
st = ScantronParser() | ||
st.scan(data, 'pages/page-1.jpg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python2 | ||
|
||
from scantron import * | ||
import PythonMagick | ||
from pyPdf import PdfFileReader | ||
from PIL import Image | ||
|
||
data = [ | ||
Field('foo', 'Foo foo foo', int), | ||
Field('bar', 'Bar bar bar', int), | ||
Field('baz', 'Baz baz baz', int), | ||
Field('laber', 'Laber laber', bool), | ||
] | ||
|
||
# Generate PDF | ||
st = Scantron('test.pdf') | ||
st.set_box_sizes(box_size=0.2*inch, box_spacing=0.3*inch) | ||
st.populate(data, matches=1, collate='no') | ||
st.save() | ||
|
||
# Convert PDF to a series of pictures | ||
pages = [] | ||
|
||
pdf = PdfFileReader(file('test.pdf', 'rb')) | ||
|
||
for page in range(pdf.getNumPages()): | ||
#page += 1 | ||
name = 'test_image_%d.png' % page | ||
|
||
im = PythonMagick.Image() | ||
im.density('200') | ||
im.read('test.pdf[%d]' % page) | ||
im.write(name) | ||
|
||
pages.append(name) | ||
|
||
# Create a series of transformations to apply | ||
transformations = [ | ||
lambda x: x.rotate(10, expand=False), | ||
lambda x: x.rotate(-10, expand=False), | ||
] | ||
|
||
tf = 0 | ||
|
||
# Take all pictures and modify them in different ways | ||
for page in pages: | ||
im = Image.open(page).convert('L') | ||
im = transformations[tf](im) | ||
im.save(page) | ||
|
||
tf = (tf + 1) % len(transformations) |