mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-24 00:46:16 +03:00
Convert various tests to pytest style
This commit is contained in:
parent
0bbee693e0
commit
7fd9663198
|
@ -1,10 +1,8 @@
|
|||
import io
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
from PIL import features
|
||||
|
||||
from .helper import PillowTestCase
|
||||
|
||||
try:
|
||||
from PIL import _webp
|
||||
|
||||
|
@ -13,78 +11,85 @@ except ImportError:
|
|||
HAVE_WEBP = False
|
||||
|
||||
|
||||
class TestFeatures(PillowTestCase):
|
||||
def test_check(self):
|
||||
# Check the correctness of the convenience function
|
||||
for module in features.modules:
|
||||
self.assertEqual(features.check_module(module), features.check(module))
|
||||
for codec in features.codecs:
|
||||
self.assertEqual(features.check_codec(codec), features.check(codec))
|
||||
for feature in features.features:
|
||||
self.assertEqual(features.check_feature(feature), features.check(feature))
|
||||
def test_check():
|
||||
# Check the correctness of the convenience function
|
||||
for module in features.modules:
|
||||
assert features.check_module(module) == features.check(module)
|
||||
for codec in features.codecs:
|
||||
assert features.check_codec(codec) == features.check(codec)
|
||||
for feature in features.features:
|
||||
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):
|
||||
for feature in features.modules:
|
||||
self.assertIn(features.check_module(feature), [True, False])
|
||||
for feature in features.codecs:
|
||||
self.assertIn(features.check_codec(feature), [True, False])
|
||||
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
|
||||
def test_webp_mux():
|
||||
assert features.check("webp_mux") == _webp.HAVE_WEBPMUX
|
||||
|
||||
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):
|
||||
# Arrange
|
||||
codec = "unsupported_codec"
|
||||
# Act / Assert
|
||||
self.assertRaises(ValueError, features.check_codec, codec)
|
||||
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
|
||||
def test_webp_anim():
|
||||
assert features.check("webp_anim") == _webp.HAVE_WEBPANIM
|
||||
|
||||
def test_unsupported_module(self):
|
||||
# Arrange
|
||||
module = "unsupported_module"
|
||||
# Act / Assert
|
||||
self.assertRaises(ValueError, features.check_module, module)
|
||||
|
||||
def test_pilinfo(self):
|
||||
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 "))
|
||||
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)
|
||||
jpeg = (
|
||||
"\n"
|
||||
+ "-" * 68
|
||||
+ "\n"
|
||||
+ "JPEG image/jpeg\n"
|
||||
+ "Extensions: .jfif, .jpe, .jpeg, .jpg\n"
|
||||
+ "Features: open, save\n"
|
||||
+ "-" * 68
|
||||
+ "\n"
|
||||
)
|
||||
self.assertIn(jpeg, out)
|
||||
def test_check_modules():
|
||||
for feature in features.modules:
|
||||
assert features.check_module(feature) in [True, False]
|
||||
for feature in features.codecs:
|
||||
assert features.check_codec(feature) in [True, False]
|
||||
|
||||
|
||||
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
|
||||
with pytest.raises(ValueError):
|
||||
features.check_codec(codec)
|
||||
|
||||
|
||||
def test_unsupported_module():
|
||||
# Arrange
|
||||
module = "unsupported_module"
|
||||
# Act / Assert
|
||||
with pytest.raises(ValueError):
|
||||
features.check_module(module)
|
||||
|
||||
|
||||
def test_pilinfo():
|
||||
buf = io.StringIO()
|
||||
features.pilinfo(buf)
|
||||
out = buf.getvalue()
|
||||
lines = out.splitlines()
|
||||
assert lines[0] == "-" * 68
|
||||
assert lines[1].startswith("Pillow ")
|
||||
assert lines[2].startswith("Python ")
|
||||
lines = lines[3:]
|
||||
while lines[0].startswith(" "):
|
||||
lines = lines[1:]
|
||||
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
|
||||
+ "\n"
|
||||
+ "JPEG image/jpeg\n"
|
||||
+ "Extensions: .jfif, .jpe, .jpeg, .jpg\n"
|
||||
+ "Features: open, save\n"
|
||||
+ "-" * 68
|
||||
+ "\n"
|
||||
)
|
||||
assert jpeg in out
|
||||
|
|
|
@ -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):
|
||||
with Image.open(TEST_FILE) as im:
|
||||
self.assertEqual(im.size, (32, 32))
|
||||
self.assertIsInstance(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))
|
||||
def test_sanity():
|
||||
with Image.open(TEST_FILE) as im:
|
||||
assert im.size == (32, 32)
|
||||
assert isinstance(im, CurImagePlugin.CurImageFile)
|
||||
# Check some pixel colors to ensure image is loaded properly
|
||||
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):
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
self.assertRaises(SyntaxError, CurImagePlugin.CurImageFile, invalid_file)
|
||||
def test_invalid_file():
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
no_cursors_file = "Tests/images/no_cursors.cur"
|
||||
with pytest.raises(SyntaxError):
|
||||
CurImagePlugin.CurImageFile(invalid_file)
|
||||
|
||||
cur = CurImagePlugin.CurImageFile(TEST_FILE)
|
||||
cur.fp.close()
|
||||
with open(no_cursors_file, "rb") as cur.fp:
|
||||
self.assertRaises(TypeError, cur._open)
|
||||
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:
|
||||
with pytest.raises(TypeError):
|
||||
cur._open()
|
||||
|
|
|
@ -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):
|
||||
# Act
|
||||
with Image.open(TEST_FILE) as im:
|
||||
def test_open():
|
||||
# Act
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# Assert
|
||||
self.assertEqual(im.format, "FITS")
|
||||
# Assert
|
||||
assert im.format == "FITS"
|
||||
|
||||
# Dummy data from the stub
|
||||
self.assertEqual(im.mode, "F")
|
||||
self.assertEqual(im.size, (1, 1))
|
||||
# Dummy data from the stub
|
||||
assert im.mode == "F"
|
||||
assert im.size == (1, 1)
|
||||
|
||||
def test_invalid_file(self):
|
||||
# Arrange
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
# Act / Assert
|
||||
self.assertRaises(
|
||||
SyntaxError, FitsStubImagePlugin.FITSStubImageFile, invalid_file
|
||||
)
|
||||
def test_invalid_file():
|
||||
# Arrange
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
def test_load(self):
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
# Act / Assert
|
||||
with pytest.raises(SyntaxError):
|
||||
FitsStubImagePlugin.FITSStubImageFile(invalid_file)
|
||||
|
||||
# Act / Assert: stub cannot load without an implemented handler
|
||||
self.assertRaises(IOError, im.load)
|
||||
|
||||
def test_save(self):
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
dummy_fp = None
|
||||
dummy_filename = "dummy.filename"
|
||||
def test_load():
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# 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
|
||||
)
|
||||
# Act / Assert: stub cannot load without an implemented handler
|
||||
with pytest.raises(IOError):
|
||||
im.load()
|
||||
|
||||
|
||||
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
|
||||
with pytest.raises(IOError):
|
||||
im.save(dummy_filename)
|
||||
with pytest.raises(IOError):
|
||||
FitsStubImagePlugin._save(im, dummy_fp, dummy_filename)
|
||||
|
|
|
@ -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):
|
||||
# Act
|
||||
with Image.open(TEST_FILE) as im:
|
||||
def test_open():
|
||||
# Act
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# Assert
|
||||
self.assertEqual(im.format, "GRIB")
|
||||
# Assert
|
||||
assert im.format == "GRIB"
|
||||
|
||||
# Dummy data from the stub
|
||||
self.assertEqual(im.mode, "F")
|
||||
self.assertEqual(im.size, (1, 1))
|
||||
# Dummy data from the stub
|
||||
assert im.mode == "F"
|
||||
assert im.size == (1, 1)
|
||||
|
||||
def test_invalid_file(self):
|
||||
# Arrange
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
# Act / Assert
|
||||
self.assertRaises(
|
||||
SyntaxError, GribStubImagePlugin.GribStubImageFile, invalid_file
|
||||
)
|
||||
def test_invalid_file():
|
||||
# Arrange
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
def test_load(self):
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
# Act / Assert
|
||||
with pytest.raises(SyntaxError):
|
||||
GribStubImagePlugin.GribStubImageFile(invalid_file)
|
||||
|
||||
# Act / Assert: stub cannot load without an implemented handler
|
||||
self.assertRaises(IOError, im.load)
|
||||
|
||||
def test_save(self):
|
||||
# Arrange
|
||||
im = hopper()
|
||||
tmpfile = self.tempfile("temp.grib")
|
||||
def test_load():
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# Act / Assert: stub cannot save without an implemented handler
|
||||
self.assertRaises(IOError, im.save, tmpfile)
|
||||
# Act / Assert: stub cannot load without an implemented handler
|
||||
with pytest.raises(IOError):
|
||||
im.load()
|
||||
|
||||
|
||||
def test_save(tmp_path):
|
||||
# Arrange
|
||||
im = hopper()
|
||||
tmpfile = str(tmp_path / "temp.grib")
|
||||
|
||||
# Act / Assert: stub cannot save without an implemented handler
|
||||
with pytest.raises(IOError):
|
||||
im.save(tmpfile)
|
||||
|
|
|
@ -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):
|
||||
# Act
|
||||
with Image.open(TEST_FILE) as im:
|
||||
def test_open():
|
||||
# Act
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# Assert
|
||||
self.assertEqual(im.format, "HDF5")
|
||||
# Assert
|
||||
assert im.format == "HDF5"
|
||||
|
||||
# Dummy data from the stub
|
||||
self.assertEqual(im.mode, "F")
|
||||
self.assertEqual(im.size, (1, 1))
|
||||
# Dummy data from the stub
|
||||
assert im.mode == "F"
|
||||
assert im.size == (1, 1)
|
||||
|
||||
def test_invalid_file(self):
|
||||
# Arrange
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
# Act / Assert
|
||||
self.assertRaises(
|
||||
SyntaxError, Hdf5StubImagePlugin.HDF5StubImageFile, invalid_file
|
||||
)
|
||||
def test_invalid_file():
|
||||
# Arrange
|
||||
invalid_file = "Tests/images/flower.jpg"
|
||||
|
||||
def test_load(self):
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
# Act / Assert
|
||||
with pytest.raises(SyntaxError):
|
||||
Hdf5StubImagePlugin.HDF5StubImageFile(invalid_file)
|
||||
|
||||
# Act / Assert: stub cannot load without an implemented handler
|
||||
self.assertRaises(IOError, im.load)
|
||||
|
||||
def test_save(self):
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
dummy_fp = None
|
||||
dummy_filename = "dummy.filename"
|
||||
def test_load():
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# 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
|
||||
)
|
||||
# Act / Assert: stub cannot load without an implemented handler
|
||||
with pytest.raises(IOError):
|
||||
im.load()
|
||||
|
||||
|
||||
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
|
||||
with pytest.raises(IOError):
|
||||
im.save(dummy_filename)
|
||||
with pytest.raises(IOError):
|
||||
Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename)
|
||||
|
|
|
@ -3,66 +3,69 @@ 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):
|
||||
# Arrange
|
||||
with hopper() as im:
|
||||
|
||||
# Act
|
||||
iptc = IptcImagePlugin.getiptcinfo(im)
|
||||
|
||||
# Assert
|
||||
self.assertIsNone(iptc)
|
||||
|
||||
def test_getiptcinfo_jpg_found(self):
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# Act
|
||||
iptc = IptcImagePlugin.getiptcinfo(im)
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(iptc, dict)
|
||||
self.assertEqual(iptc[(2, 90)], b"Budapest")
|
||||
self.assertEqual(iptc[(2, 101)], b"Hungary")
|
||||
|
||||
def test_getiptcinfo_tiff_none(self):
|
||||
# Arrange
|
||||
with Image.open("Tests/images/hopper.tif") as im:
|
||||
|
||||
# Act
|
||||
iptc = IptcImagePlugin.getiptcinfo(im)
|
||||
|
||||
# Assert
|
||||
self.assertIsNone(iptc)
|
||||
|
||||
def test_i(self):
|
||||
# Arrange
|
||||
c = b"a"
|
||||
def test_getiptcinfo_jpg_none():
|
||||
# Arrange
|
||||
with hopper() as im:
|
||||
|
||||
# Act
|
||||
ret = IptcImagePlugin.i(c)
|
||||
iptc = IptcImagePlugin.getiptcinfo(im)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(ret, 97)
|
||||
# Assert
|
||||
assert iptc is None
|
||||
|
||||
def test_dump(self):
|
||||
# Arrange
|
||||
c = b"abc"
|
||||
# Temporarily redirect stdout
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
|
||||
def test_getiptcinfo_jpg_found():
|
||||
# Arrange
|
||||
with Image.open(TEST_FILE) as im:
|
||||
|
||||
# Act
|
||||
IptcImagePlugin.dump(c)
|
||||
iptc = IptcImagePlugin.getiptcinfo(im)
|
||||
|
||||
# Reset stdout
|
||||
sys.stdout = old_stdout
|
||||
# Assert
|
||||
assert isinstance(iptc, dict)
|
||||
assert iptc[(2, 90)] == b"Budapest"
|
||||
assert iptc[(2, 101)] == b"Hungary"
|
||||
|
||||
# Assert
|
||||
self.assertEqual(mystdout.getvalue(), "61 62 63 \n")
|
||||
|
||||
def test_getiptcinfo_tiff_none():
|
||||
# Arrange
|
||||
with Image.open("Tests/images/hopper.tif") as im:
|
||||
|
||||
# Act
|
||||
iptc = IptcImagePlugin.getiptcinfo(im)
|
||||
|
||||
# Assert
|
||||
assert iptc is None
|
||||
|
||||
|
||||
def test_i():
|
||||
# Arrange
|
||||
c = b"a"
|
||||
|
||||
# Act
|
||||
ret = IptcImagePlugin.i(c)
|
||||
|
||||
# Assert
|
||||
assert ret == 97
|
||||
|
||||
|
||||
def test_dump():
|
||||
# Arrange
|
||||
c = b"abc"
|
||||
# Temporarily redirect stdout
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
|
||||
# Act
|
||||
IptcImagePlugin.dump(c)
|
||||
|
||||
# Reset stdout
|
||||
sys.stdout = old_stdout
|
||||
|
||||
# Assert
|
||||
assert mystdout.getvalue() == "61 62 63 \n"
|
||||
|
|
|
@ -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,50 +29,53 @@ static char basic_bits[] = {
|
|||
"""
|
||||
|
||||
|
||||
class TestFileXbm(PillowTestCase):
|
||||
def test_pil151(self):
|
||||
with Image.open(BytesIO(PIL151)) as im:
|
||||
im.load()
|
||||
self.assertEqual(im.mode, "1")
|
||||
self.assertEqual(im.size, (32, 32))
|
||||
def test_pil151():
|
||||
with Image.open(BytesIO(PIL151)) as im:
|
||||
im.load()
|
||||
assert im.mode == "1"
|
||||
assert im.size == (32, 32)
|
||||
|
||||
def test_open(self):
|
||||
# Arrange
|
||||
# Created with `convert hopper.png hopper.xbm`
|
||||
filename = "Tests/images/hopper.xbm"
|
||||
|
||||
# Act
|
||||
with Image.open(filename) as im:
|
||||
def test_open():
|
||||
# Arrange
|
||||
# Created with `convert hopper.png hopper.xbm`
|
||||
filename = "Tests/images/hopper.xbm"
|
||||
|
||||
# Assert
|
||||
self.assertEqual(im.mode, "1")
|
||||
self.assertEqual(im.size, (128, 128))
|
||||
# Act
|
||||
with Image.open(filename) as im:
|
||||
|
||||
def test_open_filename_with_underscore(self):
|
||||
# Arrange
|
||||
# Created with `convert hopper.png hopper_underscore.xbm`
|
||||
filename = "Tests/images/hopper_underscore.xbm"
|
||||
# Assert
|
||||
assert im.mode == "1"
|
||||
assert im.size == (128, 128)
|
||||
|
||||
# Act
|
||||
with Image.open(filename) as im:
|
||||
|
||||
# Assert
|
||||
self.assertEqual(im.mode, "1")
|
||||
self.assertEqual(im.size, (128, 128))
|
||||
def test_open_filename_with_underscore():
|
||||
# Arrange
|
||||
# Created with `convert hopper.png hopper_underscore.xbm`
|
||||
filename = "Tests/images/hopper_underscore.xbm"
|
||||
|
||||
def test_save_wrong_mode(self):
|
||||
im = hopper()
|
||||
out = self.tempfile("temp.xbm")
|
||||
# Act
|
||||
with Image.open(filename) as im:
|
||||
|
||||
with self.assertRaises(OSError):
|
||||
im.save(out)
|
||||
# Assert
|
||||
assert im.mode == "1"
|
||||
assert im.size == (128, 128)
|
||||
|
||||
def test_hotspot(self):
|
||||
im = hopper("1")
|
||||
out = self.tempfile("temp.xbm")
|
||||
|
||||
hotspot = (0, 7)
|
||||
im.save(out, hotspot=hotspot)
|
||||
def test_save_wrong_mode(tmp_path):
|
||||
im = hopper()
|
||||
out = str(tmp_path / "temp.xbm")
|
||||
|
||||
with Image.open(out) as reloaded:
|
||||
self.assertEqual(reloaded.info["hotspot"], hotspot)
|
||||
with pytest.raises(OSError):
|
||||
im.save(out)
|
||||
|
||||
|
||||
def test_hotspot(tmp_path):
|
||||
im = hopper("1")
|
||||
out = str(tmp_path / "temp.xbm")
|
||||
|
||||
hotspot = (0, 7)
|
||||
im.save(out, hotspot=hotspot)
|
||||
|
||||
with Image.open(out) as reloaded:
|
||||
assert reloaded.info["hotspot"] == hotspot
|
||||
|
|
|
@ -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)
|
||||
|
||||
with open(filename, "rb") as test_file:
|
||||
font = BdfFontFile.BdfFontFile(test_file)
|
||||
assert isinstance(font, FontFile.FontFile)
|
||||
assert len([_f for _f in font.glyph if _f]) == 190
|
||||
|
||||
self.assertIsInstance(font, FontFile.FontFile)
|
||||
self.assertEqual(len([_f for _f in font.glyph if _f]), 190)
|
||||
|
||||
def test_invalid_file(self):
|
||||
with open("Tests/images/flower.jpg", "rb") as fp:
|
||||
self.assertRaises(SyntaxError, BdfFontFile.BdfFontFile, fp)
|
||||
def test_invalid_file():
|
||||
with open("Tests/images/flower.jpg", "rb") as fp:
|
||||
with pytest.raises(SyntaxError):
|
||||
BdfFontFile.BdfFontFile(fp)
|
||||
|
|
|
@ -1,59 +1,60 @@
|
|||
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(mode):
|
||||
ai = im.convert(mode).__array_interface__
|
||||
return ai["version"], ai["shape"], ai["typestr"], len(ai["data"])
|
||||
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))
|
||||
# FIXME: wrong?
|
||||
self.assertEqual(test("F"), (3, (100, 128), Image._ENDIAN + "f4", 51200))
|
||||
# FIXME: wrong?
|
||||
assert test("I") == (3, (100, 128), Image._ENDIAN + "i4", 51200)
|
||||
# FIXME: wrong?
|
||||
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):
|
||||
class Wrapper:
|
||||
""" Class with API matching Image.fromarray """
|
||||
|
||||
def __init__(self, img, arr_params):
|
||||
self.img = img
|
||||
self.__array_interface__ = arr_params
|
||||
def test_fromarray():
|
||||
class Wrapper:
|
||||
""" Class with API matching Image.fromarray """
|
||||
|
||||
def tobytes(self):
|
||||
return self.img.tobytes()
|
||||
def __init__(self, img, arr_params):
|
||||
self.img = img
|
||||
self.__array_interface__ = arr_params
|
||||
|
||||
def test(mode):
|
||||
i = im.convert(mode)
|
||||
a = i.__array_interface__
|
||||
a["strides"] = 1 # pretend it's non-contiguous
|
||||
# Make wrapper instance for image, new array interface
|
||||
wrapped = Wrapper(i, a)
|
||||
out = Image.fromarray(wrapped)
|
||||
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
|
||||
def tobytes(self):
|
||||
return self.img.tobytes()
|
||||
|
||||
# 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))
|
||||
def test(mode):
|
||||
i = im.convert(mode)
|
||||
a = i.__array_interface__
|
||||
a["strides"] = 1 # pretend it's non-contiguous
|
||||
# Make wrapper instance for image, new array interface
|
||||
wrapped = Wrapper(i, a)
|
||||
out = Image.fromarray(wrapped)
|
||||
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
|
||||
|
||||
# Test mode is None with no "typestr" in the array interface
|
||||
with self.assertRaises(TypeError):
|
||||
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
|
||||
Image.fromarray(wrapped)
|
||||
# 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 pytest.raises(TypeError):
|
||||
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
|
||||
Image.fromarray(wrapped)
|
||||
|
|
|
@ -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):
|
||||
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)
|
||||
|
||||
# Python's copy method
|
||||
im = hopper(mode)
|
||||
out = copy.copy(im)
|
||||
self.assertEqual(out.mode, im.mode)
|
||||
self.assertEqual(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)
|
||||
|
||||
# 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)
|
||||
|
||||
def test_copy_zero(self):
|
||||
im = Image.new("RGB", (0, 0))
|
||||
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)
|
||||
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()
|
||||
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))
|
||||
assert out.mode == im.mode
|
||||
assert out.size == croppedSize
|
||||
|
||||
|
||||
def test_copy_zero():
|
||||
im = Image.new("RGB", (0, 0))
|
||||
out = im.copy()
|
||||
assert out.mode == im.mode
|
||||
assert out.size == im.size
|
||||
|
|
|
@ -1,67 +1,68 @@
|
|||
from .helper import PillowTestCase, hopper
|
||||
from .helper import hopper
|
||||
|
||||
|
||||
class TestImageGetColors(PillowTestCase):
|
||||
def test_getcolors(self):
|
||||
def getcolors(mode, limit=None):
|
||||
im = hopper(mode)
|
||||
if limit:
|
||||
colors = im.getcolors(limit)
|
||||
else:
|
||||
colors = im.getcolors()
|
||||
if colors:
|
||||
return len(colors)
|
||||
return None
|
||||
def test_getcolors():
|
||||
def getcolors(mode, limit=None):
|
||||
im = hopper(mode)
|
||||
if limit:
|
||||
colors = im.getcolors(limit)
|
||||
else:
|
||||
colors = im.getcolors()
|
||||
if colors:
|
||||
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):
|
||||
# Pack problems for small tables (@PIL209)
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
im = hopper().quantize(3).convert("RGB")
|
||||
|
||||
expected = [
|
||||
(4039, (172, 166, 181)),
|
||||
(4385, (124, 113, 134)),
|
||||
(7960, (31, 20, 33)),
|
||||
]
|
||||
def test_pack():
|
||||
# Pack problems for small tables (@PIL209)
|
||||
|
||||
A = im.getcolors(maxcolors=2)
|
||||
self.assertIsNone(A)
|
||||
im = hopper().quantize(3).convert("RGB")
|
||||
|
||||
A = im.getcolors(maxcolors=3)
|
||||
A.sort()
|
||||
self.assertEqual(A, expected)
|
||||
expected = [
|
||||
(4039, (172, 166, 181)),
|
||||
(4385, (124, 113, 134)),
|
||||
(7960, (31, 20, 33)),
|
||||
]
|
||||
|
||||
A = im.getcolors(maxcolors=4)
|
||||
A.sort()
|
||||
self.assertEqual(A, expected)
|
||||
A = im.getcolors(maxcolors=2)
|
||||
assert A is None
|
||||
|
||||
A = im.getcolors(maxcolors=8)
|
||||
A.sort()
|
||||
self.assertEqual(A, expected)
|
||||
A = im.getcolors(maxcolors=3)
|
||||
A.sort()
|
||||
assert A == expected
|
||||
|
||||
A = im.getcolors(maxcolors=16)
|
||||
A.sort()
|
||||
self.assertEqual(A, expected)
|
||||
A = im.getcolors(maxcolors=4)
|
||||
A.sort()
|
||||
assert A == expected
|
||||
|
||||
A = im.getcolors(maxcolors=8)
|
||||
A.sort()
|
||||
assert A == expected
|
||||
|
||||
A = im.getcolors(maxcolors=16)
|
||||
A.sort()
|
||||
assert A == expected
|
||||
|
|
|
@ -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()
|
||||
|
||||
data = hopper().getdata()
|
||||
len(data)
|
||||
list(data)
|
||||
|
||||
len(data)
|
||||
list(data)
|
||||
assert data[0] == (20, 20, 70)
|
||||
|
||||
self.assertEqual(data[0], (20, 20, 70))
|
||||
|
||||
def test_roundtrip(self):
|
||||
def getdata(mode):
|
||||
im = hopper(mode).resize((32, 30), Image.NEAREST)
|
||||
data = im.getdata()
|
||||
return data[0], len(data), len(list(data))
|
||||
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)
|
||||
|
|
|
@ -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):
|
||||
def extrema(mode):
|
||||
return hopper(mode).getextrema()
|
||||
@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):
|
||||
with Image.open("Tests/images/16_bit_noise.tif") as im:
|
||||
self.assertEqual(im.mode, "I;16")
|
||||
extrema = im.getextrema()
|
||||
self.assertEqual(extrema, (106, 285))
|
||||
|
||||
@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:
|
||||
assert im.mode == "I;16"
|
||||
extrema = im.getextrema()
|
||||
assert extrema == (106, 285)
|
||||
|
|
|
@ -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()
|
||||
|
||||
im = hopper()
|
||||
projection = im.getprojection()
|
||||
|
||||
projection = im.getprojection()
|
||||
assert len(projection) == 2
|
||||
assert len(projection[0]) == im.size[0]
|
||||
assert len(projection[1]) == im.size[1]
|
||||
|
||||
self.assertEqual(len(projection), 2)
|
||||
self.assertEqual(len(projection[0]), im.size[0])
|
||||
self.assertEqual(len(projection[1]), im.size[1])
|
||||
# 8-bit image
|
||||
im = Image.new("L", (10, 10))
|
||||
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))
|
||||
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]
|
||||
|
||||
# 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])
|
||||
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])
|
||||
|
||||
# 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])
|
||||
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])
|
||||
# 32-bit image
|
||||
im = Image.new("RGB", (10, 10))
|
||||
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))
|
||||
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]
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
from .helper import PillowTestCase, hopper
|
||||
from .helper import hopper
|
||||
|
||||
|
||||
class TestImageHistogram(PillowTestCase):
|
||||
def test_histogram(self):
|
||||
def histogram(mode):
|
||||
h = hopper(mode).histogram()
|
||||
return len(h), min(h), max(h)
|
||||
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)
|
||||
|
|
|
@ -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()
|
||||
|
||||
im = hopper()
|
||||
assert pix[0, 0] == (20, 20, 70)
|
||||
|
||||
pix = im.load()
|
||||
|
||||
self.assertEqual(pix[0, 0], (20, 20, 70))
|
||||
def test_close():
|
||||
im = Image.open("Tests/images/hopper.gif")
|
||||
im.close()
|
||||
with pytest.raises(ValueError):
|
||||
im.load()
|
||||
with pytest.raises(ValueError):
|
||||
im.getpixel((0, 0))
|
||||
|
||||
def test_close(self):
|
||||
im = Image.open("Tests/images/hopper.gif")
|
||||
im.close()
|
||||
self.assertRaises(ValueError, im.load)
|
||||
self.assertRaises(ValueError, im.getpixel, (0, 0))
|
||||
|
||||
def test_contextmanager(self):
|
||||
fn = None
|
||||
with Image.open("Tests/images/hopper.gif") as im:
|
||||
fn = im.fp.fileno()
|
||||
os.fstat(fn)
|
||||
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):
|
||||
with open("Tests/images/hopper.gif", "rb") as fp:
|
||||
with Image.open(fp):
|
||||
pass
|
||||
|
||||
self.assertFalse(fp.closed)
|
||||
def test_contextmanager_non_exclusive_fp():
|
||||
with open("Tests/images/hopper.gif", "rb") as fp:
|
||||
with Image.open(fp):
|
||||
pass
|
||||
|
||||
assert not fp.closed
|
||||
|
|
|
@ -1,72 +1,72 @@
|
|||
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
|
||||
with hopper() as im:
|
||||
im.mode
|
||||
|
||||
from PIL import ImageMode
|
||||
from PIL import ImageMode
|
||||
|
||||
ImageMode.getmode("1")
|
||||
ImageMode.getmode("L")
|
||||
ImageMode.getmode("P")
|
||||
ImageMode.getmode("RGB")
|
||||
ImageMode.getmode("I")
|
||||
ImageMode.getmode("F")
|
||||
ImageMode.getmode("1")
|
||||
ImageMode.getmode("L")
|
||||
ImageMode.getmode("P")
|
||||
ImageMode.getmode("RGB")
|
||||
ImageMode.getmode("I")
|
||||
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")
|
||||
m = ImageMode.getmode("1")
|
||||
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",
|
||||
"I;16S",
|
||||
"I;16L",
|
||||
"I;16LS",
|
||||
"I;16B",
|
||||
"I;16BS",
|
||||
"I;16N",
|
||||
"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")
|
||||
for mode in (
|
||||
"I;16",
|
||||
"I;16S",
|
||||
"I;16L",
|
||||
"I;16LS",
|
||||
"I;16B",
|
||||
"I;16BS",
|
||||
"I;16N",
|
||||
"I;16NS",
|
||||
):
|
||||
m = ImageMode.getmode(mode)
|
||||
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")
|
||||
m = ImageMode.getmode("RGB")
|
||||
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 check(mode, *result):
|
||||
signature = (
|
||||
Image.getmodebase(mode),
|
||||
Image.getmodetype(mode),
|
||||
Image.getmodebands(mode),
|
||||
Image.getmodebandnames(mode),
|
||||
)
|
||||
self.assertEqual(signature, result)
|
||||
|
||||
check("1", "L", "L", 1, ("1",))
|
||||
check("L", "L", "L", 1, ("L",))
|
||||
check("P", "P", "L", 1, ("P",))
|
||||
check("I", "L", "I", 1, ("I",))
|
||||
check("F", "L", "F", 1, ("F",))
|
||||
check("RGB", "RGB", "L", 3, ("R", "G", "B"))
|
||||
check("RGBA", "RGB", "L", 4, ("R", "G", "B", "A"))
|
||||
check("RGBX", "RGB", "L", 4, ("R", "G", "B", "X"))
|
||||
check("RGBX", "RGB", "L", 4, ("R", "G", "B", "X"))
|
||||
check("CMYK", "RGB", "L", 4, ("C", "M", "Y", "K"))
|
||||
check("YCbCr", "RGB", "L", 3, ("Y", "Cb", "Cr"))
|
||||
def test_properties():
|
||||
def check(mode, *result):
|
||||
signature = (
|
||||
Image.getmodebase(mode),
|
||||
Image.getmodetype(mode),
|
||||
Image.getmodebands(mode),
|
||||
Image.getmodebandnames(mode),
|
||||
)
|
||||
assert signature == result
|
||||
|
||||
check("1", "L", "L", 1, ("1",))
|
||||
check("L", "L", "L", 1, ("L",))
|
||||
check("P", "P", "L", 1, ("P",))
|
||||
check("I", "L", "I", 1, ("I",))
|
||||
check("F", "L", "F", 1, ("F",))
|
||||
check("RGB", "RGB", "L", 3, ("R", "G", "B"))
|
||||
check("RGBA", "RGB", "L", 4, ("R", "G", "B", "A"))
|
||||
check("RGBX", "RGB", "L", 4, ("R", "G", "B", "X"))
|
||||
check("RGBX", "RGB", "L", 4, ("R", "G", "B", "X"))
|
||||
check("CMYK", "RGB", "L", 4, ("C", "M", "Y", "K"))
|
||||
check("YCbCr", "RGB", "L", 3, ("Y", "Cb", "Cr"))
|
||||
|
|
|
@ -1,52 +1,48 @@
|
|||
from PIL import Image
|
||||
|
||||
from .helper import PillowTestCase
|
||||
|
||||
def test_interface():
|
||||
im = Image.new("RGBA", (1, 1), (1, 2, 3, 0))
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3, 0)
|
||||
|
||||
im = Image.new("RGBA", (1, 1), (1, 2, 3))
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3, 255)
|
||||
|
||||
im.putalpha(Image.new("L", im.size, 4))
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3, 4)
|
||||
|
||||
im.putalpha(5)
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3, 5)
|
||||
|
||||
|
||||
class TestImagePutAlpha(PillowTestCase):
|
||||
def test_interface(self):
|
||||
def test_promote():
|
||||
im = Image.new("L", (1, 1), 1)
|
||||
assert im.getpixel((0, 0)) == 1
|
||||
|
||||
im = Image.new("RGBA", (1, 1), (1, 2, 3, 0))
|
||||
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 0))
|
||||
im.putalpha(2)
|
||||
assert im.mode == "LA"
|
||||
assert im.getpixel((0, 0)) == (1, 2)
|
||||
|
||||
im = Image.new("RGBA", (1, 1), (1, 2, 3))
|
||||
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 255))
|
||||
im = Image.new("P", (1, 1), 1)
|
||||
assert im.getpixel((0, 0)) == 1
|
||||
|
||||
im.putalpha(Image.new("L", im.size, 4))
|
||||
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 4))
|
||||
im.putalpha(2)
|
||||
assert im.mode == "PA"
|
||||
assert im.getpixel((0, 0)) == (1, 2)
|
||||
|
||||
im.putalpha(5)
|
||||
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 5))
|
||||
im = Image.new("RGB", (1, 1), (1, 2, 3))
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3)
|
||||
|
||||
def test_promote(self):
|
||||
im.putalpha(4)
|
||||
assert im.mode == "RGBA"
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3, 4)
|
||||
|
||||
im = Image.new("L", (1, 1), 1)
|
||||
self.assertEqual(im.getpixel((0, 0)), 1)
|
||||
|
||||
im.putalpha(2)
|
||||
self.assertEqual(im.mode, "LA")
|
||||
self.assertEqual(im.getpixel((0, 0)), (1, 2))
|
||||
def test_readonly():
|
||||
im = Image.new("RGB", (1, 1), (1, 2, 3))
|
||||
im.readonly = 1
|
||||
|
||||
im = Image.new("P", (1, 1), 1)
|
||||
self.assertEqual(im.getpixel((0, 0)), 1)
|
||||
|
||||
im.putalpha(2)
|
||||
self.assertEqual(im.mode, "PA")
|
||||
self.assertEqual(im.getpixel((0, 0)), (1, 2))
|
||||
|
||||
im = Image.new("RGB", (1, 1), (1, 2, 3))
|
||||
self.assertEqual(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))
|
||||
|
||||
def test_readonly(self):
|
||||
|
||||
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))
|
||||
im.putalpha(4)
|
||||
assert not im.readonly
|
||||
assert im.mode == "RGBA"
|
||||
assert im.getpixel((0, 0)) == (1, 2, 3, 4)
|
||||
|
|
|
@ -1,184 +1,192 @@
|
|||
import pytest
|
||||
from PIL import Image, ImageColor
|
||||
|
||||
from .helper import PillowTestCase
|
||||
|
||||
def test_hash():
|
||||
# short 3 components
|
||||
assert (255, 0, 0) == ImageColor.getrgb("#f00")
|
||||
assert (0, 255, 0) == ImageColor.getrgb("#0f0")
|
||||
assert (0, 0, 255) == ImageColor.getrgb("#00f")
|
||||
|
||||
# short 4 components
|
||||
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
|
||||
assert (222, 0, 0) == ImageColor.getrgb("#de0000")
|
||||
assert (0, 222, 0) == ImageColor.getrgb("#00de00")
|
||||
assert (0, 0, 222) == ImageColor.getrgb("#0000de")
|
||||
|
||||
# long 4 components
|
||||
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
|
||||
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
|
||||
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
|
||||
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 ")
|
||||
|
||||
|
||||
class TestImageColor(PillowTestCase):
|
||||
def test_hash(self):
|
||||
# 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"))
|
||||
def test_colormap():
|
||||
assert (0, 0, 0) == ImageColor.getrgb("black")
|
||||
assert (255, 255, 255) == ImageColor.getrgb("white")
|
||||
assert (255, 255, 255) == ImageColor.getrgb("WHITE")
|
||||
|
||||
# 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"))
|
||||
with pytest.raises(ValueError):
|
||||
ImageColor.getrgb("black ")
|
||||
|
||||
# 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"))
|
||||
|
||||
# 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"))
|
||||
def test_functions():
|
||||
# rgb numbers
|
||||
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)")
|
||||
|
||||
# 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"))
|
||||
# percents
|
||||
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%)")
|
||||
|
||||
# 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")
|
||||
# rgba numbers
|
||||
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)")
|
||||
|
||||
# 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 ")
|
||||
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%)")
|
||||
|
||||
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"))
|
||||
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%)")
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "black ")
|
||||
# alternate format
|
||||
assert ImageColor.getrgb("hsb(0,100%,50%)") == ImageColor.getrgb("hsv(0,100%,50%)")
|
||||
|
||||
def test_functions(self):
|
||||
# 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)"))
|
||||
# floats
|
||||
assert (254, 3, 3) == ImageColor.getrgb("hsl(0.1,99.2%,50.3%)")
|
||||
assert (255, 0, 0) == ImageColor.getrgb("hsl(360.,100.0%,50%)")
|
||||
|
||||
# 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 (253, 2, 2) == ImageColor.getrgb("hsv(0.1,99.2%,99.3%)")
|
||||
assert (255, 0, 0) == ImageColor.getrgb("hsv(360.,100.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)"))
|
||||
# case insensitivity
|
||||
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%)")
|
||||
|
||||
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%)"))
|
||||
# space agnosticism
|
||||
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% )")
|
||||
|
||||
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%)"))
|
||||
# wrong number of components
|
||||
with pytest.raises(ValueError):
|
||||
ImageColor.getrgb("rgb(255,0)")
|
||||
with pytest.raises(ValueError):
|
||||
ImageColor.getrgb("rgb(255,0,0,0)")
|
||||
|
||||
# alternate format
|
||||
self.assertEqual(
|
||||
ImageColor.getrgb("hsb(0,100%,50%)"), ImageColor.getrgb("hsv(0,100%,50%)")
|
||||
)
|
||||
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%)")
|
||||
|
||||
# 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%)"))
|
||||
with pytest.raises(ValueError):
|
||||
ImageColor.getrgb("rgba(255,0,0)")
|
||||
with pytest.raises(ValueError):
|
||||
ImageColor.getrgb("rgba(255,0,0,0,0)")
|
||||
|
||||
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%)"))
|
||||
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)")
|
||||
|
||||
# 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%)")
|
||||
)
|
||||
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)")
|
||||
|
||||
# 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% )"))
|
||||
|
||||
# wrong number of components
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0,0,0)")
|
||||
# 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")
|
||||
assert expected == actual
|
||||
|
||||
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%)")
|
||||
assert (0, 255, 115) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")
|
||||
Image.new("RGB", (1, 1), "white")
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgba(255,0,0)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgba(255,0,0,0,0)")
|
||||
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.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)")
|
||||
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.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)")
|
||||
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
|
||||
assert 163 == ImageColor.getcolor("rgba(0, 255, 115, 33)", "1")
|
||||
# Correct behavior
|
||||
# assert
|
||||
# 255, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
|
||||
Image.new("1", (1, 1), "white")
|
||||
|
||||
# look for rounding errors (based on code by Tim Hatch)
|
||||
def test_rounding_errors(self):
|
||||
|
||||
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)
|
||||
|
||||
self.assertEqual(
|
||||
(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")
|
||||
)
|
||||
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"))
|
||||
Image.new("L", (1, 1), "white")
|
||||
|
||||
self.assertEqual(0, ImageColor.getcolor("black", "1"))
|
||||
self.assertEqual(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"))
|
||||
# Correct behavior
|
||||
# self.assertEqual(
|
||||
# 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"))
|
||||
Image.new("LA", (1, 1), "white")
|
||||
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")
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,57 +1,61 @@
|
|||
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):
|
||||
dir(Image)
|
||||
dir(ImageShow)
|
||||
def test_sanity():
|
||||
dir(Image)
|
||||
dir(ImageShow)
|
||||
|
||||
def test_register(self):
|
||||
# Test registering a viewer that is not a class
|
||||
ImageShow.register("not a class")
|
||||
|
||||
# Restore original state
|
||||
ImageShow._viewers.pop()
|
||||
def test_register():
|
||||
# Test registering a viewer that is not a class
|
||||
ImageShow.register("not a class")
|
||||
|
||||
def test_viewer_show(self):
|
||||
class TestViewer(ImageShow.Viewer):
|
||||
methodCalled = False
|
||||
# Restore original state
|
||||
ImageShow._viewers.pop()
|
||||
|
||||
def show_image(self, image, **options):
|
||||
self.methodCalled = True
|
||||
return True
|
||||
|
||||
viewer = TestViewer()
|
||||
ImageShow.register(viewer, -1)
|
||||
def test_viewer_show():
|
||||
class TestViewer(ImageShow.Viewer):
|
||||
methodCalled = False
|
||||
|
||||
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
|
||||
with hopper() as im:
|
||||
self.assertTrue(ImageShow.show(im))
|
||||
self.assertTrue(viewer.methodCalled)
|
||||
def show_image(self, image, **options):
|
||||
self.methodCalled = True
|
||||
return True
|
||||
|
||||
# Restore original state
|
||||
ImageShow._viewers.pop(0)
|
||||
viewer = TestViewer()
|
||||
ImageShow.register(viewer, -1)
|
||||
|
||||
@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):
|
||||
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
|
||||
im = hopper(mode)
|
||||
self.assertTrue(ImageShow.show(im))
|
||||
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
|
||||
with hopper() as im:
|
||||
assert ImageShow.show(im)
|
||||
assert viewer.methodCalled
|
||||
|
||||
def test_viewer(self):
|
||||
viewer = ImageShow.Viewer()
|
||||
# Restore original state
|
||||
ImageShow._viewers.pop(0)
|
||||
|
||||
self.assertIsNone(viewer.get_format(None))
|
||||
|
||||
self.assertRaises(NotImplementedError, viewer.get_command, None)
|
||||
@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)
|
||||
assert ImageShow.show(im)
|
||||
|
||||
def test_viewers(self):
|
||||
for viewer in ImageShow._viewers:
|
||||
viewer.get_command("test.jpg")
|
||||
|
||||
def test_viewer():
|
||||
viewer = ImageShow.Viewer()
|
||||
|
||||
assert viewer.get_format(None) is None
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
viewer.get_command(None)
|
||||
|
||||
|
||||
def test_viewers():
|
||||
for viewer in ImageShow._viewers:
|
||||
viewer.get_command("test.jpg")
|
||||
|
|
|
@ -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,34 +81,33 @@ if is_win32():
|
|||
memcpy(bp + bf.bfOffBits, pixels, bi.biSizeImage)
|
||||
return bytearray(buf)
|
||||
|
||||
class TestImageWinPointers(PillowTestCase):
|
||||
def test_pointer(self):
|
||||
im = hopper()
|
||||
(width, height) = im.size
|
||||
opath = self.tempfile("temp.png")
|
||||
imdib = ImageWin.Dib(im)
|
||||
def test_pointer(tmp_path):
|
||||
im = hopper()
|
||||
(width, height) = im.size
|
||||
opath = str(tmp_path / "temp.png")
|
||||
imdib = ImageWin.Dib(im)
|
||||
|
||||
hdr = BITMAPINFOHEADER()
|
||||
hdr.biSize = ctypes.sizeof(hdr)
|
||||
hdr.biWidth = width
|
||||
hdr.biHeight = height
|
||||
hdr.biPlanes = 1
|
||||
hdr.biBitCount = 32
|
||||
hdr.biCompression = BI_RGB
|
||||
hdr.biSizeImage = width * height * 4
|
||||
hdr.biClrUsed = 0
|
||||
hdr.biClrImportant = 0
|
||||
hdr = BITMAPINFOHEADER()
|
||||
hdr.biSize = ctypes.sizeof(hdr)
|
||||
hdr.biWidth = width
|
||||
hdr.biHeight = height
|
||||
hdr.biPlanes = 1
|
||||
hdr.biBitCount = 32
|
||||
hdr.biCompression = BI_RGB
|
||||
hdr.biSizeImage = width * height * 4
|
||||
hdr.biClrUsed = 0
|
||||
hdr.biClrImportant = 0
|
||||
|
||||
hdc = CreateCompatibleDC(None)
|
||||
pixels = ctypes.c_void_p()
|
||||
dib = CreateDIBSection(
|
||||
hdc, ctypes.byref(hdr), DIB_RGB_COLORS, ctypes.byref(pixels), None, 0
|
||||
)
|
||||
SelectObject(hdc, dib)
|
||||
hdc = CreateCompatibleDC(None)
|
||||
pixels = ctypes.c_void_p()
|
||||
dib = CreateDIBSection(
|
||||
hdc, ctypes.byref(hdr), DIB_RGB_COLORS, ctypes.byref(pixels), None, 0
|
||||
)
|
||||
SelectObject(hdc, dib)
|
||||
|
||||
imdib.expose(hdc)
|
||||
bitmap = serialize_dib(hdr, pixels)
|
||||
DeleteObject(dib)
|
||||
DeleteDC(hdc)
|
||||
imdib.expose(hdc)
|
||||
bitmap = serialize_dib(hdr, pixels)
|
||||
DeleteObject(dib)
|
||||
DeleteDC(hdc)
|
||||
|
||||
Image.open(BytesIO(bitmap)).save(opath)
|
||||
Image.open(BytesIO(bitmap)).save(opath)
|
||||
|
|
|
@ -1,38 +1,32 @@
|
|||
import unittest
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from .helper import PillowTestCase
|
||||
|
||||
def test_setmode():
|
||||
|
||||
class TestLibImage(PillowTestCase):
|
||||
def test_setmode(self):
|
||||
im = Image.new("L", (1, 1), 255)
|
||||
im.im.setmode("1")
|
||||
assert im.im.getpixel((0, 0)) == 255
|
||||
im.im.setmode("L")
|
||||
assert im.im.getpixel((0, 0)) == 255
|
||||
|
||||
im = Image.new("L", (1, 1), 255)
|
||||
im.im.setmode("1")
|
||||
self.assertEqual(im.im.getpixel((0, 0)), 255)
|
||||
im = Image.new("1", (1, 1), 1)
|
||||
im.im.setmode("L")
|
||||
assert im.im.getpixel((0, 0)) == 255
|
||||
im.im.setmode("1")
|
||||
assert im.im.getpixel((0, 0)) == 255
|
||||
|
||||
im = Image.new("RGB", (1, 1), (1, 2, 3))
|
||||
im.im.setmode("RGB")
|
||||
assert im.im.getpixel((0, 0)) == (1, 2, 3)
|
||||
im.im.setmode("RGBA")
|
||||
assert im.im.getpixel((0, 0)) == (1, 2, 3, 255)
|
||||
im.im.setmode("RGBX")
|
||||
assert im.im.getpixel((0, 0)) == (1, 2, 3, 255)
|
||||
im.im.setmode("RGB")
|
||||
assert im.im.getpixel((0, 0)) == (1, 2, 3)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
im.im.setmode("L")
|
||||
self.assertEqual(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)
|
||||
im.im.setmode("1")
|
||||
self.assertEqual(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))
|
||||
im.im.setmode("RGBA")
|
||||
self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3, 255))
|
||||
im.im.setmode("RGBX")
|
||||
self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3, 255))
|
||||
im.im.setmode("RGB")
|
||||
self.assertEqual(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("RGBABCDE")
|
||||
|
|
|
@ -4,57 +4,55 @@ from io import StringIO
|
|||
|
||||
from PIL import Image, PSDraw
|
||||
|
||||
from .helper import PillowTestCase
|
||||
|
||||
def _create_document(ps):
|
||||
title = "hopper"
|
||||
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
|
||||
|
||||
ps.begin_document(title)
|
||||
|
||||
# draw diagonal lines in a cross
|
||||
ps.line((1 * 72, 2 * 72), (7 * 72, 10 * 72))
|
||||
ps.line((7 * 72, 2 * 72), (1 * 72, 10 * 72))
|
||||
|
||||
# draw the image (75 dpi)
|
||||
with Image.open("Tests/images/hopper.ppm") as im:
|
||||
ps.image(box, im, 75)
|
||||
ps.rectangle(box)
|
||||
|
||||
# draw title
|
||||
ps.setfont("Courier", 36)
|
||||
ps.text((3 * 72, 4 * 72), title)
|
||||
|
||||
ps.end_document()
|
||||
|
||||
|
||||
class TestPsDraw(PillowTestCase):
|
||||
def _create_document(self, ps):
|
||||
title = "hopper"
|
||||
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
|
||||
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
|
||||
|
||||
ps.begin_document(title)
|
||||
# Arrange
|
||||
tempfile = str(tmp_path / "temp.ps")
|
||||
with open(tempfile, "wb") as fp:
|
||||
# Act
|
||||
ps = PSDraw.PSDraw(fp)
|
||||
_create_document(ps)
|
||||
|
||||
# draw diagonal lines in a cross
|
||||
ps.line((1 * 72, 2 * 72), (7 * 72, 10 * 72))
|
||||
ps.line((7 * 72, 2 * 72), (1 * 72, 10 * 72))
|
||||
# Assert
|
||||
# Check non-zero file was created
|
||||
assert os.path.isfile(tempfile)
|
||||
assert os.path.getsize(tempfile) > 0
|
||||
|
||||
# draw the image (75 dpi)
|
||||
with Image.open("Tests/images/hopper.ppm") as im:
|
||||
ps.image(box, im, 75)
|
||||
ps.rectangle(box)
|
||||
|
||||
# draw title
|
||||
ps.setfont("Courier", 36)
|
||||
ps.text((3 * 72, 4 * 72), title)
|
||||
def test_stdout():
|
||||
# Temporarily redirect stdout
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
|
||||
ps.end_document()
|
||||
ps = PSDraw.PSDraw()
|
||||
_create_document(ps)
|
||||
|
||||
def test_draw_postscript(self):
|
||||
# Reset stdout
|
||||
sys.stdout = old_stdout
|
||||
|
||||
# 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")
|
||||
with open(tempfile, "wb") as fp:
|
||||
# Act
|
||||
ps = PSDraw.PSDraw(fp)
|
||||
self._create_document(ps)
|
||||
|
||||
# Assert
|
||||
# Check non-zero file was created
|
||||
self.assertTrue(os.path.isfile(tempfile))
|
||||
self.assertGreater(os.path.getsize(tempfile), 0)
|
||||
|
||||
def test_stdout(self):
|
||||
# Temporarily redirect stdout
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
|
||||
ps = PSDraw.PSDraw()
|
||||
self._create_document(ps)
|
||||
|
||||
# Reset stdout
|
||||
sys.stdout = old_stdout
|
||||
|
||||
self.assertNotEqual(mystdout.getvalue(), "")
|
||||
assert mystdout.getvalue() != ""
|
||||
|
|
|
@ -1,32 +1,28 @@
|
|||
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):
|
||||
# Arrange
|
||||
data = pyroma.projectdata.get_data(".")
|
||||
@pytest.mark.skipif(pyroma is None, reason="Pyroma is not installed")
|
||||
def test_pyroma():
|
||||
# Arrange
|
||||
data = pyroma.projectdata.get_data(".")
|
||||
|
||||
# Act
|
||||
rating = pyroma.ratings.rate(data)
|
||||
# Act
|
||||
rating = pyroma.ratings.rate(data)
|
||||
|
||||
# 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
|
||||
if "rc" in __version__:
|
||||
# Pyroma needs to chill about RC versions and not kill all our tests.
|
||||
assert rating == (
|
||||
9,
|
||||
["The package's version number does not comply with PEP-386."],
|
||||
)
|
||||
|
||||
else:
|
||||
# Should have a perfect score
|
||||
self.assertEqual(rating, (10, []))
|
||||
else:
|
||||
# Should have a perfect score
|
||||
assert rating == (10, [])
|
||||
|
|
Loading…
Reference in New Issue
Block a user