Pillow/Tests/test_image_thumbnail.py

144 lines
4.1 KiB
Python
Raw Normal View History

2020-02-12 19:29:19 +03:00
import pytest
from PIL import Image
from .helper import (
assert_image_equal,
assert_image_similar,
fromstring,
hopper,
2022-04-20 04:05:12 +03:00
skip_unless_feature,
tostring,
)
2020-02-12 19:29:19 +03:00
def test_sanity():
im = hopper()
assert im.thumbnail((100, 100)) is None
2020-02-12 19:29:19 +03:00
assert im.size == (100, 100)
2020-02-12 19:29:19 +03:00
def test_aspect():
im = Image.new("L", (128, 128))
im.thumbnail((100, 100))
assert im.size == (100, 100)
2020-02-12 19:29:19 +03:00
im = Image.new("L", (128, 256))
im.thumbnail((100, 100))
assert im.size == (50, 100)
2020-02-12 19:29:19 +03:00
im = Image.new("L", (128, 256))
im.thumbnail((50, 100))
assert im.size == (50, 100)
2020-02-12 19:29:19 +03:00
im = Image.new("L", (256, 128))
im.thumbnail((100, 100))
assert im.size == (100, 50)
2014-06-10 13:10:47 +04:00
2020-02-09 03:19:53 +03:00
im = Image.new("L", (256, 128))
im.thumbnail((100, 50))
assert im.size == (100, 50)
2020-02-05 01:06:07 +03:00
im = Image.new("L", (64, 64))
im.thumbnail((100, 100))
assert im.size == (64, 64)
2020-02-12 19:29:19 +03:00
im = Image.new("L", (256, 162)) # ratio is 1.5802469136
im.thumbnail((33, 33))
assert im.size == (33, 21) # ratio is 1.5714285714
2020-02-12 19:29:19 +03:00
im = Image.new("L", (162, 256)) # ratio is 0.6328125
im.thumbnail((33, 33))
assert im.size == (21, 33) # ratio is 0.6363636364
im = Image.new("L", (145, 100)) # ratio is 1.45
im.thumbnail((50, 50))
2020-02-09 03:19:53 +03:00
assert im.size == (50, 34) # ratio is 1.47058823529
im = Image.new("L", (100, 145)) # ratio is 0.689655172414
im.thumbnail((50, 50))
assert im.size == (34, 50) # ratio is 0.68
2020-02-22 02:30:35 +03:00
im = Image.new("L", (100, 30)) # ratio is 3.333333333333
im.thumbnail((75, 75))
assert im.size == (75, 23) # ratio is 3.260869565217
2020-05-15 11:29:52 +03:00
def test_division_by_zero():
im = Image.new("L", (200, 2))
im.thumbnail((75, 75))
assert im.size == (75, 1)
2020-02-12 19:29:19 +03:00
def test_float():
im = Image.new("L", (128, 128))
im.thumbnail((99.9, 99.9))
2020-02-09 03:19:53 +03:00
assert im.size == (99, 99)
2020-02-12 19:29:19 +03:00
def test_no_resize():
# Check that draft() can resize the image to the destination size
with Image.open("Tests/images/hopper.jpg") as im:
im.draft(None, (64, 64))
assert im.size == (64, 64)
# Test thumbnail(), where only draft() is necessary to resize the image
with Image.open("Tests/images/hopper.jpg") as im:
im.thumbnail((64, 64))
assert im.size == (64, 64)
2021-01-07 16:57:49 +03:00
2022-04-20 04:05:12 +03:00
@skip_unless_feature("libtiff")
def test_load_first():
# load() may change the size of the image
# Test that thumbnail() is calling it before performing size calculations
with Image.open("Tests/images/g4_orientation_5.tif") as im:
im.thumbnail((64, 64))
assert im.size == (64, 10)
2021-04-10 00:33:21 +03:00
# valgrind test is failing with memory allocated in libjpeg
@pytest.mark.valgrind_known_error(reason="Known Failing")
2020-02-12 19:29:19 +03:00
def test_DCT_scaling_edges():
# Make an image with red borders and size (N * 8) + 1 to cross DCT grid
im = Image.new("RGB", (257, 257), "red")
im.paste(Image.new("RGB", (235, 235)), (11, 11))
2019-11-24 05:24:00 +03:00
2020-02-12 19:29:19 +03:00
thumb = fromstring(tostring(im, "JPEG", quality=99, subsampling=0))
# small reducing_gap to amplify the effect
2022-01-15 01:02:31 +03:00
thumb.thumbnail((32, 32), Image.Resampling.BICUBIC, reducing_gap=1.0)
2019-11-24 05:24:00 +03:00
2022-01-15 01:02:31 +03:00
ref = im.resize((32, 32), Image.Resampling.BICUBIC)
2020-02-12 19:29:19 +03:00
# This is still JPEG, some error is present. Without the fix it is 11.5
assert_image_similar(thumb, ref, 1.5)
2019-12-20 17:10:40 +03:00
2020-02-12 19:29:19 +03:00
def test_reducing_gap_values():
im = hopper()
2022-01-15 01:02:31 +03:00
im.thumbnail((18, 18), Image.Resampling.BICUBIC)
2020-02-12 19:29:19 +03:00
ref = hopper()
2022-01-15 01:02:31 +03:00
ref.thumbnail((18, 18), Image.Resampling.BICUBIC, reducing_gap=2.0)
2020-02-12 19:29:19 +03:00
# reducing_gap=2.0 should be the default
assert_image_equal(ref, im)
ref = hopper()
2022-01-15 01:02:31 +03:00
ref.thumbnail((18, 18), Image.Resampling.BICUBIC, reducing_gap=None)
2020-02-12 19:29:19 +03:00
with pytest.raises(AssertionError):
assert_image_equal(ref, im)
2019-12-20 17:10:40 +03:00
2020-02-12 19:29:19 +03:00
assert_image_similar(ref, im, 3.5)
2019-12-20 17:10:40 +03:00
2020-02-12 19:29:19 +03:00
def test_reducing_gap_for_DCT_scaling():
with Image.open("Tests/images/hopper.jpg") as ref:
# thumbnail should call draft with reducing_gap scale
ref.draft(None, (18 * 3, 18 * 3))
2022-01-15 01:02:31 +03:00
ref = ref.resize((18, 18), Image.Resampling.BICUBIC)
2019-12-20 17:10:40 +03:00
2020-02-12 19:29:19 +03:00
with Image.open("Tests/images/hopper.jpg") as im:
2022-01-15 01:02:31 +03:00
im.thumbnail((18, 18), Image.Resampling.BICUBIC, reducing_gap=3.0)
2019-12-20 17:10:40 +03:00
assert_image_similar(ref, im, 1.4)