1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
import os
from unittest import mock
import pytest
from pilkit.lib import Image, ImageDraw, ImageColor
from pilkit.processors import (Resize, ResizeToFill, ResizeToFit, SmartCrop,
SmartResize, MakeOpaque, ColorOverlay, Convert,
GaussianBlur)
from pilkit.processors.resize import Thumbnail
from .utils import create_image, compare_images, get_image_file
def test_smartcrop():
img = SmartCrop(100, 100).process(create_image())
assert img.size == (100, 100)
def test_resizetofill():
img = ResizeToFill(100, 100).process(create_image())
assert 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.
assert img.size == (100, 50)
def test_resize_rounding():
"""
Regression test for matthewwithanm/pilkit#1
"""
img = Image.new('RGB', (95, 95))
img = ResizeToFill(28, 28).process(img)
assert img.size == (28, 28)
def test_resizetofit_mat():
img = Image.new('RGB', (200, 100))
img = ResizeToFit(100, 100, mat_color=0x000000).process(img)
assert img.size == (100, 100)
def test_coloroverlay():
"""
Test that the ColorOverlay processor
"""
img = Image.new('RGB', (200, 100))
color = ImageColor.getrgb('#cc0000')
img = ColorOverlay(color, overlay_opacity=1.0).process(img)
assert img.getpixel((0,0)) == (204, 0, 0)
def test_convert():
img = Image.new('RGBA', (200, 100))
img_RGBa = Convert("RGBa").process(img)
assert img_RGBa.mode == "RGBa"
img_RGBa_RGBA = Convert("RGBA").process(img)
assert img_RGBa_RGBA.mode == "RGBA"
def test_resize_antialiasing():
"""
Test that the Resize processor antialiases.
The Resize processor is used by all of the Resize* variants, so this should
cover all of resize processors. Basically, this is to test that it converts
to RGBA mode before resizing.
Related: jdriscoll/django-imagekit#192
"""
# Create a palette image and draw a circle into it.
img = Image.new('P', (500, 500), 1)
img.putpalette([
0, 0, 0,
255, 255, 255,
0, 0, 255,
])
d = ImageDraw.ImageDraw(img)
d.ellipse((100, 100, 400, 400), fill=2)
# Resize the image using the Resize processor
img = Resize(100, 100).process(img)
# Count the number of colors
color_count = len(list(filter(None, img.histogram())))
assert color_count > 2
def test_upscale():
"""
Test that the upscale argument works as expected.
"""
img = Image.new('RGB', (100, 100))
for P in [Resize, ResizeToFit, ResizeToFill, SmartResize]:
img2 = P(500, 500, upscale=True).process(img)
assert img2.size == (500, 500)
img2 = P(500, 500, upscale=False).process(img)
assert img2.size == (100, 100)
def test_should_raise_exception_if_anchor_is_passed_and_crop_is_set_to_false():
with pytest.raises(Exception, match=r"You can't specify an anchor point if crop is False."):
Thumbnail(height=200, width=200, upscale=False, crop=False, anchor='t')
def test_should_set_crop_to_true_if_anchor_is_passed_without_crop():
thumb = Thumbnail(height=200, width=200, upscale=False, anchor='t')
assert thumb.crop
def test_should_raise_exception_when_crop_is_passed_without_height_and_width():
img = Image.new('RGB', (100, 100))
with pytest.raises(Exception, match=r"You must provide both a width and height when cropping."):
Thumbnail(crop=True).process(img)
@mock.patch('pilkit.processors.resize.SmartResize')
def test_should_call_smartresize_when_crop_not_passed(my_mock):
img = Image.new('RGB', (100, 100))
Thumbnail(height=200, width=200, upscale=False).process(img)
assert my_mock.called
@mock.patch('pilkit.processors.resize.SmartResize')
def test_should_repass_upscale_option_true(my_mock):
img = Image.new('RGB', (100, 100))
Thumbnail(height=200, width=200, upscale=True).process(img)
my_mock.assert_called_once_with(width=200, upscale=True, height=200)
@mock.patch('pilkit.processors.resize.SmartResize')
def test_should_repass_upscale_option_false(my_mock):
img = Image.new('RGB', (100, 100))
Thumbnail(height=200, width=200, upscale=False).process(img)
my_mock.assert_called_once_with(width=200, upscale=False, height=200)
@mock.patch('pilkit.processors.resize.ResizeToFill')
def test_should_call_resizetofill_when_crop_and_ancho_is_passed(my_mock):
img = Image.new('RGB', (100, 100))
Thumbnail(height=200, width=200, anchor='fake').process(img)
assert my_mock.called
@mock.patch('pilkit.processors.resize.ResizeToFit')
def test_should_call_resizetofit_when_crop_is_not_passed(my_mock):
img = Image.new('RGB', (100, 100))
Thumbnail(height=200, width=200, crop=False).process(img)
assert my_mock.called
def test_GaussianBlur_radius_3():
img = GaussianBlur(radius = 3).process(create_image())
img = img.crop((112,112,144,144))
expected_img = Image.open(get_image_file("GaussianBlur_radius_3.png"))
assert compare_images(img, expected_img)
def test_GaussianBlur_radius_7():
img = GaussianBlur(radius=7).process(create_image())
img = img.crop((112, 112, 144, 144))
expected_img = Image.open(get_image_file("GaussianBlur_radius_7.png"))
assert compare_images(img, expected_img)
def test_make_gifs_opaque():
dir = os.path.dirname(__file__)
path = os.path.join(dir, 'assets', 'cat.gif')
gif = Image.open(path)
MakeOpaque().process(gif)
|