From 6255b93b78f9275f73b25c4ec0dd67192ed294ff Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Mon, 5 Nov 2012 21:56:05 -0500 Subject: [PATCH] Add some processor tests --- tests/test_processors.py | 31 +++++++++++++++++++++++++++++++ tests/utils.py | 16 +++++++++------- 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 tests/test_processors.py diff --git a/tests/test_processors.py b/tests/test_processors.py new file mode 100644 index 0000000..316b57f --- /dev/null +++ b/tests/test_processors.py @@ -0,0 +1,31 @@ +from imagekit.lib import Image +from imagekit.processors import ResizeToFill, ResizeToFit, SmartCrop +from nose.tools import eq_ +from .utils import create_image + + +def test_smartcrop(): + img = SmartCrop(100, 100).process(create_image()) + eq_(img.size, (100, 100)) + + +def test_resizetofill(): + img = ResizeToFill(100, 100).process(create_image()) + eq_(img.size, (100, 100)) + + +def test_resizetofit(): + # First create an image with aspect ratio 2:1... + img = Image.new('RGB', (200, 100)) + + # ...then resize it to fit within a 100x100 canvas. + img = ResizeToFit(100, 100).process(img) + + # Assert that the image has maintained the aspect ratio. + eq_(img.size, (100, 50)) + + +def test_resizetofit_mat(): + img = Image.new('RGB', (200, 100)) + img = ResizeToFit(100, 100, mat_color=0x000000).process(img) + eq_(img.size, (100, 100)) diff --git a/tests/utils.py b/tests/utils.py index 4acc13c..746920f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,5 +1,4 @@ import os -import tempfile from django.core.files.base import ContentFile @@ -8,7 +7,7 @@ from .models import Photo import pickle -def generate_lenna(): +def get_image_file(): """ See also: @@ -16,17 +15,20 @@ def generate_lenna(): http://sipi.usc.edu/database/database.php?volume=misc&image=12 """ - tmp = tempfile.TemporaryFile() - lennapath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', 'lenna-800x600-white-border.jpg') - with open(lennapath, "r+b") as lennafile: - Image.open(lennafile).save(tmp, 'JPEG') + path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', 'lenna-800x600-white-border.jpg') + tmp = StringIO() + tmp.write(open(path, 'r+b').read()) tmp.seek(0) return tmp +def create_image(): + return Image.open(get_image_file()) + + def create_instance(model_class, image_name): instance = model_class() - img = generate_lenna() + img = get_image_file() file = ContentFile(img.read()) instance.original_image = file instance.original_image.save(image_name, file)