Add some processor tests

This commit is contained in:
Matthew Tretter 2012-11-05 21:56:05 -05:00
parent 56f1ccb8a8
commit 6255b93b78
2 changed files with 40 additions and 7 deletions

31
tests/test_processors.py Normal file
View file

@ -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))

View file

@ -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)