mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-05-20 10:21:52 +00:00
Add some processor tests
This commit is contained in:
parent
56f1ccb8a8
commit
6255b93b78
2 changed files with 40 additions and 7 deletions
31
tests/test_processors.py
Normal file
31
tests/test_processors.py
Normal 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))
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import os
|
import os
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
|
|
||||||
|
|
@ -8,7 +7,7 @@ from .models import Photo
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
|
||||||
def generate_lenna():
|
def get_image_file():
|
||||||
"""
|
"""
|
||||||
See also:
|
See also:
|
||||||
|
|
||||||
|
|
@ -16,17 +15,20 @@ def generate_lenna():
|
||||||
http://sipi.usc.edu/database/database.php?volume=misc&image=12
|
http://sipi.usc.edu/database/database.php?volume=misc&image=12
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tmp = tempfile.TemporaryFile()
|
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', 'lenna-800x600-white-border.jpg')
|
||||||
lennapath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', 'lenna-800x600-white-border.jpg')
|
tmp = StringIO()
|
||||||
with open(lennapath, "r+b") as lennafile:
|
tmp.write(open(path, 'r+b').read())
|
||||||
Image.open(lennafile).save(tmp, 'JPEG')
|
|
||||||
tmp.seek(0)
|
tmp.seek(0)
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
|
|
||||||
|
def create_image():
|
||||||
|
return Image.open(get_image_file())
|
||||||
|
|
||||||
|
|
||||||
def create_instance(model_class, image_name):
|
def create_instance(model_class, image_name):
|
||||||
instance = model_class()
|
instance = model_class()
|
||||||
img = generate_lenna()
|
img = get_image_file()
|
||||||
file = ContentFile(img.read())
|
file = ContentFile(img.read())
|
||||||
instance.original_image = file
|
instance.original_image = file
|
||||||
instance.original_image.save(image_name, file)
|
instance.original_image.save(image_name, file)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue