mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-24 17:06:16 +03:00
Converted addCleanup
This commit is contained in:
parent
7b628a5ef6
commit
748739c992
|
@ -61,9 +61,8 @@ class TestDecompressionBomb(PillowTestCase):
|
|||
|
||||
class TestDecompressionCrop(PillowTestCase):
|
||||
def setUp(self):
|
||||
self.src = hopper()
|
||||
self.addCleanup(self.src.close)
|
||||
Image.MAX_IMAGE_PIXELS = self.src.height * self.src.width * 4 - 1
|
||||
width, height = 128, 128
|
||||
Image.MAX_IMAGE_PIXELS = height * width * 4 - 1
|
||||
|
||||
def tearDown(self):
|
||||
Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT
|
||||
|
@ -71,8 +70,9 @@ class TestDecompressionCrop(PillowTestCase):
|
|||
def testEnlargeCrop(self):
|
||||
# Crops can extend the extents, therefore we should have the
|
||||
# same decompression bomb warnings on them.
|
||||
box = (0, 0, self.src.width * 2, self.src.height * 2)
|
||||
pytest.warns(Image.DecompressionBombWarning, self.src.crop, box)
|
||||
with hopper() as src:
|
||||
box = (0, 0, src.width * 2, src.height * 2)
|
||||
pytest.warns(Image.DecompressionBombWarning, src.crop, box)
|
||||
|
||||
def test_crop_decompression_checks(self):
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import os
|
||||
import pytest
|
||||
from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile
|
||||
|
||||
|
@ -13,17 +14,23 @@ fontname = "Tests/fonts/10x20-ISO8859-1.pcf"
|
|||
message = "hello, world"
|
||||
|
||||
|
||||
@skip_unless_feature("zlib")
|
||||
class TestFontPcf(PillowTestCase):
|
||||
def save_font(self):
|
||||
pytestmark = skip_unless_feature("zlib")
|
||||
|
||||
def save_font(request, tmp_path):
|
||||
with open(fontname, "rb") as test_file:
|
||||
font = PcfFontFile.PcfFontFile(test_file)
|
||||
assert isinstance(font, FontFile.FontFile)
|
||||
# check the number of characters in the font
|
||||
assert len([_f for _f in font.glyph if _f]) == 223
|
||||
|
||||
tempname = self.tempfile("temp.pil")
|
||||
self.addCleanup(self.delete_tempfile, tempname[:-4] + ".pbm")
|
||||
tempname = str(tmp_path / "temp.pil")
|
||||
|
||||
def delete_tempfile():
|
||||
try:
|
||||
os.remove(tempname[:-4] + ".pbm")
|
||||
except OSError:
|
||||
pass # report?
|
||||
request.addfinalizer(delete_tempfile)
|
||||
font.save(tempname)
|
||||
|
||||
with Image.open(tempname.replace(".pil", ".pbm")) as loaded:
|
||||
|
@ -35,16 +42,16 @@ class TestFontPcf(PillowTestCase):
|
|||
assert f_loaded.read() == f_target.read()
|
||||
return tempname
|
||||
|
||||
def test_sanity(self):
|
||||
self.save_font()
|
||||
def test_sanity(request, tmp_path):
|
||||
save_font(request, tmp_path)
|
||||
|
||||
def test_invalid_file(self):
|
||||
def test_invalid_file():
|
||||
with open("Tests/images/flower.jpg", "rb") as fp:
|
||||
with pytest.raises(SyntaxError):
|
||||
PcfFontFile.PcfFontFile(fp)
|
||||
|
||||
def test_draw(self):
|
||||
tempname = self.save_font()
|
||||
def test_draw(request, tmp_path):
|
||||
tempname = save_font(request, tmp_path)
|
||||
font = ImageFont.load(tempname)
|
||||
im = Image.new("L", (130, 30), "white")
|
||||
draw = ImageDraw.Draw(im)
|
||||
|
@ -52,8 +59,8 @@ class TestFontPcf(PillowTestCase):
|
|||
with Image.open("Tests/images/test_draw_pbm_target.png") as target:
|
||||
assert_image_similar(im, target, 0)
|
||||
|
||||
def test_textsize(self):
|
||||
tempname = self.save_font()
|
||||
def test_textsize(request, tmp_path):
|
||||
tempname = save_font(request, tmp_path)
|
||||
font = ImageFont.load(tempname)
|
||||
for i in range(255):
|
||||
(dx, dy) = font.getsize(chr(i))
|
||||
|
@ -63,8 +70,8 @@ class TestFontPcf(PillowTestCase):
|
|||
msg = message[: l + 1]
|
||||
assert font.getsize(msg) == (len(msg) * 10, 20)
|
||||
|
||||
def _test_high_characters(self, message):
|
||||
tempname = self.save_font()
|
||||
def _test_high_characters(request, tmp_path, message):
|
||||
tempname = save_font(request, tmp_path)
|
||||
font = ImageFont.load(tempname)
|
||||
im = Image.new("L", (750, 30), "white")
|
||||
draw = ImageDraw.Draw(im)
|
||||
|
@ -72,8 +79,8 @@ class TestFontPcf(PillowTestCase):
|
|||
with Image.open("Tests/images/high_ascii_chars.png") as target:
|
||||
assert_image_similar(im, target, 0)
|
||||
|
||||
def test_high_characters(self):
|
||||
def test_high_characters(request, tmp_path):
|
||||
message = "".join(chr(i + 1) for i in range(140, 232))
|
||||
self._test_high_characters(message)
|
||||
_test_high_characters(request, tmp_path, message)
|
||||
# accept bytes instances.
|
||||
self._test_high_characters(message.encode("latin1"))
|
||||
_test_high_characters(request, tmp_path, message.encode("latin1"))
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
import pytest
|
||||
from PIL import Image, ImageQt
|
||||
|
||||
from .helper import PillowTestCase, assert_image_equal, hopper
|
||||
from .test_imageqt import PillowQtTestCase
|
||||
from .test_imageqt import skip_if_qt_is_not_installed
|
||||
|
||||
|
||||
class TestFromQImage(PillowQtTestCase, PillowTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.files_to_test = [
|
||||
pytestmark = skip_if_qt_is_not_installed()
|
||||
|
||||
@pytest.fixture
|
||||
def test_images():
|
||||
ims = [
|
||||
hopper(),
|
||||
Image.open("Tests/images/transparent.png"),
|
||||
Image.open("Tests/images/7x13.png"),
|
||||
]
|
||||
for im in self.files_to_test:
|
||||
self.addCleanup(im.close)
|
||||
try:
|
||||
yield ims
|
||||
finally:
|
||||
for im in ims.values():
|
||||
im.close()
|
||||
|
||||
def roundtrip(self, expected):
|
||||
def roundtrip(expected):
|
||||
# PIL -> Qt
|
||||
intermediate = expected.toqimage()
|
||||
# Qt -> PIL
|
||||
|
@ -26,22 +31,22 @@ class TestFromQImage(PillowQtTestCase, PillowTestCase):
|
|||
else:
|
||||
assert_image_equal(result, expected.convert("RGB"))
|
||||
|
||||
def test_sanity_1(self):
|
||||
for im in self.files_to_test:
|
||||
self.roundtrip(im.convert("1"))
|
||||
def test_sanity_1(test_images):
|
||||
for im in test_images:
|
||||
roundtrip(im.convert("1"))
|
||||
|
||||
def test_sanity_rgb(self):
|
||||
for im in self.files_to_test:
|
||||
self.roundtrip(im.convert("RGB"))
|
||||
def test_sanity_rgb(test_images):
|
||||
for im in test_images:
|
||||
roundtrip(im.convert("RGB"))
|
||||
|
||||
def test_sanity_rgba(self):
|
||||
for im in self.files_to_test:
|
||||
self.roundtrip(im.convert("RGBA"))
|
||||
def test_sanity_rgba(test_images):
|
||||
for im in test_images:
|
||||
roundtrip(im.convert("RGBA"))
|
||||
|
||||
def test_sanity_l(self):
|
||||
for im in self.files_to_test:
|
||||
self.roundtrip(im.convert("L"))
|
||||
def test_sanity_l(test_images):
|
||||
for im in test_images:
|
||||
roundtrip(im.convert("L"))
|
||||
|
||||
def test_sanity_p(self):
|
||||
for im in self.files_to_test:
|
||||
self.roundtrip(im.convert("P"))
|
||||
def test_sanity_p(test_images):
|
||||
for im in test_images:
|
||||
roundtrip(im.convert("P"))
|
||||
|
|
|
@ -4,68 +4,82 @@ from PIL import Image, ImageFilter
|
|||
from .helper import PillowTestCase
|
||||
|
||||
|
||||
class TestImageOpsUsm(PillowTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.im = Image.open("Tests/images/hopper.ppm")
|
||||
self.addCleanup(self.im.close)
|
||||
self.snakes = Image.open("Tests/images/color_snakes.png")
|
||||
self.addCleanup(self.snakes.close)
|
||||
@pytest.fixture
|
||||
def test_images():
|
||||
ims = {
|
||||
"im": Image.open("Tests/images/hopper.ppm"),
|
||||
"snakes": Image.open("Tests/images/color_snakes.png"),
|
||||
}
|
||||
try:
|
||||
yield ims
|
||||
finally:
|
||||
for im in ims.values():
|
||||
im.close()
|
||||
|
||||
def test_filter_api(self):
|
||||
|
||||
def test_filter_api(test_images):
|
||||
im = test_images["im"]
|
||||
|
||||
test_filter = ImageFilter.GaussianBlur(2.0)
|
||||
i = self.im.filter(test_filter)
|
||||
i = im.filter(test_filter)
|
||||
assert i.mode == "RGB"
|
||||
assert i.size == (128, 128)
|
||||
|
||||
test_filter = ImageFilter.UnsharpMask(2.0, 125, 8)
|
||||
i = self.im.filter(test_filter)
|
||||
i = im.filter(test_filter)
|
||||
assert i.mode == "RGB"
|
||||
assert i.size == (128, 128)
|
||||
|
||||
def test_usm_formats(self):
|
||||
|
||||
def test_usm_formats(test_images):
|
||||
im = test_images["im"]
|
||||
|
||||
usm = ImageFilter.UnsharpMask
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("1").filter(usm)
|
||||
self.im.convert("L").filter(usm)
|
||||
im.convert("1").filter(usm)
|
||||
im.convert("L").filter(usm)
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("I").filter(usm)
|
||||
im.convert("I").filter(usm)
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("F").filter(usm)
|
||||
self.im.convert("RGB").filter(usm)
|
||||
self.im.convert("RGBA").filter(usm)
|
||||
self.im.convert("CMYK").filter(usm)
|
||||
im.convert("F").filter(usm)
|
||||
im.convert("RGB").filter(usm)
|
||||
im.convert("RGBA").filter(usm)
|
||||
im.convert("CMYK").filter(usm)
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("YCbCr").filter(usm)
|
||||
im.convert("YCbCr").filter(usm)
|
||||
|
||||
def test_blur_formats(self):
|
||||
|
||||
def test_blur_formats(test_images):
|
||||
im = test_images["im"]
|
||||
|
||||
blur = ImageFilter.GaussianBlur
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("1").filter(blur)
|
||||
blur(self.im.convert("L"))
|
||||
im.convert("1").filter(blur)
|
||||
blur(im.convert("L"))
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("I").filter(blur)
|
||||
im.convert("I").filter(blur)
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("F").filter(blur)
|
||||
self.im.convert("RGB").filter(blur)
|
||||
self.im.convert("RGBA").filter(blur)
|
||||
self.im.convert("CMYK").filter(blur)
|
||||
im.convert("F").filter(blur)
|
||||
im.convert("RGB").filter(blur)
|
||||
im.convert("RGBA").filter(blur)
|
||||
im.convert("CMYK").filter(blur)
|
||||
with pytest.raises(ValueError):
|
||||
self.im.convert("YCbCr").filter(blur)
|
||||
im.convert("YCbCr").filter(blur)
|
||||
|
||||
def test_usm_accuracy(self):
|
||||
|
||||
src = self.snakes.convert("RGB")
|
||||
def test_usm_accuracy(test_images):
|
||||
snakes = test_images["snakes"]
|
||||
|
||||
src = snakes.convert("RGB")
|
||||
i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0))
|
||||
# Image should not be changed because it have only 0 and 255 levels.
|
||||
assert i.tobytes() == src.tobytes()
|
||||
|
||||
def test_blur_accuracy(self):
|
||||
|
||||
i = self.snakes.filter(ImageFilter.GaussianBlur(0.4))
|
||||
def test_blur_accuracy(test_images):
|
||||
snakes = test_images["snakes"]
|
||||
|
||||
i = snakes.filter(ImageFilter.GaussianBlur(0.4))
|
||||
# These pixels surrounded with pixels with 255 intensity.
|
||||
# They must be very close to 255.
|
||||
for x, y, c in [
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import pytest
|
||||
from PIL import ImageQt
|
||||
|
||||
from .helper import PillowTestCase, hopper
|
||||
|
@ -5,14 +6,14 @@ from .helper import PillowTestCase, hopper
|
|||
if ImageQt.qt_is_installed:
|
||||
from PIL.ImageQt import qRgba
|
||||
|
||||
def skip_if_qt_is_not_installed(_):
|
||||
def skip_if_qt_is_not_installed():
|
||||
pass
|
||||
|
||||
|
||||
else:
|
||||
|
||||
def skip_if_qt_is_not_installed(test_case):
|
||||
test_case.skipTest("Qt bindings are not installed")
|
||||
def skip_if_qt_is_not_installed():
|
||||
return pytest.mark.skip(reason="Qt bindings are not installed")
|
||||
|
||||
|
||||
class PillowQtTestCase:
|
||||
|
|
Loading…
Reference in New Issue
Block a user