Skip to content

Commit

Permalink
Adding script to generate a bunch of filled scantrons.
Browse files Browse the repository at this point in the history
  • Loading branch information
philsc committed May 19, 2013
1 parent 5f22299 commit 5361cf3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
6 changes: 2 additions & 4 deletions scantron.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def scan(self, data, path):
shape = sub_img.shape

ratio = float(shape[0]) / float(shape[1])
brightness = float(num_ones)/float(num_all)
darkness = float(num_ones)/float(num_all)

if brightness > 0.95 and abs(ratio - 1.0) < 0.1 and shape[0] > 14:
if darkness > 0.95 and abs(ratio - 1.0) < 0.1 and shape[0] > 14:
x1, x2 = slices[i][1].start, slices[i][1].stop
y1, y2 = slices[i][0].start, slices[i][0].stop

Expand All @@ -64,8 +64,6 @@ def scan(self, data, path):
rotation = math.atan2(bl[1] - tl[1], bl[0] - tl[0])
print('rotation: ' + str(rotation))

#img.show()


class Scantron:
def __init__(self, filename, spacing=0.3*inch):
Expand Down
51 changes: 51 additions & 0 deletions testcase.py
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)

0 comments on commit 5361cf3

Please sign in to comment.