From f8fc621d5344f2594638ca4f320f606f0d7138af Mon Sep 17 00:00:00 2001 From: Philipp Schrader Date: Sun, 19 May 2013 11:35:45 -0700 Subject: [PATCH] Removed black background from rotated images. --- testcase.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/testcase.py b/testcase.py index bff61c1..0dc7471 100755 --- a/testcase.py +++ b/testcase.py @@ -3,7 +3,7 @@ from scantron import ScantronGenerator, ScantronField, inch import PythonMagick from pyPdf import PdfFileReader -from PIL import Image +from PIL import Image, ImageOps test_data = [ ScantronField('foo', 'Foo foo foo', int), @@ -35,9 +35,16 @@ pages.append(name) # Create a series of transformations to apply +def rotate(im, deg): + blank = Image.new('L', im.size, 255) + mask = blank.rotate(deg, expand=False) + mask = ImageOps.invert(mask) + rot = im.rotate(deg, expand=False) + return Image.composite(blank, rot, mask) + transformations = [ - lambda x: x.rotate(10, expand=False), - lambda x: x.rotate(-10, expand=False), + lambda x: rotate(x, 10), + lambda x: rotate(x, -10), ] tf = 0