From 5361cf34b81f492442f04895408f36d2d0ddf311 Mon Sep 17 00:00:00 2001 From: Philipp Schrader Date: Sun, 19 May 2013 09:21:40 -0700 Subject: [PATCH] Adding script to generate a bunch of filled scantrons. --- scantron.py | 6 ++---- testcase.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100755 testcase.py diff --git a/scantron.py b/scantron.py index a8b55c6..b8ebb5a 100755 --- a/scantron.py +++ b/scantron.py @@ -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 @@ -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): diff --git a/testcase.py b/testcase.py new file mode 100755 index 0000000..ea0fa9d --- /dev/null +++ b/testcase.py @@ -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)