Convert various tests to pytest style

This commit is contained in:
Andrew Murray 2020-01-27 22:46:52 +11:00
parent 0bbee693e0
commit 7fd9663198
25 changed files with 1092 additions and 1064 deletions

View File

@ -1,10 +1,8 @@
import io
import unittest
import pytest
from PIL import features
from .helper import PillowTestCase
try:
from PIL import _webp
@ -13,70 +11,77 @@ except ImportError:
HAVE_WEBP = False
class TestFeatures(PillowTestCase):
def test_check(self):
def test_check():
# Check the correctness of the convenience function
for module in features.modules:
self.assertEqual(features.check_module(module), features.check(module))
assert features.check_module(module) == features.check(module)
for codec in features.codecs:
self.assertEqual(features.check_codec(codec), features.check(codec))
assert features.check_codec(codec) == features.check(codec)
for feature in features.features:
self.assertEqual(features.check_feature(feature), features.check(feature))
assert features.check_feature(feature) == features.check(feature)
@unittest.skipUnless(HAVE_WEBP, "WebP not available")
def test_webp_transparency(self):
self.assertEqual(
features.check("transp_webp"), not _webp.WebPDecoderBuggyAlpha()
)
self.assertEqual(features.check("transp_webp"), _webp.HAVE_TRANSPARENCY)
@unittest.skipUnless(HAVE_WEBP, "WebP not available")
def test_webp_mux(self):
self.assertEqual(features.check("webp_mux"), _webp.HAVE_WEBPMUX)
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
def test_webp_transparency():
assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha()
assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY
@unittest.skipUnless(HAVE_WEBP, "WebP not available")
def test_webp_anim(self):
self.assertEqual(features.check("webp_anim"), _webp.HAVE_WEBPANIM)
def test_check_modules(self):
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
def test_webp_mux():
assert features.check("webp_mux") == _webp.HAVE_WEBPMUX
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
def test_webp_anim():
assert features.check("webp_anim") == _webp.HAVE_WEBPANIM
def test_check_modules():
for feature in features.modules:
self.assertIn(features.check_module(feature), [True, False])
assert features.check_module(feature) in [True, False]
for feature in features.codecs:
self.assertIn(features.check_codec(feature), [True, False])
assert features.check_codec(feature) in [True, False]
def test_supported_modules(self):
self.assertIsInstance(features.get_supported_modules(), list)
self.assertIsInstance(features.get_supported_codecs(), list)
self.assertIsInstance(features.get_supported_features(), list)
self.assertIsInstance(features.get_supported(), list)
def test_unsupported_codec(self):
def test_supported_modules():
assert isinstance(features.get_supported_modules(), list)
assert isinstance(features.get_supported_codecs(), list)
assert isinstance(features.get_supported_features(), list)
assert isinstance(features.get_supported(), list)
def test_unsupported_codec():
# Arrange
codec = "unsupported_codec"
# Act / Assert
self.assertRaises(ValueError, features.check_codec, codec)
with pytest.raises(ValueError):
features.check_codec(codec)
def test_unsupported_module(self):
def test_unsupported_module():
# Arrange
module = "unsupported_module"
# Act / Assert
self.assertRaises(ValueError, features.check_module, module)
with pytest.raises(ValueError):
features.check_module(module)
def test_pilinfo(self):
def test_pilinfo():
buf = io.StringIO()
features.pilinfo(buf)
out = buf.getvalue()
lines = out.splitlines()
self.assertEqual(lines[0], "-" * 68)
self.assertTrue(lines[1].startswith("Pillow "))
self.assertTrue(lines[2].startswith("Python "))
assert lines[0] == "-" * 68
assert lines[1].startswith("Pillow ")
assert lines[2].startswith("Python ")
lines = lines[3:]
while lines[0].startswith(" "):
lines = lines[1:]
self.assertEqual(lines[0], "-" * 68)
self.assertTrue(lines[1].startswith("Python modules loaded from "))
self.assertTrue(lines[2].startswith("Binary modules loaded from "))
self.assertEqual(lines[3], "-" * 68)
assert lines[0] == "-" * 68
assert lines[1].startswith("Python modules loaded from ")
assert lines[2].startswith("Binary modules loaded from ")
assert lines[3] == "-" * 68
jpeg = (
"\n"
+ "-" * 68
@ -87,4 +92,4 @@ class TestFeatures(PillowTestCase):
+ "-" * 68
+ "\n"
)
self.assertIn(jpeg, out)
assert jpeg in out

View File

@ -1,28 +1,29 @@
import pytest
from PIL import CurImagePlugin, Image
from .helper import PillowTestCase
TEST_FILE = "Tests/images/deerstalker.cur"
class TestFileCur(PillowTestCase):
def test_sanity(self):
def test_sanity():
with Image.open(TEST_FILE) as im:
self.assertEqual(im.size, (32, 32))
self.assertIsInstance(im, CurImagePlugin.CurImageFile)
assert im.size == (32, 32)
assert isinstance(im, CurImagePlugin.CurImageFile)
# Check some pixel colors to ensure image is loaded properly
self.assertEqual(im.getpixel((10, 1)), (0, 0, 0, 0))
self.assertEqual(im.getpixel((11, 1)), (253, 254, 254, 1))
self.assertEqual(im.getpixel((16, 16)), (84, 87, 86, 255))
assert im.getpixel((10, 1)) == (0, 0, 0, 0)
assert im.getpixel((11, 1)) == (253, 254, 254, 1)
assert im.getpixel((16, 16)) == (84, 87, 86, 255)
def test_invalid_file(self):
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
self.assertRaises(SyntaxError, CurImagePlugin.CurImageFile, invalid_file)
with pytest.raises(SyntaxError):
CurImagePlugin.CurImageFile(invalid_file)
no_cursors_file = "Tests/images/no_cursors.cur"
cur = CurImagePlugin.CurImageFile(TEST_FILE)
cur.fp.close()
with open(no_cursors_file, "rb") as cur.fp:
self.assertRaises(TypeError, cur._open)
with pytest.raises(TypeError):
cur._open()

View File

@ -1,46 +1,47 @@
import pytest
from PIL import FitsStubImagePlugin, Image
from .helper import PillowTestCase
TEST_FILE = "Tests/images/hopper.fits"
class TestFileFitsStub(PillowTestCase):
def test_open(self):
def test_open():
# Act
with Image.open(TEST_FILE) as im:
# Assert
self.assertEqual(im.format, "FITS")
assert im.format == "FITS"
# Dummy data from the stub
self.assertEqual(im.mode, "F")
self.assertEqual(im.size, (1, 1))
assert im.mode == "F"
assert im.size == (1, 1)
def test_invalid_file(self):
def test_invalid_file():
# Arrange
invalid_file = "Tests/images/flower.jpg"
# Act / Assert
self.assertRaises(
SyntaxError, FitsStubImagePlugin.FITSStubImageFile, invalid_file
)
with pytest.raises(SyntaxError):
FitsStubImagePlugin.FITSStubImageFile(invalid_file)
def test_load(self):
def test_load():
# Arrange
with Image.open(TEST_FILE) as im:
# Act / Assert: stub cannot load without an implemented handler
self.assertRaises(IOError, im.load)
with pytest.raises(IOError):
im.load()
def test_save(self):
def test_save():
# Arrange
with Image.open(TEST_FILE) as im:
dummy_fp = None
dummy_filename = "dummy.filename"
# Act / Assert: stub cannot save without an implemented handler
self.assertRaises(IOError, im.save, dummy_filename)
self.assertRaises(
IOError, FitsStubImagePlugin._save, im, dummy_fp, dummy_filename
)
with pytest.raises(IOError):
im.save(dummy_filename)
with pytest.raises(IOError):
FitsStubImagePlugin._save(im, dummy_fp, dummy_filename)

View File

@ -1,42 +1,46 @@
import pytest
from PIL import GribStubImagePlugin, Image
from .helper import PillowTestCase, hopper
from .helper import hopper
TEST_FILE = "Tests/images/WAlaska.wind.7days.grb"
class TestFileGribStub(PillowTestCase):
def test_open(self):
def test_open():
# Act
with Image.open(TEST_FILE) as im:
# Assert
self.assertEqual(im.format, "GRIB")
assert im.format == "GRIB"
# Dummy data from the stub
self.assertEqual(im.mode, "F")
self.assertEqual(im.size, (1, 1))
assert im.mode == "F"
assert im.size == (1, 1)
def test_invalid_file(self):
def test_invalid_file():
# Arrange
invalid_file = "Tests/images/flower.jpg"
# Act / Assert
self.assertRaises(
SyntaxError, GribStubImagePlugin.GribStubImageFile, invalid_file
)
with pytest.raises(SyntaxError):
GribStubImagePlugin.GribStubImageFile(invalid_file)
def test_load(self):
def test_load():
# Arrange
with Image.open(TEST_FILE) as im:
# Act / Assert: stub cannot load without an implemented handler
self.assertRaises(IOError, im.load)
with pytest.raises(IOError):
im.load()
def test_save(self):
def test_save(tmp_path):
# Arrange
im = hopper()
tmpfile = self.tempfile("temp.grib")
tmpfile = str(tmp_path / "temp.grib")
# Act / Assert: stub cannot save without an implemented handler
self.assertRaises(IOError, im.save, tmpfile)
with pytest.raises(IOError):
im.save(tmpfile)

View File

@ -1,46 +1,47 @@
import pytest
from PIL import Hdf5StubImagePlugin, Image
from .helper import PillowTestCase
TEST_FILE = "Tests/images/hdf5.h5"
class TestFileHdf5Stub(PillowTestCase):
def test_open(self):
def test_open():
# Act
with Image.open(TEST_FILE) as im:
# Assert
self.assertEqual(im.format, "HDF5")
assert im.format == "HDF5"
# Dummy data from the stub
self.assertEqual(im.mode, "F")
self.assertEqual(im.size, (1, 1))
assert im.mode == "F"
assert im.size == (1, 1)
def test_invalid_file(self):
def test_invalid_file():
# Arrange
invalid_file = "Tests/images/flower.jpg"
# Act / Assert
self.assertRaises(
SyntaxError, Hdf5StubImagePlugin.HDF5StubImageFile, invalid_file
)
with pytest.raises(SyntaxError):
Hdf5StubImagePlugin.HDF5StubImageFile(invalid_file)
def test_load(self):
def test_load():
# Arrange
with Image.open(TEST_FILE) as im:
# Act / Assert: stub cannot load without an implemented handler
self.assertRaises(IOError, im.load)
with pytest.raises(IOError):
im.load()
def test_save(self):
def test_save():
# Arrange
with Image.open(TEST_FILE) as im:
dummy_fp = None
dummy_filename = "dummy.filename"
# Act / Assert: stub cannot save without an implemented handler
self.assertRaises(IOError, im.save, dummy_filename)
self.assertRaises(
IOError, Hdf5StubImagePlugin._save, im, dummy_fp, dummy_filename
)
with pytest.raises(IOError):
im.save(dummy_filename)
with pytest.raises(IOError):
Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename)

View File

@ -3,13 +3,12 @@ from io import StringIO
from PIL import Image, IptcImagePlugin
from .helper import PillowTestCase, hopper
from .helper import hopper
TEST_FILE = "Tests/images/iptc.jpg"
class TestFileIptc(PillowTestCase):
def test_getiptcinfo_jpg_none(self):
def test_getiptcinfo_jpg_none():
# Arrange
with hopper() as im:
@ -17,9 +16,10 @@ class TestFileIptc(PillowTestCase):
iptc = IptcImagePlugin.getiptcinfo(im)
# Assert
self.assertIsNone(iptc)
assert iptc is None
def test_getiptcinfo_jpg_found(self):
def test_getiptcinfo_jpg_found():
# Arrange
with Image.open(TEST_FILE) as im:
@ -27,11 +27,12 @@ class TestFileIptc(PillowTestCase):
iptc = IptcImagePlugin.getiptcinfo(im)
# Assert
self.assertIsInstance(iptc, dict)
self.assertEqual(iptc[(2, 90)], b"Budapest")
self.assertEqual(iptc[(2, 101)], b"Hungary")
assert isinstance(iptc, dict)
assert iptc[(2, 90)] == b"Budapest"
assert iptc[(2, 101)] == b"Hungary"
def test_getiptcinfo_tiff_none(self):
def test_getiptcinfo_tiff_none():
# Arrange
with Image.open("Tests/images/hopper.tif") as im:
@ -39,9 +40,10 @@ class TestFileIptc(PillowTestCase):
iptc = IptcImagePlugin.getiptcinfo(im)
# Assert
self.assertIsNone(iptc)
assert iptc is None
def test_i(self):
def test_i():
# Arrange
c = b"a"
@ -49,9 +51,10 @@ class TestFileIptc(PillowTestCase):
ret = IptcImagePlugin.i(c)
# Assert
self.assertEqual(ret, 97)
assert ret == 97
def test_dump(self):
def test_dump():
# Arrange
c = b"abc"
# Temporarily redirect stdout
@ -65,4 +68,4 @@ class TestFileIptc(PillowTestCase):
sys.stdout = old_stdout
# Assert
self.assertEqual(mystdout.getvalue(), "61 62 63 \n")
assert mystdout.getvalue() == "61 62 63 \n"

View File

@ -1,8 +1,9 @@
from io import BytesIO
import pytest
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
PIL151 = b"""
#define basic_width 32
@ -28,14 +29,14 @@ static char basic_bits[] = {
"""
class TestFileXbm(PillowTestCase):
def test_pil151(self):
def test_pil151():
with Image.open(BytesIO(PIL151)) as im:
im.load()
self.assertEqual(im.mode, "1")
self.assertEqual(im.size, (32, 32))
assert im.mode == "1"
assert im.size == (32, 32)
def test_open(self):
def test_open():
# Arrange
# Created with `convert hopper.png hopper.xbm`
filename = "Tests/images/hopper.xbm"
@ -44,10 +45,11 @@ class TestFileXbm(PillowTestCase):
with Image.open(filename) as im:
# Assert
self.assertEqual(im.mode, "1")
self.assertEqual(im.size, (128, 128))
assert im.mode == "1"
assert im.size == (128, 128)
def test_open_filename_with_underscore(self):
def test_open_filename_with_underscore():
# Arrange
# Created with `convert hopper.png hopper_underscore.xbm`
filename = "Tests/images/hopper_underscore.xbm"
@ -56,22 +58,24 @@ class TestFileXbm(PillowTestCase):
with Image.open(filename) as im:
# Assert
self.assertEqual(im.mode, "1")
self.assertEqual(im.size, (128, 128))
assert im.mode == "1"
assert im.size == (128, 128)
def test_save_wrong_mode(self):
def test_save_wrong_mode(tmp_path):
im = hopper()
out = self.tempfile("temp.xbm")
out = str(tmp_path / "temp.xbm")
with self.assertRaises(OSError):
with pytest.raises(OSError):
im.save(out)
def test_hotspot(self):
def test_hotspot(tmp_path):
im = hopper("1")
out = self.tempfile("temp.xbm")
out = str(tmp_path / "temp.xbm")
hotspot = (0, 7)
im.save(out, hotspot=hotspot)
with Image.open(out) as reloaded:
self.assertEqual(reloaded.info["hotspot"], hotspot)
assert reloaded.info["hotspot"] == hotspot

View File

@ -1,19 +1,18 @@
import pytest
from PIL import BdfFontFile, FontFile
from .helper import PillowTestCase
filename = "Tests/images/courB08.bdf"
class TestFontBdf(PillowTestCase):
def test_sanity(self):
def test_sanity():
with open(filename, "rb") as test_file:
font = BdfFontFile.BdfFontFile(test_file)
self.assertIsInstance(font, FontFile.FontFile)
self.assertEqual(len([_f for _f in font.glyph if _f]), 190)
assert isinstance(font, FontFile.FontFile)
assert len([_f for _f in font.glyph if _f]) == 190
def test_invalid_file(self):
def test_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, BdfFontFile.BdfFontFile, fp)
with pytest.raises(SyntaxError):
BdfFontFile.BdfFontFile(fp)

View File

@ -1,30 +1,31 @@
import pytest
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
im = hopper().resize((128, 100))
class TestImageArray(PillowTestCase):
def test_toarray(self):
def test_toarray():
def test(mode):
ai = im.convert(mode).__array_interface__
return ai["version"], ai["shape"], ai["typestr"], len(ai["data"])
# self.assertEqual(test("1"), (3, (100, 128), '|b1', 1600))
self.assertEqual(test("L"), (3, (100, 128), "|u1", 12800))
# assert test("1") == (3, (100, 128), '|b1', 1600))
assert test("L") == (3, (100, 128), "|u1", 12800)
# FIXME: wrong?
self.assertEqual(test("I"), (3, (100, 128), Image._ENDIAN + "i4", 51200))
assert test("I") == (3, (100, 128), Image._ENDIAN + "i4", 51200)
# FIXME: wrong?
self.assertEqual(test("F"), (3, (100, 128), Image._ENDIAN + "f4", 51200))
assert test("F") == (3, (100, 128), Image._ENDIAN + "f4", 51200)
self.assertEqual(test("LA"), (3, (100, 128, 2), "|u1", 25600))
self.assertEqual(test("RGB"), (3, (100, 128, 3), "|u1", 38400))
self.assertEqual(test("RGBA"), (3, (100, 128, 4), "|u1", 51200))
self.assertEqual(test("RGBX"), (3, (100, 128, 4), "|u1", 51200))
assert test("LA") == (3, (100, 128, 2), "|u1", 25600)
assert test("RGB") == (3, (100, 128, 3), "|u1", 38400)
assert test("RGBA") == (3, (100, 128, 4), "|u1", 51200)
assert test("RGBX") == (3, (100, 128, 4), "|u1", 51200)
def test_fromarray(self):
def test_fromarray():
class Wrapper:
""" Class with API matching Image.fromarray """
@ -44,16 +45,16 @@ class TestImageArray(PillowTestCase):
out = Image.fromarray(wrapped)
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
# self.assertEqual(test("1"), ("1", (128, 100), True))
self.assertEqual(test("L"), ("L", (128, 100), True))
self.assertEqual(test("I"), ("I", (128, 100), True))
self.assertEqual(test("F"), ("F", (128, 100), True))
self.assertEqual(test("LA"), ("LA", (128, 100), True))
self.assertEqual(test("RGB"), ("RGB", (128, 100), True))
self.assertEqual(test("RGBA"), ("RGBA", (128, 100), True))
self.assertEqual(test("RGBX"), ("RGBA", (128, 100), True))
# assert test("1") == ("1", (128, 100), True)
assert test("L") == ("L", (128, 100), True)
assert test("I") == ("I", (128, 100), True)
assert test("F") == ("F", (128, 100), True)
assert test("LA") == ("LA", (128, 100), True)
assert test("RGB") == ("RGB", (128, 100), True)
assert test("RGBA") == ("RGBA", (128, 100), True)
assert test("RGBX") == ("RGBA", (128, 100), True)
# Test mode is None with no "typestr" in the array interface
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
Image.fromarray(wrapped)

View File

@ -2,40 +2,40 @@ import copy
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageCopy(PillowTestCase):
def test_copy(self):
def test_copy():
croppedCoordinates = (10, 10, 20, 20)
croppedSize = (10, 10)
for mode in "1", "P", "L", "RGB", "I", "F":
# Internal copy method
im = hopper(mode)
out = im.copy()
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, im.size)
assert out.mode == im.mode
assert out.size == im.size
# Python's copy method
im = hopper(mode)
out = copy.copy(im)
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, im.size)
assert out.mode == im.mode
assert out.size == im.size
# Internal copy method on a cropped image
im = hopper(mode)
out = im.crop(croppedCoordinates).copy()
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, croppedSize)
assert out.mode == im.mode
assert out.size == croppedSize
# Python's copy method on a cropped image
im = hopper(mode)
out = copy.copy(im.crop(croppedCoordinates))
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, croppedSize)
assert out.mode == im.mode
assert out.size == croppedSize
def test_copy_zero(self):
def test_copy_zero():
im = Image.new("RGB", (0, 0))
out = im.copy()
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, im.size)
assert out.mode == im.mode
assert out.size == im.size

View File

@ -1,8 +1,7 @@
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageGetColors(PillowTestCase):
def test_getcolors(self):
def test_getcolors():
def getcolors(mode, limit=None):
im = hopper(mode)
if limit:
@ -13,30 +12,32 @@ class TestImageGetColors(PillowTestCase):
return len(colors)
return None
self.assertEqual(getcolors("1"), 2)
self.assertEqual(getcolors("L"), 255)
self.assertEqual(getcolors("I"), 255)
self.assertEqual(getcolors("F"), 255)
self.assertEqual(getcolors("P"), 90) # fixed palette
self.assertIsNone(getcolors("RGB"))
self.assertIsNone(getcolors("RGBA"))
self.assertIsNone(getcolors("CMYK"))
self.assertIsNone(getcolors("YCbCr"))
assert getcolors("1") == 2
assert getcolors("L") == 255
assert getcolors("I") == 255
assert getcolors("F") == 255
assert getcolors("P") == 90 # fixed palette
assert getcolors("RGB") is None
assert getcolors("RGBA") is None
assert getcolors("CMYK") is None
assert getcolors("YCbCr") is None
self.assertIsNone(getcolors("L", 128))
self.assertEqual(getcolors("L", 1024), 255)
assert getcolors("L", 128) is None
assert getcolors("L", 1024) == 255
self.assertIsNone(getcolors("RGB", 8192))
self.assertEqual(getcolors("RGB", 16384), 10100)
self.assertEqual(getcolors("RGB", 100000), 10100)
assert getcolors("RGB", 8192) is None
assert getcolors("RGB", 16384) == 10100
assert getcolors("RGB", 100000) == 10100
self.assertEqual(getcolors("RGBA", 16384), 10100)
self.assertEqual(getcolors("CMYK", 16384), 10100)
self.assertEqual(getcolors("YCbCr", 16384), 9329)
assert getcolors("RGBA", 16384) == 10100
assert getcolors("CMYK", 16384) == 10100
assert getcolors("YCbCr", 16384) == 9329
# --------------------------------------------------------------------
def test_pack(self):
# --------------------------------------------------------------------
def test_pack():
# Pack problems for small tables (@PIL209)
im = hopper().quantize(3).convert("RGB")
@ -48,20 +49,20 @@ class TestImageGetColors(PillowTestCase):
]
A = im.getcolors(maxcolors=2)
self.assertIsNone(A)
assert A is None
A = im.getcolors(maxcolors=3)
A.sort()
self.assertEqual(A, expected)
assert A == expected
A = im.getcolors(maxcolors=4)
A.sort()
self.assertEqual(A, expected)
assert A == expected
A = im.getcolors(maxcolors=8)
A.sort()
self.assertEqual(A, expected)
assert A == expected
A = im.getcolors(maxcolors=16)
A.sort()
self.assertEqual(A, expected)
assert A == expected

View File

@ -1,29 +1,28 @@
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageGetData(PillowTestCase):
def test_sanity(self):
def test_sanity():
data = hopper().getdata()
len(data)
list(data)
self.assertEqual(data[0], (20, 20, 70))
assert data[0] == (20, 20, 70)
def test_roundtrip(self):
def test_roundtrip():
def getdata(mode):
im = hopper(mode).resize((32, 30), Image.NEAREST)
data = im.getdata()
return data[0], len(data), len(list(data))
self.assertEqual(getdata("1"), (0, 960, 960))
self.assertEqual(getdata("L"), (17, 960, 960))
self.assertEqual(getdata("I"), (17, 960, 960))
self.assertEqual(getdata("F"), (17.0, 960, 960))
self.assertEqual(getdata("RGB"), ((11, 13, 52), 960, 960))
self.assertEqual(getdata("RGBA"), ((11, 13, 52, 255), 960, 960))
self.assertEqual(getdata("CMYK"), ((244, 242, 203, 0), 960, 960))
self.assertEqual(getdata("YCbCr"), ((16, 147, 123), 960, 960))
assert getdata("1") == (0, 960, 960)
assert getdata("L") == (17, 960, 960)
assert getdata("I") == (17, 960, 960)
assert getdata("F") == (17.0, 960, 960)
assert getdata("RGB") == ((11, 13, 52), 960, 960)
assert getdata("RGBA") == ((11, 13, 52, 255), 960, 960)
assert getdata("CMYK") == ((244, 242, 203, 0), 960, 960)
assert getdata("YCbCr") == ((16, 147, 123), 960, 960)

View File

@ -1,28 +1,28 @@
import pytest
from PIL import Image
from .helper import PillowTestCase, hopper, is_big_endian, on_ci
from .helper import hopper, is_big_endian, on_ci
class TestImageGetExtrema(PillowTestCase):
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
def test_extrema(self):
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
def test_extrema():
def extrema(mode):
return hopper(mode).getextrema()
self.assertEqual(extrema("1"), (0, 255))
self.assertEqual(extrema("L"), (1, 255))
self.assertEqual(extrema("I"), (1, 255))
self.assertEqual(extrema("F"), (1, 255))
self.assertEqual(extrema("P"), (0, 225)) # fixed palette
self.assertEqual(extrema("RGB"), ((0, 255), (0, 255), (0, 255)))
self.assertEqual(extrema("RGBA"), ((0, 255), (0, 255), (0, 255), (255, 255)))
self.assertEqual(extrema("CMYK"), ((0, 255), (0, 255), (0, 255), (0, 0)))
self.assertEqual(extrema("I;16"), (1, 255))
assert extrema("1") == (0, 255)
assert extrema("L") == (1, 255)
assert extrema("I") == (1, 255)
assert extrema("F") == (1, 255)
assert extrema("P") == (0, 225) # fixed palette
assert extrema("RGB") == ((0, 255), (0, 255), (0, 255))
assert extrema("RGBA") == ((0, 255), (0, 255), (0, 255), (255, 255))
assert extrema("CMYK") == ((0, 255), (0, 255), (0, 255), (0, 0))
assert extrema("I;16") == (1, 255)
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
def test_true_16(self):
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
def test_true_16():
with Image.open("Tests/images/16_bit_noise.tif") as im:
self.assertEqual(im.mode, "I;16")
assert im.mode == "I;16"
extrema = im.getextrema()
self.assertEqual(extrema, (106, 285))
assert extrema == (106, 285)

View File

@ -1,31 +1,29 @@
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageGetProjection(PillowTestCase):
def test_sanity(self):
def test_sanity():
im = hopper()
projection = im.getprojection()
self.assertEqual(len(projection), 2)
self.assertEqual(len(projection[0]), im.size[0])
self.assertEqual(len(projection[1]), im.size[1])
assert len(projection) == 2
assert len(projection[0]) == im.size[0]
assert len(projection[1]) == im.size[1]
# 8-bit image
im = Image.new("L", (10, 10))
self.assertEqual(im.getprojection()[0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
self.assertEqual(im.getprojection()[1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
assert im.getprojection()[0] == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert im.getprojection()[1] == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
im.paste(255, (2, 4, 8, 6))
self.assertEqual(im.getprojection()[0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0])
self.assertEqual(im.getprojection()[1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0])
assert im.getprojection()[0] == [0, 0, 1, 1, 1, 1, 1, 1, 0, 0]
assert im.getprojection()[1] == [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
# 32-bit image
im = Image.new("RGB", (10, 10))
self.assertEqual(im.getprojection()[0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
self.assertEqual(im.getprojection()[1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
assert im.getprojection()[0] == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert im.getprojection()[1] == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
im.paste(255, (2, 4, 8, 6))
self.assertEqual(im.getprojection()[0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0])
self.assertEqual(im.getprojection()[1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0])
assert im.getprojection()[0] == [0, 0, 1, 1, 1, 1, 1, 1, 0, 0]
assert im.getprojection()[1] == [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]

View File

@ -1,18 +1,17 @@
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageHistogram(PillowTestCase):
def test_histogram(self):
def test_histogram():
def histogram(mode):
h = hopper(mode).histogram()
return len(h), min(h), max(h)
self.assertEqual(histogram("1"), (256, 0, 10994))
self.assertEqual(histogram("L"), (256, 0, 662))
self.assertEqual(histogram("I"), (256, 0, 662))
self.assertEqual(histogram("F"), (256, 0, 662))
self.assertEqual(histogram("P"), (256, 0, 1871))
self.assertEqual(histogram("RGB"), (768, 4, 675))
self.assertEqual(histogram("RGBA"), (1024, 0, 16384))
self.assertEqual(histogram("CMYK"), (1024, 0, 16384))
self.assertEqual(histogram("YCbCr"), (768, 0, 1908))
assert histogram("1") == (256, 0, 10994)
assert histogram("L") == (256, 0, 662)
assert histogram("I") == (256, 0, 662)
assert histogram("F") == (256, 0, 662)
assert histogram("P") == (256, 0, 1871)
assert histogram("RGB") == (768, 4, 675)
assert histogram("RGBA") == (1024, 0, 16384)
assert histogram("CMYK") == (1024, 0, 16384)
assert histogram("YCbCr") == (768, 0, 1908)

View File

@ -1,36 +1,40 @@
import os
import pytest
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageLoad(PillowTestCase):
def test_sanity(self):
def test_sanity():
im = hopper()
pix = im.load()
self.assertEqual(pix[0, 0], (20, 20, 70))
assert pix[0, 0] == (20, 20, 70)
def test_close(self):
def test_close():
im = Image.open("Tests/images/hopper.gif")
im.close()
self.assertRaises(ValueError, im.load)
self.assertRaises(ValueError, im.getpixel, (0, 0))
with pytest.raises(ValueError):
im.load()
with pytest.raises(ValueError):
im.getpixel((0, 0))
def test_contextmanager(self):
def test_contextmanager():
fn = None
with Image.open("Tests/images/hopper.gif") as im:
fn = im.fp.fileno()
os.fstat(fn)
self.assertRaises(OSError, os.fstat, fn)
with pytest.raises(OSError):
os.fstat(fn)
def test_contextmanager_non_exclusive_fp(self):
def test_contextmanager_non_exclusive_fp():
with open("Tests/images/hopper.gif", "rb") as fp:
with Image.open(fp):
pass
self.assertFalse(fp.closed)
assert not fp.closed

View File

@ -1,10 +1,9 @@
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageMode(PillowTestCase):
def test_sanity(self):
def test_sanity():
with hopper() as im:
im.mode
@ -19,11 +18,11 @@ class TestImageMode(PillowTestCase):
ImageMode.getmode("F")
m = ImageMode.getmode("1")
self.assertEqual(m.mode, "1")
self.assertEqual(str(m), "1")
self.assertEqual(m.bands, ("1",))
self.assertEqual(m.basemode, "L")
self.assertEqual(m.basetype, "L")
assert m.mode == "1"
assert str(m) == "1"
assert m.bands == ("1",)
assert m.basemode == "L"
assert m.basetype == "L"
for mode in (
"I;16",
@ -36,20 +35,21 @@ class TestImageMode(PillowTestCase):
"I;16NS",
):
m = ImageMode.getmode(mode)
self.assertEqual(m.mode, mode)
self.assertEqual(str(m), mode)
self.assertEqual(m.bands, ("I",))
self.assertEqual(m.basemode, "L")
self.assertEqual(m.basetype, "L")
assert m.mode == mode
assert str(m) == mode
assert m.bands == ("I",)
assert m.basemode == "L"
assert m.basetype == "L"
m = ImageMode.getmode("RGB")
self.assertEqual(m.mode, "RGB")
self.assertEqual(str(m), "RGB")
self.assertEqual(m.bands, ("R", "G", "B"))
self.assertEqual(m.basemode, "RGB")
self.assertEqual(m.basetype, "L")
assert m.mode == "RGB"
assert str(m) == "RGB"
assert m.bands == ("R", "G", "B")
assert m.basemode == "RGB"
assert m.basetype == "L"
def test_properties(self):
def test_properties():
def check(mode, *result):
signature = (
Image.getmodebase(mode),
@ -57,7 +57,7 @@ class TestImageMode(PillowTestCase):
Image.getmodebands(mode),
Image.getmodebandnames(mode),
)
self.assertEqual(signature, result)
assert signature == result
check("1", "L", "L", 1, ("1",))
check("L", "L", "L", 1, ("L",))

View File

@ -1,52 +1,48 @@
from PIL import Image
from .helper import PillowTestCase
class TestImagePutAlpha(PillowTestCase):
def test_interface(self):
def test_interface():
im = Image.new("RGBA", (1, 1), (1, 2, 3, 0))
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 0))
assert im.getpixel((0, 0)) == (1, 2, 3, 0)
im = Image.new("RGBA", (1, 1), (1, 2, 3))
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 255))
assert im.getpixel((0, 0)) == (1, 2, 3, 255)
im.putalpha(Image.new("L", im.size, 4))
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 4))
assert im.getpixel((0, 0)) == (1, 2, 3, 4)
im.putalpha(5)
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 5))
assert im.getpixel((0, 0)) == (1, 2, 3, 5)
def test_promote(self):
def test_promote():
im = Image.new("L", (1, 1), 1)
self.assertEqual(im.getpixel((0, 0)), 1)
assert im.getpixel((0, 0)) == 1
im.putalpha(2)
self.assertEqual(im.mode, "LA")
self.assertEqual(im.getpixel((0, 0)), (1, 2))
assert im.mode == "LA"
assert im.getpixel((0, 0)) == (1, 2)
im = Image.new("P", (1, 1), 1)
self.assertEqual(im.getpixel((0, 0)), 1)
assert im.getpixel((0, 0)) == 1
im.putalpha(2)
self.assertEqual(im.mode, "PA")
self.assertEqual(im.getpixel((0, 0)), (1, 2))
assert im.mode == "PA"
assert im.getpixel((0, 0)) == (1, 2)
im = Image.new("RGB", (1, 1), (1, 2, 3))
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3))
assert im.getpixel((0, 0)) == (1, 2, 3)
im.putalpha(4)
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 4))
assert im.mode == "RGBA"
assert im.getpixel((0, 0)) == (1, 2, 3, 4)
def test_readonly(self):
def test_readonly():
im = Image.new("RGB", (1, 1), (1, 2, 3))
im.readonly = 1
im.putalpha(4)
self.assertFalse(im.readonly)
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 4))
assert not im.readonly
assert im.mode == "RGBA"
assert im.getpixel((0, 0)) == (1, 2, 3, 4)

View File

@ -1,184 +1,192 @@
import pytest
from PIL import Image, ImageColor
from .helper import PillowTestCase
class TestImageColor(PillowTestCase):
def test_hash(self):
def test_hash():
# short 3 components
self.assertEqual((255, 0, 0), ImageColor.getrgb("#f00"))
self.assertEqual((0, 255, 0), ImageColor.getrgb("#0f0"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("#00f"))
assert (255, 0, 0) == ImageColor.getrgb("#f00")
assert (0, 255, 0) == ImageColor.getrgb("#0f0")
assert (0, 0, 255) == ImageColor.getrgb("#00f")
# short 4 components
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("#f000"))
self.assertEqual((0, 255, 0, 0), ImageColor.getrgb("#0f00"))
self.assertEqual((0, 0, 255, 0), ImageColor.getrgb("#00f0"))
self.assertEqual((0, 0, 0, 255), ImageColor.getrgb("#000f"))
assert (255, 0, 0, 0) == ImageColor.getrgb("#f000")
assert (0, 255, 0, 0) == ImageColor.getrgb("#0f00")
assert (0, 0, 255, 0) == ImageColor.getrgb("#00f0")
assert (0, 0, 0, 255) == ImageColor.getrgb("#000f")
# long 3 components
self.assertEqual((222, 0, 0), ImageColor.getrgb("#de0000"))
self.assertEqual((0, 222, 0), ImageColor.getrgb("#00de00"))
self.assertEqual((0, 0, 222), ImageColor.getrgb("#0000de"))
assert (222, 0, 0) == ImageColor.getrgb("#de0000")
assert (0, 222, 0) == ImageColor.getrgb("#00de00")
assert (0, 0, 222) == ImageColor.getrgb("#0000de")
# long 4 components
self.assertEqual((222, 0, 0, 0), ImageColor.getrgb("#de000000"))
self.assertEqual((0, 222, 0, 0), ImageColor.getrgb("#00de0000"))
self.assertEqual((0, 0, 222, 0), ImageColor.getrgb("#0000de00"))
self.assertEqual((0, 0, 0, 222), ImageColor.getrgb("#000000de"))
assert (222, 0, 0, 0) == ImageColor.getrgb("#de000000")
assert (0, 222, 0, 0) == ImageColor.getrgb("#00de0000")
assert (0, 0, 222, 0) == ImageColor.getrgb("#0000de00")
assert (0, 0, 0, 222) == ImageColor.getrgb("#000000de")
# case insensitivity
self.assertEqual(ImageColor.getrgb("#DEF"), ImageColor.getrgb("#def"))
self.assertEqual(ImageColor.getrgb("#CDEF"), ImageColor.getrgb("#cdef"))
self.assertEqual(ImageColor.getrgb("#DEFDEF"), ImageColor.getrgb("#defdef"))
self.assertEqual(ImageColor.getrgb("#CDEFCDEF"), ImageColor.getrgb("#cdefcdef"))
assert ImageColor.getrgb("#DEF") == ImageColor.getrgb("#def")
assert ImageColor.getrgb("#CDEF") == ImageColor.getrgb("#cdef")
assert ImageColor.getrgb("#DEFDEF") == ImageColor.getrgb("#defdef")
assert ImageColor.getrgb("#CDEFCDEF") == ImageColor.getrgb("#cdefcdef")
# not a number
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo00")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0000")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo000000")
with pytest.raises(ValueError):
ImageColor.getrgb("#fo0")
with pytest.raises(ValueError):
ImageColor.getrgb("#fo00")
with pytest.raises(ValueError):
ImageColor.getrgb("#fo0000")
with pytest.raises(ValueError):
ImageColor.getrgb("#fo000000")
# wrong number of components
self.assertRaises(ValueError, ImageColor.getrgb, "#f0000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000 ")
with pytest.raises(ValueError):
ImageColor.getrgb("#f0000")
with pytest.raises(ValueError):
ImageColor.getrgb("#f000000")
with pytest.raises(ValueError):
ImageColor.getrgb("#f00000000")
with pytest.raises(ValueError):
ImageColor.getrgb("#f000000000")
with pytest.raises(ValueError):
ImageColor.getrgb("#f00000 ")
def test_colormap(self):
self.assertEqual((0, 0, 0), ImageColor.getrgb("black"))
self.assertEqual((255, 255, 255), ImageColor.getrgb("white"))
self.assertEqual((255, 255, 255), ImageColor.getrgb("WHITE"))
self.assertRaises(ValueError, ImageColor.getrgb, "black ")
def test_colormap():
assert (0, 0, 0) == ImageColor.getrgb("black")
assert (255, 255, 255) == ImageColor.getrgb("white")
assert (255, 255, 255) == ImageColor.getrgb("WHITE")
def test_functions(self):
with pytest.raises(ValueError):
ImageColor.getrgb("black ")
def test_functions():
# rgb numbers
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)"))
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0,255,0)"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0,0,255)"))
assert (255, 0, 0) == ImageColor.getrgb("rgb(255,0,0)")
assert (0, 255, 0) == ImageColor.getrgb("rgb(0,255,0)")
assert (0, 0, 255) == ImageColor.getrgb("rgb(0,0,255)")
# percents
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)"))
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0%,100%,0%)"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0%,0%,100%)"))
assert (255, 0, 0) == ImageColor.getrgb("rgb(100%,0%,0%)")
assert (0, 255, 0) == ImageColor.getrgb("rgb(0%,100%,0%)")
assert (0, 0, 255) == ImageColor.getrgb("rgb(0%,0%,100%)")
# rgba numbers
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)"))
self.assertEqual((0, 255, 0, 0), ImageColor.getrgb("rgba(0,255,0,0)"))
self.assertEqual((0, 0, 255, 0), ImageColor.getrgb("rgba(0,0,255,0)"))
self.assertEqual((0, 0, 0, 255), ImageColor.getrgb("rgba(0,0,0,255)"))
assert (255, 0, 0, 0) == ImageColor.getrgb("rgba(255,0,0,0)")
assert (0, 255, 0, 0) == ImageColor.getrgb("rgba(0,255,0,0)")
assert (0, 0, 255, 0) == ImageColor.getrgb("rgba(0,0,255,0)")
assert (0, 0, 0, 255) == ImageColor.getrgb("rgba(0,0,0,255)")
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(0,100%,50%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360,100%,50%)"))
self.assertEqual((0, 255, 255), ImageColor.getrgb("hsl(180,100%,50%)"))
assert (255, 0, 0) == ImageColor.getrgb("hsl(0,100%,50%)")
assert (255, 0, 0) == ImageColor.getrgb("hsl(360,100%,50%)")
assert (0, 255, 255) == ImageColor.getrgb("hsl(180,100%,50%)")
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(0,100%,100%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(360,100%,100%)"))
self.assertEqual((0, 255, 255), ImageColor.getrgb("hsv(180,100%,100%)"))
assert (255, 0, 0) == ImageColor.getrgb("hsv(0,100%,100%)")
assert (255, 0, 0) == ImageColor.getrgb("hsv(360,100%,100%)")
assert (0, 255, 255) == ImageColor.getrgb("hsv(180,100%,100%)")
# alternate format
self.assertEqual(
ImageColor.getrgb("hsb(0,100%,50%)"), ImageColor.getrgb("hsv(0,100%,50%)")
)
assert ImageColor.getrgb("hsb(0,100%,50%)") == ImageColor.getrgb("hsv(0,100%,50%)")
# floats
self.assertEqual((254, 3, 3), ImageColor.getrgb("hsl(0.1,99.2%,50.3%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360.,100.0%,50%)"))
assert (254, 3, 3) == ImageColor.getrgb("hsl(0.1,99.2%,50.3%)")
assert (255, 0, 0) == ImageColor.getrgb("hsl(360.,100.0%,50%)")
self.assertEqual((253, 2, 2), ImageColor.getrgb("hsv(0.1,99.2%,99.3%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(360.,100.0%,100%)"))
assert (253, 2, 2) == ImageColor.getrgb("hsv(0.1,99.2%,99.3%)")
assert (255, 0, 0) == ImageColor.getrgb("hsv(360.,100.0%,100%)")
# case insensitivity
self.assertEqual(
ImageColor.getrgb("RGB(255,0,0)"), ImageColor.getrgb("rgb(255,0,0)")
)
self.assertEqual(
ImageColor.getrgb("RGB(100%,0%,0%)"), ImageColor.getrgb("rgb(100%,0%,0%)")
)
self.assertEqual(
ImageColor.getrgb("RGBA(255,0,0,0)"), ImageColor.getrgb("rgba(255,0,0,0)")
)
self.assertEqual(
ImageColor.getrgb("HSL(0,100%,50%)"), ImageColor.getrgb("hsl(0,100%,50%)")
)
self.assertEqual(
ImageColor.getrgb("HSV(0,100%,50%)"), ImageColor.getrgb("hsv(0,100%,50%)")
)
self.assertEqual(
ImageColor.getrgb("HSB(0,100%,50%)"), ImageColor.getrgb("hsb(0,100%,50%)")
)
assert ImageColor.getrgb("RGB(255,0,0)") == ImageColor.getrgb("rgb(255,0,0)")
assert ImageColor.getrgb("RGB(100%,0%,0%)") == ImageColor.getrgb("rgb(100%,0%,0%)")
assert ImageColor.getrgb("RGBA(255,0,0,0)") == ImageColor.getrgb("rgba(255,0,0,0)")
assert ImageColor.getrgb("HSL(0,100%,50%)") == ImageColor.getrgb("hsl(0,100%,50%)")
assert ImageColor.getrgb("HSV(0,100%,50%)") == ImageColor.getrgb("hsv(0,100%,50%)")
assert ImageColor.getrgb("HSB(0,100%,50%)") == ImageColor.getrgb("hsb(0,100%,50%)")
# space agnosticism
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb( 255 , 0 , 0 )"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb( 100% , 0% , 0% )"))
self.assertEqual(
(255, 0, 0, 0), ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")
)
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl( 0 , 100% , 50% )"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv( 0 , 100% , 100% )"))
assert (255, 0, 0) == ImageColor.getrgb("rgb( 255 , 0 , 0 )")
assert (255, 0, 0) == ImageColor.getrgb("rgb( 100% , 0% , 0% )")
assert (255, 0, 0, 0) == ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")
assert (255, 0, 0) == ImageColor.getrgb("hsl( 0 , 100% , 50% )")
assert (255, 0, 0) == ImageColor.getrgb("hsv( 0 , 100% , 100% )")
# wrong number of components
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0,0,0)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgb(255,0)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgb(255,0,0,0)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%,0)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%,0 %)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%,0%,0%)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgb(100%,0%)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgb(100%,0%,0)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgb(100%,0%,0 %)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgb(100%,0%,0%,0%)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgba(255,0,0)")
self.assertRaises(ValueError, ImageColor.getrgb, "rgba(255,0,0,0,0)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgba(255,0,0)")
with pytest.raises(ValueError):
ImageColor.getrgb("rgba(255,0,0,0,0)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%,0%,0%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0%,100%,50%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100,50%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%,50)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsl(0,100%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsl(0,100%,0%,0%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsl(0%,100%,50%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsl(0,100,50%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsl(0,100%,50)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%,0%,0%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0%,100%,50%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100,50%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%,50)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsv(0,100%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsv(0,100%,0%,0%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsv(0%,100%,50%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsv(0,100,50%)")
with pytest.raises(ValueError):
ImageColor.getrgb("hsv(0,100%,50)")
# look for rounding errors (based on code by Tim Hatch)
def test_rounding_errors(self):
# look for rounding errors (based on code by Tim Hatch)
def test_rounding_errors():
for color in ImageColor.colormap:
expected = Image.new("RGB", (1, 1), color).convert("L").getpixel((0, 0))
actual = ImageColor.getcolor(color, "L")
self.assertEqual(expected, actual)
assert expected == actual
self.assertEqual(
(0, 255, 115), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")
)
assert (0, 255, 115) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")
Image.new("RGB", (1, 1), "white")
self.assertEqual((0, 0, 0, 255), ImageColor.getcolor("black", "RGBA"))
self.assertEqual((255, 255, 255, 255), ImageColor.getcolor("white", "RGBA"))
self.assertEqual(
(0, 255, 115, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGBA")
)
assert (0, 0, 0, 255) == ImageColor.getcolor("black", "RGBA")
assert (255, 255, 255, 255) == ImageColor.getcolor("white", "RGBA")
assert (0, 255, 115, 33) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGBA")
Image.new("RGBA", (1, 1), "white")
self.assertEqual(0, ImageColor.getcolor("black", "L"))
self.assertEqual(255, ImageColor.getcolor("white", "L"))
self.assertEqual(163, ImageColor.getcolor("rgba(0, 255, 115, 33)", "L"))
assert 0 == ImageColor.getcolor("black", "L")
assert 255 == ImageColor.getcolor("white", "L")
assert 163 == ImageColor.getcolor("rgba(0, 255, 115, 33)", "L")
Image.new("L", (1, 1), "white")
self.assertEqual(0, ImageColor.getcolor("black", "1"))
self.assertEqual(255, ImageColor.getcolor("white", "1"))
assert 0 == ImageColor.getcolor("black", "1")
assert 255 == ImageColor.getcolor("white", "1")
# The following test is wrong, but is current behavior
# The correct result should be 255 due to the mode 1
self.assertEqual(163, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
assert 163 == ImageColor.getcolor("rgba(0, 255, 115, 33)", "1")
# Correct behavior
# self.assertEqual(
# assert
# 255, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
Image.new("1", (1, 1), "white")
self.assertEqual((0, 255), ImageColor.getcolor("black", "LA"))
self.assertEqual((255, 255), ImageColor.getcolor("white", "LA"))
self.assertEqual((163, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA"))
assert (0, 255) == ImageColor.getcolor("black", "LA")
assert (255, 255) == ImageColor.getcolor("white", "LA")
assert (163, 33) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA")
Image.new("LA", (1, 1), "white")

View File

@ -1,7 +1,5 @@
from PIL import Image, ImageMath
from .helper import PillowTestCase
def pixel(im):
if hasattr(im, "im"):
@ -24,153 +22,168 @@ B2 = B.resize((2, 2))
images = {"A": A, "B": B, "F": F, "I": I}
class TestImageMath(PillowTestCase):
def test_sanity(self):
self.assertEqual(ImageMath.eval("1"), 1)
self.assertEqual(ImageMath.eval("1+A", A=2), 3)
self.assertEqual(pixel(ImageMath.eval("A+B", A=A, B=B)), "I 3")
self.assertEqual(pixel(ImageMath.eval("A+B", images)), "I 3")
self.assertEqual(pixel(ImageMath.eval("float(A)+B", images)), "F 3.0")
self.assertEqual(pixel(ImageMath.eval("int(float(A)+B)", images)), "I 3")
def test_sanity():
assert ImageMath.eval("1") == 1
assert ImageMath.eval("1+A", A=2) == 3
assert pixel(ImageMath.eval("A+B", A=A, B=B)) == "I 3"
assert pixel(ImageMath.eval("A+B", images)) == "I 3"
assert pixel(ImageMath.eval("float(A)+B", images)) == "F 3.0"
assert pixel(ImageMath.eval("int(float(A)+B)", images)) == "I 3"
def test_ops(self):
self.assertEqual(pixel(ImageMath.eval("-A", images)), "I -1")
self.assertEqual(pixel(ImageMath.eval("+B", images)), "L 2")
def test_ops():
assert pixel(ImageMath.eval("-A", images)) == "I -1"
assert pixel(ImageMath.eval("+B", images)) == "L 2"
self.assertEqual(pixel(ImageMath.eval("A+B", images)), "I 3")
self.assertEqual(pixel(ImageMath.eval("A-B", images)), "I -1")
self.assertEqual(pixel(ImageMath.eval("A*B", images)), "I 2")
self.assertEqual(pixel(ImageMath.eval("A/B", images)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B**2", images)), "I 4")
self.assertEqual(pixel(ImageMath.eval("B**33", images)), "I 2147483647")
assert pixel(ImageMath.eval("A+B", images)) == "I 3"
assert pixel(ImageMath.eval("A-B", images)) == "I -1"
assert pixel(ImageMath.eval("A*B", images)) == "I 2"
assert pixel(ImageMath.eval("A/B", images)) == "I 0"
assert pixel(ImageMath.eval("B**2", images)) == "I 4"
assert pixel(ImageMath.eval("B**33", images)) == "I 2147483647"
self.assertEqual(pixel(ImageMath.eval("float(A)+B", images)), "F 3.0")
self.assertEqual(pixel(ImageMath.eval("float(A)-B", images)), "F -1.0")
self.assertEqual(pixel(ImageMath.eval("float(A)*B", images)), "F 2.0")
self.assertEqual(pixel(ImageMath.eval("float(A)/B", images)), "F 0.5")
self.assertEqual(pixel(ImageMath.eval("float(B)**2", images)), "F 4.0")
self.assertEqual(
pixel(ImageMath.eval("float(B)**33", images)), "F 8589934592.0"
)
assert pixel(ImageMath.eval("float(A)+B", images)) == "F 3.0"
assert pixel(ImageMath.eval("float(A)-B", images)) == "F -1.0"
assert pixel(ImageMath.eval("float(A)*B", images)) == "F 2.0"
assert pixel(ImageMath.eval("float(A)/B", images)) == "F 0.5"
assert pixel(ImageMath.eval("float(B)**2", images)) == "F 4.0"
assert pixel(ImageMath.eval("float(B)**33", images)) == "F 8589934592.0"
def test_logical(self):
self.assertEqual(pixel(ImageMath.eval("not A", images)), 0)
self.assertEqual(pixel(ImageMath.eval("A and B", images)), "L 2")
self.assertEqual(pixel(ImageMath.eval("A or B", images)), "L 1")
def test_convert(self):
self.assertEqual(pixel(ImageMath.eval("convert(A+B, 'L')", images)), "L 3")
self.assertEqual(pixel(ImageMath.eval("convert(A+B, '1')", images)), "1 0")
self.assertEqual(
pixel(ImageMath.eval("convert(A+B, 'RGB')", images)), "RGB (3, 3, 3)"
)
def test_logical():
assert pixel(ImageMath.eval("not A", images)) == 0
assert pixel(ImageMath.eval("A and B", images)) == "L 2"
assert pixel(ImageMath.eval("A or B", images)) == "L 1"
def test_compare(self):
self.assertEqual(pixel(ImageMath.eval("min(A, B)", images)), "I 1")
self.assertEqual(pixel(ImageMath.eval("max(A, B)", images)), "I 2")
self.assertEqual(pixel(ImageMath.eval("A == 1", images)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A == 2", images)), "I 0")
def test_one_image_larger(self):
self.assertEqual(pixel(ImageMath.eval("A+B", A=A2, B=B)), "I 3")
self.assertEqual(pixel(ImageMath.eval("A+B", A=A, B=B2)), "I 3")
def test_convert():
assert pixel(ImageMath.eval("convert(A+B, 'L')", images)) == "L 3"
assert pixel(ImageMath.eval("convert(A+B, '1')", images)) == "1 0"
assert pixel(ImageMath.eval("convert(A+B, 'RGB')", images)) == "RGB (3, 3, 3)"
def test_abs(self):
self.assertEqual(pixel(ImageMath.eval("abs(A)", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("abs(B)", B=B)), "I 2")
def test_binary_mod(self):
self.assertEqual(pixel(ImageMath.eval("A%A", A=A)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B%B", B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A%B", A=A, B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B%A", A=A, B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z%A", A=A, Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z%B", B=B, Z=Z)), "I 0")
def test_compare():
assert pixel(ImageMath.eval("min(A, B)", images)) == "I 1"
assert pixel(ImageMath.eval("max(A, B)", images)) == "I 2"
assert pixel(ImageMath.eval("A == 1", images)) == "I 1"
assert pixel(ImageMath.eval("A == 2", images)) == "I 0"
def test_bitwise_invert(self):
self.assertEqual(pixel(ImageMath.eval("~Z", Z=Z)), "I -1")
self.assertEqual(pixel(ImageMath.eval("~A", A=A)), "I -2")
self.assertEqual(pixel(ImageMath.eval("~B", B=B)), "I -3")
def test_bitwise_and(self):
self.assertEqual(pixel(ImageMath.eval("Z&Z", A=A, Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z&A", A=A, Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A&Z", A=A, Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A&A", A=A, Z=Z)), "I 1")
def test_one_image_larger():
assert pixel(ImageMath.eval("A+B", A=A2, B=B)) == "I 3"
assert pixel(ImageMath.eval("A+B", A=A, B=B2)) == "I 3"
def test_bitwise_or(self):
self.assertEqual(pixel(ImageMath.eval("Z|Z", A=A, Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z|A", A=A, Z=Z)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A|Z", A=A, Z=Z)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A|A", A=A, Z=Z)), "I 1")
def test_bitwise_xor(self):
self.assertEqual(pixel(ImageMath.eval("Z^Z", A=A, Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z^A", A=A, Z=Z)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A^Z", A=A, Z=Z)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A^A", A=A, Z=Z)), "I 0")
def test_abs():
assert pixel(ImageMath.eval("abs(A)", A=A)) == "I 1"
assert pixel(ImageMath.eval("abs(B)", B=B)) == "I 2"
def test_bitwise_leftshift(self):
self.assertEqual(pixel(ImageMath.eval("Z<<0", Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z<<1", Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A<<0", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A<<1", A=A)), "I 2")
def test_bitwise_rightshift(self):
self.assertEqual(pixel(ImageMath.eval("Z>>0", Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("Z>>1", Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A>>0", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A>>1", A=A)), "I 0")
def test_binary_mod():
assert pixel(ImageMath.eval("A%A", A=A)) == "I 0"
assert pixel(ImageMath.eval("B%B", B=B)) == "I 0"
assert pixel(ImageMath.eval("A%B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.eval("B%A", A=A, B=B)) == "I 0"
assert pixel(ImageMath.eval("Z%A", A=A, Z=Z)) == "I 0"
assert pixel(ImageMath.eval("Z%B", B=B, Z=Z)) == "I 0"
def test_logical_eq(self):
self.assertEqual(pixel(ImageMath.eval("A==A", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B==B", B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A==B", A=A, B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B==A", A=A, B=B)), "I 0")
def test_logical_ne(self):
self.assertEqual(pixel(ImageMath.eval("A!=A", A=A)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B!=B", B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A!=B", A=A, B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B!=A", A=A, B=B)), "I 1")
def test_bitwise_invert():
assert pixel(ImageMath.eval("~Z", Z=Z)) == "I -1"
assert pixel(ImageMath.eval("~A", A=A)) == "I -2"
assert pixel(ImageMath.eval("~B", B=B)) == "I -3"
def test_logical_lt(self):
self.assertEqual(pixel(ImageMath.eval("A<A", A=A)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B<B", B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A<B", A=A, B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B<A", A=A, B=B)), "I 0")
def test_logical_le(self):
self.assertEqual(pixel(ImageMath.eval("A<=A", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B<=B", B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A<=B", A=A, B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B<=A", A=A, B=B)), "I 0")
def test_bitwise_and():
assert pixel(ImageMath.eval("Z&Z", A=A, Z=Z)) == "I 0"
assert pixel(ImageMath.eval("Z&A", A=A, Z=Z)) == "I 0"
assert pixel(ImageMath.eval("A&Z", A=A, Z=Z)) == "I 0"
assert pixel(ImageMath.eval("A&A", A=A, Z=Z)) == "I 1"
def test_logical_gt(self):
self.assertEqual(pixel(ImageMath.eval("A>A", A=A)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B>B", B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("A>B", A=A, B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B>A", A=A, B=B)), "I 1")
def test_logical_ge(self):
self.assertEqual(pixel(ImageMath.eval("A>=A", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("B>=B", B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("A>=B", A=A, B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("B>=A", A=A, B=B)), "I 1")
def test_bitwise_or():
assert pixel(ImageMath.eval("Z|Z", A=A, Z=Z)) == "I 0"
assert pixel(ImageMath.eval("Z|A", A=A, Z=Z)) == "I 1"
assert pixel(ImageMath.eval("A|Z", A=A, Z=Z)) == "I 1"
assert pixel(ImageMath.eval("A|A", A=A, Z=Z)) == "I 1"
def test_logical_equal(self):
self.assertEqual(pixel(ImageMath.eval("equal(A, A)", A=A)), "I 1")
self.assertEqual(pixel(ImageMath.eval("equal(B, B)", B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("equal(Z, Z)", Z=Z)), "I 1")
self.assertEqual(pixel(ImageMath.eval("equal(A, B)", A=A, B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("equal(B, A)", A=A, B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("equal(A, Z)", A=A, Z=Z)), "I 0")
def test_logical_not_equal(self):
self.assertEqual(pixel(ImageMath.eval("notequal(A, A)", A=A)), "I 0")
self.assertEqual(pixel(ImageMath.eval("notequal(B, B)", B=B)), "I 0")
self.assertEqual(pixel(ImageMath.eval("notequal(Z, Z)", Z=Z)), "I 0")
self.assertEqual(pixel(ImageMath.eval("notequal(A, B)", A=A, B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("notequal(B, A)", A=A, B=B)), "I 1")
self.assertEqual(pixel(ImageMath.eval("notequal(A, Z)", A=A, Z=Z)), "I 1")
def test_bitwise_xor():
assert pixel(ImageMath.eval("Z^Z", A=A, Z=Z)) == "I 0"
assert pixel(ImageMath.eval("Z^A", A=A, Z=Z)) == "I 1"
assert pixel(ImageMath.eval("A^Z", A=A, Z=Z)) == "I 1"
assert pixel(ImageMath.eval("A^A", A=A, Z=Z)) == "I 0"
def test_bitwise_leftshift():
assert pixel(ImageMath.eval("Z<<0", Z=Z)) == "I 0"
assert pixel(ImageMath.eval("Z<<1", Z=Z)) == "I 0"
assert pixel(ImageMath.eval("A<<0", A=A)) == "I 1"
assert pixel(ImageMath.eval("A<<1", A=A)) == "I 2"
def test_bitwise_rightshift():
assert pixel(ImageMath.eval("Z>>0", Z=Z)) == "I 0"
assert pixel(ImageMath.eval("Z>>1", Z=Z)) == "I 0"
assert pixel(ImageMath.eval("A>>0", A=A)) == "I 1"
assert pixel(ImageMath.eval("A>>1", A=A)) == "I 0"
def test_logical_eq():
assert pixel(ImageMath.eval("A==A", A=A)) == "I 1"
assert pixel(ImageMath.eval("B==B", B=B)) == "I 1"
assert pixel(ImageMath.eval("A==B", A=A, B=B)) == "I 0"
assert pixel(ImageMath.eval("B==A", A=A, B=B)) == "I 0"
def test_logical_ne():
assert pixel(ImageMath.eval("A!=A", A=A)) == "I 0"
assert pixel(ImageMath.eval("B!=B", B=B)) == "I 0"
assert pixel(ImageMath.eval("A!=B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.eval("B!=A", A=A, B=B)) == "I 1"
def test_logical_lt():
assert pixel(ImageMath.eval("A<A", A=A)) == "I 0"
assert pixel(ImageMath.eval("B<B", B=B)) == "I 0"
assert pixel(ImageMath.eval("A<B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.eval("B<A", A=A, B=B)) == "I 0"
def test_logical_le():
assert pixel(ImageMath.eval("A<=A", A=A)) == "I 1"
assert pixel(ImageMath.eval("B<=B", B=B)) == "I 1"
assert pixel(ImageMath.eval("A<=B", A=A, B=B)) == "I 1"
assert pixel(ImageMath.eval("B<=A", A=A, B=B)) == "I 0"
def test_logical_gt():
assert pixel(ImageMath.eval("A>A", A=A)) == "I 0"
assert pixel(ImageMath.eval("B>B", B=B)) == "I 0"
assert pixel(ImageMath.eval("A>B", A=A, B=B)) == "I 0"
assert pixel(ImageMath.eval("B>A", A=A, B=B)) == "I 1"
def test_logical_ge():
assert pixel(ImageMath.eval("A>=A", A=A)) == "I 1"
assert pixel(ImageMath.eval("B>=B", B=B)) == "I 1"
assert pixel(ImageMath.eval("A>=B", A=A, B=B)) == "I 0"
assert pixel(ImageMath.eval("B>=A", A=A, B=B)) == "I 1"
def test_logical_equal():
assert pixel(ImageMath.eval("equal(A, A)", A=A)) == "I 1"
assert pixel(ImageMath.eval("equal(B, B)", B=B)) == "I 1"
assert pixel(ImageMath.eval("equal(Z, Z)", Z=Z)) == "I 1"
assert pixel(ImageMath.eval("equal(A, B)", A=A, B=B)) == "I 0"
assert pixel(ImageMath.eval("equal(B, A)", A=A, B=B)) == "I 0"
assert pixel(ImageMath.eval("equal(A, Z)", A=A, Z=Z)) == "I 0"
def test_logical_not_equal():
assert pixel(ImageMath.eval("notequal(A, A)", A=A)) == "I 0"
assert pixel(ImageMath.eval("notequal(B, B)", B=B)) == "I 0"
assert pixel(ImageMath.eval("notequal(Z, Z)", Z=Z)) == "I 0"
assert pixel(ImageMath.eval("notequal(A, B)", A=A, B=B)) == "I 1"
assert pixel(ImageMath.eval("notequal(B, A)", A=A, B=B)) == "I 1"
assert pixel(ImageMath.eval("notequal(A, Z)", A=A, Z=Z)) == "I 1"

View File

@ -1,23 +1,23 @@
import unittest
import pytest
from PIL import Image, ImageShow
from .helper import PillowTestCase, hopper, is_win32, on_ci, on_github_actions
from .helper import hopper, is_win32, on_ci, on_github_actions
class TestImageShow(PillowTestCase):
def test_sanity(self):
def test_sanity():
dir(Image)
dir(ImageShow)
def test_register(self):
def test_register():
# Test registering a viewer that is not a class
ImageShow.register("not a class")
# Restore original state
ImageShow._viewers.pop()
def test_viewer_show(self):
def test_viewer_show():
class TestViewer(ImageShow.Viewer):
methodCalled = False
@ -30,28 +30,32 @@ class TestImageShow(PillowTestCase):
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
with hopper() as im:
self.assertTrue(ImageShow.show(im))
self.assertTrue(viewer.methodCalled)
assert ImageShow.show(im)
assert viewer.methodCalled
# Restore original state
ImageShow._viewers.pop(0)
@unittest.skipUnless(
on_ci() and not (is_win32() and on_github_actions()),
"Only run on CIs; hangs on Windows on GitHub Actions",
)
def test_show(self):
@pytest.mark.skipif(
not on_ci() or not (is_win32() and on_github_actions()),
reason="Only run on CIs; hangs on Windows on GitHub Actions",
)
def test_show():
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
im = hopper(mode)
self.assertTrue(ImageShow.show(im))
assert ImageShow.show(im)
def test_viewer(self):
def test_viewer():
viewer = ImageShow.Viewer()
self.assertIsNone(viewer.get_format(None))
assert viewer.get_format(None) is None
self.assertRaises(NotImplementedError, viewer.get_command, None)
with pytest.raises(NotImplementedError):
viewer.get_command(None)
def test_viewers(self):
def test_viewers():
for viewer in ImageShow._viewers:
viewer.get_command("test.jpg")

View File

@ -3,7 +3,7 @@ from io import BytesIO
from PIL import Image, ImageWin
from .helper import PillowTestCase, hopper, is_win32
from .helper import hopper, is_win32
# see https://github.com/python-pillow/Pillow/pull/1431#issuecomment-144692652
@ -81,11 +81,10 @@ if is_win32():
memcpy(bp + bf.bfOffBits, pixels, bi.biSizeImage)
return bytearray(buf)
class TestImageWinPointers(PillowTestCase):
def test_pointer(self):
def test_pointer(tmp_path):
im = hopper()
(width, height) = im.size
opath = self.tempfile("temp.png")
opath = str(tmp_path / "temp.png")
imdib = ImageWin.Dib(im)
hdr = BITMAPINFOHEADER()

View File

@ -1,38 +1,32 @@
import unittest
import pytest
from PIL import Image
from .helper import PillowTestCase
class TestLibImage(PillowTestCase):
def test_setmode(self):
def test_setmode():
im = Image.new("L", (1, 1), 255)
im.im.setmode("1")
self.assertEqual(im.im.getpixel((0, 0)), 255)
assert im.im.getpixel((0, 0)) == 255
im.im.setmode("L")
self.assertEqual(im.im.getpixel((0, 0)), 255)
assert im.im.getpixel((0, 0)) == 255
im = Image.new("1", (1, 1), 1)
im.im.setmode("L")
self.assertEqual(im.im.getpixel((0, 0)), 255)
assert im.im.getpixel((0, 0)) == 255
im.im.setmode("1")
self.assertEqual(im.im.getpixel((0, 0)), 255)
assert im.im.getpixel((0, 0)) == 255
im = Image.new("RGB", (1, 1), (1, 2, 3))
im.im.setmode("RGB")
self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3))
assert im.im.getpixel((0, 0)) == (1, 2, 3)
im.im.setmode("RGBA")
self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3, 255))
assert im.im.getpixel((0, 0)) == (1, 2, 3, 255)
im.im.setmode("RGBX")
self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3, 255))
assert im.im.getpixel((0, 0)) == (1, 2, 3, 255)
im.im.setmode("RGB")
self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3))
assert im.im.getpixel((0, 0)) == (1, 2, 3)
self.assertRaises(ValueError, im.im.setmode, "L")
self.assertRaises(ValueError, im.im.setmode, "RGBABCDE")
if __name__ == "__main__":
unittest.main()
with pytest.raises(ValueError):
im.im.setmode("L")
with pytest.raises(ValueError):
im.im.setmode("RGBABCDE")

View File

@ -4,11 +4,8 @@ from io import StringIO
from PIL import Image, PSDraw
from .helper import PillowTestCase
class TestPsDraw(PillowTestCase):
def _create_document(self, ps):
def _create_document(ps):
title = "hopper"
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
@ -29,32 +26,33 @@ class TestPsDraw(PillowTestCase):
ps.end_document()
def test_draw_postscript(self):
def test_draw_postscript(tmp_path):
# Based on Pillow tutorial, but there is no textsize:
# https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#drawing-postscript
# Arrange
tempfile = self.tempfile("temp.ps")
tempfile = str(tmp_path / "temp.ps")
with open(tempfile, "wb") as fp:
# Act
ps = PSDraw.PSDraw(fp)
self._create_document(ps)
_create_document(ps)
# Assert
# Check non-zero file was created
self.assertTrue(os.path.isfile(tempfile))
self.assertGreater(os.path.getsize(tempfile), 0)
assert os.path.isfile(tempfile)
assert os.path.getsize(tempfile) > 0
def test_stdout(self):
def test_stdout():
# Temporarily redirect stdout
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
ps = PSDraw.PSDraw()
self._create_document(ps)
_create_document(ps)
# Reset stdout
sys.stdout = old_stdout
self.assertNotEqual(mystdout.getvalue(), "")
assert mystdout.getvalue() != ""

View File

@ -1,18 +1,14 @@
import unittest
import pytest
from PIL import __version__
from .helper import PillowTestCase
try:
import pyroma
except ImportError:
pyroma = None
@unittest.skipIf(pyroma is None, "Pyroma is not installed")
class TestPyroma(PillowTestCase):
def test_pyroma(self):
@pytest.mark.skipif(pyroma is None, reason="Pyroma is not installed")
def test_pyroma():
# Arrange
data = pyroma.projectdata.get_data(".")
@ -22,11 +18,11 @@ class TestPyroma(PillowTestCase):
# Assert
if "rc" in __version__:
# Pyroma needs to chill about RC versions and not kill all our tests.
self.assertEqual(
rating,
(9, ["The package's version number does not comply with PEP-386."]),
assert rating == (
9,
["The package's version number does not comply with PEP-386."],
)
else:
# Should have a perfect score
self.assertEqual(rating, (10, []))
assert rating == (10, [])