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))
class TestTTypeFontLeak(PillowLeakTestCase): # fails at iteration 3 in master iterations = 10 mem_limit = 4096 # k def _test_font(self, font): im = Image.new('RGB', (255, 255), 'white') draw = ImageDraw.ImageDraw(im) self._test_leak(lambda: draw.text((0, 0), "some text "*1024, # ~10k font=font, fill="black")) @unittest.skipIf(not features.check('freetype2'), "Test requires freetype2") def test_leak(self): ttype = ImageFont.truetype('Tests/fonts/FreeMono.ttf', 20) self._test_font(ttype)
from helper import unittest, PillowTestCase, hopper from PIL2 import Image, ImagePalette, features try: from PIL2 import MicImagePlugin except ImportError: olefile_installed = False else: olefile_installed = True TEST_FILE = "Tests/images/hopper.mic" @unittest.skipUnless(olefile_installed, "olefile package not installed") @unittest.skipUnless(features.check('libtiff'), "libtiff not installed") class TestFileMic(PillowTestCase): def test_sanity(self): im = Image.open(TEST_FILE) im.load() self.assertEqual(im.mode, "RGBA") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "MIC") # Adjust for the gamma of 2.2 encoded into the file lut = ImagePalette.make_gamma_lut(1 / 2.2) im = Image.merge('RGBA', [chan.point(lut) for chan in im.split()]) im2 = hopper("RGBA") self.assert_image_similar(im, im2, 10)
# -*- coding: utf-8 -*- from helper import unittest, PillowTestCase from PIL2 import Image, ImageDraw, ImageFont, features FONT_SIZE = 20 FONT_PATH = "Tests/fonts/DejaVuSans.ttf" @unittest.skipUnless(features.check('raqm'), "Raqm Library is not installed.") class TestImagecomplextext(PillowTestCase): def test_english(self): # smoke test, this should not fail ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) im = Image.new(mode='RGB', size=(300, 100)) draw = ImageDraw.Draw(im) draw.text((0, 0), 'TEST', font=ttf, fill=500, direction='ltr') def test_complex_text(self): ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) im = Image.new(mode='RGB', size=(300, 100)) draw = ImageDraw.Draw(im) draw.text((0, 0), 'اهلا عمان', font=ttf, fill=500) target = 'Tests/images/test_text.png' target_img = Image.open(target) self.assert_image_similar(im, target_img, .5) def test_y_offset(self): ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf",
def setUp(self): if not features.check('libtiff'): self.skipTest("tiff support not available")
def check_webp_anim(self): self.assertEqual(features.check('webp_anim'), _webp.HAVE_WEBPANIM)
def check_webp_mux(self): self.assertEqual(features.check('webp_mux'), _webp.HAVE_WEBPMUX)
def check_webp_transparency(self): self.assertEqual(features.check('transp_webp'), not _webp.WebPDecoderBuggyAlpha()) self.assertEqual(features.check('transp_webp'), _webp.HAVE_TRANSPARENCY)
print("-" * 68) print("Pillow2", Image.__version__, "TEST SUMMARY ") print("-" * 68) print("Python modules loaded from", os.path.dirname(Image.__file__)) print("Binary modules loaded from", os.path.dirname(Image.core.__file__)) print("-" * 68) for name, feature in [("pil", "PIL2 CORE"), ("tkinter", "TKINTER"), ("freetype2", "FREETYPE2"), ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"), ("transp_webp", "WEBP Transparency"), ("webp_mux", "WEBPMUX"), ("webp_anim", "WEBP Animation"), ("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"), ("zlib", "ZLIB (PNG/ZIP)"), ("libtiff", "LIBTIFF"), ("raqm", "RAQM (Bidirectional Text)")]: if features.check(name): print("---", feature, "support ok") else: print("***", feature, "support not installed") print("-" * 68) # use doctest to make sure the test program behaves as documented! import doctest print("Running selftest:") status = doctest.testmod(sys.modules[__name__]) if status[0]: print("*** %s tests of %d failed." % status) exit_status = 1 else: print("--- %s tests passed." % status[1])
# -*- coding: utf-8 -*- from helper import unittest, PillowTestCase from PIL2 import Image, ImageDraw, ImageFont, features from io import BytesIO import os import sys import copy FONT_PATH = "Tests/fonts/FreeMono.ttf" FONT_SIZE = 20 TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward" HAS_FREETYPE = features.check('freetype2') HAS_RAQM = features.check('raqm') class SimplePatcher(object): def __init__(self, parent_obj, attr_name, value): self._parent_obj = parent_obj self._attr_name = attr_name self._saved = None self._is_saved = False self._value = value def __enter__(self): # Patch the attr on the object if hasattr(self._parent_obj, self._attr_name): self._saved = getattr(self._parent_obj, self._attr_name) setattr(self._parent_obj, self._attr_name, self._value)
from helper import unittest, PillowLeakTestCase from PIL2 import Image, features from io import BytesIO test_file = "Tests/images/hopper.webp" @unittest.skipUnless(features.check('webp'), "WebP is not installed") class TestWebPLeaks(PillowLeakTestCase): mem_limit = 3 * 1024 # kb iterations = 100 def test_leak_load(self): with open(test_file, 'rb') as f: im_data = f.read() def core(): with Image.open(BytesIO(im_data)) as im: im.load() self._test_leak(core) if __name__ == '__main__': unittest.main()
X0 = int(W / 4) X1 = int(X0 * 3) Y0 = int(H / 4) Y1 = int(X0 * 3) # Two kinds of bounding box BBOX1 = [(X0, Y0), (X1, Y1)] BBOX2 = [X0, Y0, X1, Y1] # Two kinds of coordinate sequences POINTS1 = [(10, 10), (20, 40), (30, 30)] POINTS2 = [10, 10, 20, 40, 30, 30] KITE_POINTS = [(10, 50), (70, 10), (90, 50), (70, 90), (10, 50)] HAS_FREETYPE = features.check("freetype2") FONT_PATH = "Tests/fonts/FreeMono.ttf" class TestImageDraw(PillowTestCase): def test_sanity(self): im = hopper("RGB").copy() draw = ImageDraw2.Draw(im) pen = ImageDraw2.Pen("blue", width=7) draw.line(list(range(10)), pen) from PIL2 import ImageDraw draw, handler = ImageDraw.getdraw(im)