From cd967680cb1314ecb45cc10ff246b5c6756eb93f Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 3 Jun 2014 18:04:27 +0300 Subject: [PATCH 01/33] Move dummy test to test/ and run with nosetests --- .travis.yml | 4 +++- {PIL => test}/tests.py | 0 2 files changed, 3 insertions(+), 1 deletion(-) rename {PIL => test}/tests.py (100%) diff --git a/.travis.yml b/.travis.yml index 36dae5b7a..17a0b0363 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ python: install: - "sudo apt-get -qq install libfreetype6-dev liblcms2-dev python-qt4 ghostscript libffi-dev cmake" - "pip install cffi" - - "pip install coveralls" + - "pip install coveralls nose" # webp - pushd depends && ./install_webp.sh && popd @@ -35,6 +35,8 @@ script: - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* selftest.py; fi - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then python Tests/run.py --coverage; fi + - nosetests test/ + after_success: - coverage report - coveralls diff --git a/PIL/tests.py b/test/tests.py similarity index 100% rename from PIL/tests.py rename to test/tests.py From 5c8c777ea4ac1a47efa32e1ee96a13b531351676 Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 3 Jun 2014 22:38:56 +0300 Subject: [PATCH 02/33] Start moving tests over to use unittest --- .travis.yml | 3 +- Tests/test_image_frombytes.py | 10 - Tests/test_image_getim.py | 14 -- Tests/test_image_tobytes.py | 7 - test/test_image_frombytes.py | 18 ++ test/test_image_getim.py | 19 ++ test/test_image_tobytes.py | 13 ++ test/tester.py | 393 ++++++++++++++++++++++++++++++++++ test/tests.py | 8 +- 9 files changed, 450 insertions(+), 35 deletions(-) delete mode 100644 Tests/test_image_frombytes.py delete mode 100644 Tests/test_image_getim.py delete mode 100644 Tests/test_image_tobytes.py create mode 100644 test/test_image_frombytes.py create mode 100644 test/test_image_getim.py create mode 100644 test/test_image_tobytes.py create mode 100644 test/tester.py diff --git a/.travis.yml b/.travis.yml index 17a0b0363..1b864eecf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,12 +30,13 @@ script: # Don't cover PyPy: it fails intermittently and is x5.8 slower (#640) - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python selftest.py; fi - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python Tests/run.py; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then nosetests test/; fi # Cover the others - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* selftest.py; fi - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then python Tests/run.py --coverage; fi + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* -m nose test/; fi - - nosetests test/ after_success: - coverage report diff --git a/Tests/test_image_frombytes.py b/Tests/test_image_frombytes.py deleted file mode 100644 index aa157aa6a..000000000 --- a/Tests/test_image_frombytes.py +++ /dev/null @@ -1,10 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - im1 = lena() - im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes()) - - assert_image_equal(im1, im2) - diff --git a/Tests/test_image_getim.py b/Tests/test_image_getim.py deleted file mode 100644 index 8d2f12fc2..000000000 --- a/Tests/test_image_getim.py +++ /dev/null @@ -1,14 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - - im = lena() - type_repr = repr(type(im.getim())) - - if py3: - assert_true("PyCapsule" in type_repr) - - assert_true(isinstance(im.im.id, int)) - diff --git a/Tests/test_image_tobytes.py b/Tests/test_image_tobytes.py deleted file mode 100644 index d42399993..000000000 --- a/Tests/test_image_tobytes.py +++ /dev/null @@ -1,7 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - data = lena().tobytes() - assert_true(isinstance(data, bytes)) diff --git a/test/test_image_frombytes.py b/test/test_image_frombytes.py new file mode 100644 index 000000000..ee68b6722 --- /dev/null +++ b/test/test_image_frombytes.py @@ -0,0 +1,18 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImageFromBytes(PillowTestCase): + + def test_sanity(self): + im1 = lena() + im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes()) + + self.assert_image_equal(im1, im2) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_getim.py b/test/test_image_getim.py new file mode 100644 index 000000000..02744a529 --- /dev/null +++ b/test/test_image_getim.py @@ -0,0 +1,19 @@ +from tester import unittest, PillowTestCase, lena, py3 + + +class TestImageGetIm(PillowTestCase): + + def test_sanity(self): + im = lena() + type_repr = repr(type(im.getim())) + + if py3: + self.assertIn("PyCapsule", type_repr) + + self.assertIsInstance(im.im.id, int) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_tobytes.py b/test/test_image_tobytes.py new file mode 100644 index 000000000..ee7b4c9e6 --- /dev/null +++ b/test/test_image_tobytes.py @@ -0,0 +1,13 @@ +from tester import unittest, lena + + +class TestImageToBytes(unittest.TestCase): + + def test_sanity(self): + data = lena().tobytes() + self.assertTrue(isinstance(data, bytes)) + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/tester.py b/test/tester.py new file mode 100644 index 000000000..77b38a15a --- /dev/null +++ b/test/tester.py @@ -0,0 +1,393 @@ +""" +Helper functions. +""" +from __future__ import print_function +import sys + +if sys.version_info[:2] <= (2, 6): + import unittest2 as unittest +else: + import unittest + + +class PillowTestCase(unittest.TestCase): + + def assert_image_equal(self, a, b, msg=None): + self.assertEqual( + a.mode, b.mode, + msg or "got mode %r, expected %r" % (a.mode, b.mode)) + self.assertEqual( + a.size, b.size, + msg or "got size %r, expected %r" % (a.size, b.size)) + self.assertEqual( + a.tobytes(), b.tobytes(), + msg or "got different content") + + +# # require that deprecation warnings are triggered +# import warnings +# warnings.simplefilter('default') +# # temporarily turn off resource warnings that warn about unclosed +# # files in the test scripts. +# try: +# warnings.filterwarnings("ignore", category=ResourceWarning) +# except NameError: +# # we expect a NameError on py2.x, since it doesn't have ResourceWarnings. +# pass + +import sys +py3 = (sys.version_info >= (3, 0)) + +# # some test helpers +# +# _target = None +# _tempfiles = [] +# _logfile = None +# +# +# def success(): +# import sys +# success.count += 1 +# if _logfile: +# print(sys.argv[0], success.count, failure.count, file=_logfile) +# return True +# +# +# def failure(msg=None, frame=None): +# import sys +# import linecache +# failure.count += 1 +# if _target: +# if frame is None: +# frame = sys._getframe() +# while frame.f_globals.get("__name__") != _target.__name__: +# frame = frame.f_back +# location = (frame.f_code.co_filename, frame.f_lineno) +# prefix = "%s:%d: " % location +# line = linecache.getline(*location) +# print(prefix + line.strip() + " failed:") +# if msg: +# print("- " + msg) +# if _logfile: +# print(sys.argv[0], success.count, failure.count, file=_logfile) +# return False +# +# success.count = failure.count = 0 +# +# +# # predicates +# +# def assert_almost_equal(a, b, msg=None, eps=1e-6): +# if abs(a-b) < eps: +# success() +# else: +# failure(msg or "got %r, expected %r" % (a, b)) +# +# +# def assert_deep_equal(a, b, msg=None): +# try: +# if len(a) == len(b): +# if all([x == y for x, y in zip(a, b)]): +# success() +# else: +# failure(msg or "got %s, expected %s" % (a, b)) +# else: +# failure(msg or "got length %s, expected %s" % (len(a), len(b))) +# except: +# assert_equal(a, b, msg) +# +# +# def assert_greater(a, b, msg=None): +# if a > b: +# success() +# else: +# failure(msg or "%r unexpectedly not greater than %r" % (a, b)) +# +# +# def assert_greater_equal(a, b, msg=None): +# if a >= b: +# success() +# else: +# failure( +# msg or "%r unexpectedly not greater than or equal to %r" +# % (a, b)) +# +# +# def assert_less(a, b, msg=None): +# if a < b: +# success() +# else: +# failure(msg or "%r unexpectedly not less than %r" % (a, b)) +# +# +# def assert_less_equal(a, b, msg=None): +# if a <= b: +# success() +# else: +# failure( +# msg or "%r unexpectedly not less than or equal to %r" % (a, b)) +# +# +# def assert_is_instance(a, b, msg=None): +# if isinstance(a, b): +# success() +# else: +# failure(msg or "got %r, expected %r" % (type(a), b)) +# +# +# def assert_in(a, b, msg=None): +# if a in b: +# success() +# else: +# failure(msg or "%r unexpectedly not in %r" % (a, b)) +# +# +# def assert_match(v, pattern, msg=None): +# import re +# if re.match(pattern, v): +# success() +# else: +# failure(msg or "got %r, doesn't match pattern %r" % (v, pattern)) +# +# +# def assert_exception(exc_class, func): +# import sys +# import traceback +# try: +# func() +# except exc_class: +# success() +# except: +# failure("expected %r exception, got %r" % ( +# exc_class.__name__, sys.exc_info()[0].__name__)) +# traceback.print_exc() +# else: +# failure( +# "expected %r exception, got no exception" % exc_class.__name__) +# +# +# def assert_no_exception(func): +# import sys +# import traceback +# try: +# func() +# except: +# failure("expected no exception, got %r" % sys.exc_info()[0].__name__) +# traceback.print_exc() +# else: +# success() +# +# +# def assert_warning(warn_class, func): +# # note: this assert calls func three times! +# import warnings +# +# def warn_error(message, category=UserWarning, **options): +# raise category(message) +# +# def warn_ignore(message, category=UserWarning, **options): +# pass +# warn = warnings.warn +# result = None +# try: +# warnings.warn = warn_ignore +# assert_no_exception(func) +# result = func() +# warnings.warn = warn_error +# assert_exception(warn_class, func) +# finally: +# warnings.warn = warn # restore +# return result +# +# # helpers +# +# from io import BytesIO +# +# +# def fromstring(data): +# from PIL import Image +# return Image.open(BytesIO(data)) +# +# +# def tostring(im, format, **options): +# out = BytesIO() +# im.save(out, format, **options) +# return out.getvalue() + + +def lena(mode="RGB", cache={}): + from PIL import Image + im = cache.get(mode) + if im is None: + if mode == "RGB": + im = Image.open("Images/lena.ppm") + elif mode == "F": + im = lena("L").convert(mode) + elif mode[:4] == "I;16": + im = lena("I").convert(mode) + else: + im = lena("RGB").convert(mode) + cache[mode] = im + return im + + +# def assert_image(im, mode, size, msg=None): +# if mode is not None and im.mode != mode: +# failure(msg or "got mode %r, expected %r" % (im.mode, mode)) +# elif size is not None and im.size != size: +# failure(msg or "got size %r, expected %r" % (im.size, size)) +# else: +# success() +# +# +# def assert_image_equal(a, b, msg=None): +# if a.mode != b.mode: +# failure(msg or "got mode %r, expected %r" % (a.mode, b.mode)) +# elif a.size != b.size: +# failure(msg or "got size %r, expected %r" % (a.size, b.size)) +# elif a.tobytes() != b.tobytes(): +# failure(msg or "got different content") +# else: +# success() +# +# +# def assert_image_completely_equal(a, b, msg=None): +# if a != b: +# failure(msg or "images different") +# else: +# success() +# +# +# def assert_image_similar(a, b, epsilon, msg=None): +# epsilon = float(epsilon) +# if a.mode != b.mode: +# return failure(msg or "got mode %r, expected %r" % (a.mode, b.mode)) +# elif a.size != b.size: +# return failure(msg or "got size %r, expected %r" % (a.size, b.size)) +# diff = 0 +# try: +# ord(b'0') +# for abyte, bbyte in zip(a.tobytes(), b.tobytes()): +# diff += abs(ord(abyte)-ord(bbyte)) +# except: +# for abyte, bbyte in zip(a.tobytes(), b.tobytes()): +# diff += abs(abyte-bbyte) +# ave_diff = float(diff)/(a.size[0]*a.size[1]) +# if epsilon < ave_diff: +# return failure( +# msg or "average pixel value difference %.4f > epsilon %.4f" % ( +# ave_diff, epsilon)) +# else: +# return success() +# +# +# def tempfile(template, *extra): +# import os +# import os.path +# import sys +# import tempfile +# files = [] +# root = os.path.join(tempfile.gettempdir(), 'pillow-tests') +# try: +# os.mkdir(root) +# except OSError: +# pass +# for temp in (template,) + extra: +# assert temp[:5] in ("temp.", "temp_") +# name = os.path.basename(sys.argv[0]) +# name = temp[:4] + os.path.splitext(name)[0][4:] +# name = name + "_%d" % len(_tempfiles) + temp[4:] +# name = os.path.join(root, name) +# files.append(name) +# _tempfiles.extend(files) +# return files[0] +# +# +# # test runner +# +# def run(): +# global _target, _tests, run +# import sys +# import traceback +# _target = sys.modules["__main__"] +# run = None # no need to run twice +# tests = [] +# for name, value in list(vars(_target).items()): +# if name[:5] == "test_" and type(value) is type(success): +# tests.append((value.__code__.co_firstlineno, name, value)) +# tests.sort() # sort by line +# for lineno, name, func in tests: +# try: +# _tests = [] +# func() +# for func, args in _tests: +# func(*args) +# except: +# t, v, tb = sys.exc_info() +# tb = tb.tb_next +# if tb: +# failure(frame=tb.tb_frame) +# traceback.print_exception(t, v, tb) +# else: +# print("%s:%d: cannot call test function: %s" % ( +# sys.argv[0], lineno, v)) +# failure.count += 1 +# +# +# def yield_test(function, *args): +# # collect delayed/generated tests +# _tests.append((function, args)) +# +# +# def skip(msg=None): +# import os +# print("skip") +# os._exit(0) # don't run exit handlers +# +# +# def ignore(pattern): +# """Tells the driver to ignore messages matching the pattern, for the +# duration of the current test.""" +# print('ignore: %s' % pattern) +# +# +# def _setup(): +# global _logfile +# +# import sys +# if "--coverage" in sys.argv: +# # Temporary: ignore PendingDeprecationWarning from Coverage (Py3.4) +# with warnings.catch_warnings(): +# warnings.simplefilter("ignore") +# import coverage +# cov = coverage.coverage(auto_data=True, include="PIL/*") +# cov.start() +# +# def report(): +# if run: +# run() +# if success.count and not failure.count: +# print("ok") +# # only clean out tempfiles if test passed +# import os +# import os.path +# import tempfile +# for file in _tempfiles: +# try: +# os.remove(file) +# except OSError: +# pass # report? +# temp_root = os.path.join(tempfile.gettempdir(), 'pillow-tests') +# try: +# os.rmdir(temp_root) +# except OSError: +# pass +# +# import atexit +# atexit.register(report) +# +# if "--log" in sys.argv: +# _logfile = open("test.log", "a") +# +# +# _setup() diff --git a/test/tests.py b/test/tests.py index eb4a8342d..1bd308922 100644 --- a/test/tests.py +++ b/test/tests.py @@ -1,7 +1,7 @@ -import unittest +from tester import unittest -class PillowTests(unittest.TestCase): +class SomeTests(unittest.TestCase): """ Can we start moving the test suite here? """ @@ -10,8 +10,10 @@ class PillowTests(unittest.TestCase): """ Great idea! """ - assert True is True + self.assertTrue(True) if __name__ == '__main__': unittest.main() + +# End of file From 2bde38d8f5a7e4040ddd8d2f5760d927e24a0bd1 Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 3 Jun 2014 23:22:02 +0300 Subject: [PATCH 03/33] Move more tests; add more helpers; install unittest2 for Python 2.7 --- .travis.yml | 1 + Tests/test_imagedraw.py | 265 ---------------------------------------- test/test_imagedraw.py | 253 ++++++++++++++++++++++++++++++++++++++ test/tester.py | 15 +++ 4 files changed, 269 insertions(+), 265 deletions(-) delete mode 100644 Tests/test_imagedraw.py create mode 100644 test/test_imagedraw.py diff --git a/.travis.yml b/.travis.yml index 1b864eecf..68e985b9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ install: - "sudo apt-get -qq install libfreetype6-dev liblcms2-dev python-qt4 ghostscript libffi-dev cmake" - "pip install cffi" - "pip install coveralls nose" + - if [ "$TRAVIS_PYTHON_VERSION" == "2.6" ]; then pip install unittest2; fi # webp - pushd depends && ./install_webp.sh && popd diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py deleted file mode 100644 index c47638a05..000000000 --- a/Tests/test_imagedraw.py +++ /dev/null @@ -1,265 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageColor -from PIL import ImageDraw - -# Image size -w, h = 100, 100 - -# Bounding box points -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] - - -def test_sanity(): - - im = lena("RGB").copy() - - draw = ImageDraw.ImageDraw(im) - draw = ImageDraw.Draw(im) - - draw.ellipse(list(range(4))) - draw.line(list(range(10))) - draw.polygon(list(range(100))) - draw.rectangle(list(range(4))) - - success() - - -def test_deprecated(): - - im = lena().copy() - - draw = ImageDraw.Draw(im) - - assert_warning(DeprecationWarning, lambda: draw.setink(0)) - assert_warning(DeprecationWarning, lambda: draw.setfill(0)) - - -def helper_arc(bbox): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - # FIXME Fill param should be named outline. - draw.arc(bbox, 0, 180) - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_arc.png")) - - -def test_arc1(): - helper_arc(bbox1) - - -def test_arc2(): - helper_arc(bbox2) - - -def test_bitmap(): - # Arrange - small = Image.open("Tests/images/pil123rgba.png").resize((50, 50)) - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.bitmap((10, 10), small) - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_bitmap.png")) - - -def helper_chord(bbox): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.chord(bbox, 0, 180, fill="red", outline="yellow") - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_chord.png")) - - -def test_chord1(): - helper_chord(bbox1) - - -def test_chord2(): - helper_chord(bbox2) - - -def helper_ellipse(bbox): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.ellipse(bbox, fill="green", outline="blue") - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_ellipse.png")) - - -def test_ellipse1(): - helper_ellipse(bbox1) - - -def test_ellipse2(): - helper_ellipse(bbox2) - - -def helper_line(points): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.line(points1, fill="yellow", width=2) - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_line.png")) - - -def test_line1(): - helper_line(points1) - - -def test_line2(): - helper_line(points2) - - -def helper_pieslice(bbox): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.pieslice(bbox, -90, 45, fill="white", outline="blue") - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_pieslice.png")) - - -def test_pieslice1(): - helper_pieslice(bbox1) - - -def test_pieslice2(): - helper_pieslice(bbox2) - - -def helper_point(points): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.point(points1, fill="yellow") - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_point.png")) - - -def test_point1(): - helper_point(points1) - - -def test_point2(): - helper_point(points2) - - -def helper_polygon(points): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.polygon(points1, fill="red", outline="blue") - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_polygon.png")) - - -def test_polygon1(): - helper_polygon(points1) - - -def test_polygon2(): - helper_polygon(points2) - - -def helper_rectangle(bbox): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - - # Act - draw.rectangle(bbox, fill="black", outline="green") - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_rectangle.png")) - - -def test_rectangle1(): - helper_rectangle(bbox1) - - -def test_rectangle2(): - helper_rectangle(bbox2) - - -def test_floodfill(): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - draw.rectangle(bbox2, outline="yellow", fill="green") - centre_point = (int(w/2), int(h/2)) - - # Act - ImageDraw.floodfill(im, centre_point, ImageColor.getrgb("red")) - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_floodfill.png")) - - -def test_floodfill_border(): - # Arrange - im = Image.new("RGB", (w, h)) - draw = ImageDraw.Draw(im) - draw.rectangle(bbox2, outline="yellow", fill="green") - centre_point = (int(w/2), int(h/2)) - - # Act - ImageDraw.floodfill( - im, centre_point, ImageColor.getrgb("red"), - border=ImageColor.getrgb("black")) - del draw - - # Assert - assert_image_equal(im, Image.open("Tests/images/imagedraw_floodfill2.png")) - - -# End of file diff --git a/test/test_imagedraw.py b/test/test_imagedraw.py new file mode 100644 index 000000000..60e9b6f03 --- /dev/null +++ b/test/test_imagedraw.py @@ -0,0 +1,253 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image + +from PIL import Image +from PIL import ImageColor +from PIL import ImageDraw + +# Image size +w, h = 100, 100 + +# Bounding box points +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] + + +class TestImageDraw(PillowTestCase): + + def test_sanity(self): + im = lena("RGB").copy() + + draw = ImageDraw.ImageDraw(im) + draw = ImageDraw.Draw(im) + + draw.ellipse(list(range(4))) + draw.line(list(range(10))) + draw.polygon(list(range(100))) + draw.rectangle(list(range(4))) + + def test_deprecated(self): + im = lena().copy() + + draw = ImageDraw.Draw(im) + + self.assert_warning(DeprecationWarning, lambda: draw.setink(0)) + self.assert_warning(DeprecationWarning, lambda: draw.setfill(0)) + + def helper_arc(self, bbox): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + # FIXME Fill param should be named outline. + draw.arc(bbox, 0, 180) + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_arc.png")) + + def test_arc1(self): + self.helper_arc(bbox1) + + def test_arc2(self): + self.helper_arc(bbox2) + + def test_bitmap(self): + # Arrange + small = Image.open("Tests/images/pil123rgba.png").resize((50, 50)) + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.bitmap((10, 10), small) + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_bitmap.png")) + + def helper_chord(self, bbox): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.chord(bbox, 0, 180, fill="red", outline="yellow") + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_chord.png")) + + def test_chord1(self): + self.helper_chord(bbox1) + + def test_chord2(self): + self.helper_chord(bbox2) + + def helper_ellipse(self, bbox): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.ellipse(bbox, fill="green", outline="blue") + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_ellipse.png")) + + def test_ellipse1(self): + self.helper_ellipse(bbox1) + + def test_ellipse2(self): + self.helper_ellipse(bbox2) + + def helper_line(self, points): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.line(points1, fill="yellow", width=2) + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_line.png")) + + def test_line1(self): + self.helper_line(points1) + + def test_line2(self): + self.helper_line(points2) + + def helper_pieslice(self, bbox): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.pieslice(bbox, -90, 45, fill="white", outline="blue") + del draw + + # Assert + self.assert_image_equal(im, Image.open("Tests/images/imagedraw_pieslice.png")) + + def test_pieslice1(self): + self.helper_pieslice(bbox1) + + def test_pieslice2(self): + self.helper_pieslice(bbox2) + + def helper_point(self, points): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.point(points1, fill="yellow") + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_point.png")) + + def test_point1(self): + self.helper_point(points1) + + + def test_point2(self): + self.helper_point(points2) + + def helper_polygon(self, points): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.polygon(points1, fill="red", outline="blue") + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_polygon.png")) + + def test_polygon1(self): + self.helper_polygon(points1) + + + def test_polygon2(self): + self.helper_polygon(points2) + + + def helper_rectangle(self, bbox): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + + # Act + draw.rectangle(bbox, fill="black", outline="green") + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_rectangle.png")) + + def test_rectangle1(self): + self.helper_rectangle(bbox1) + + def test_rectangle2(self): + self.helper_rectangle(bbox2) + + def test_floodfill(self): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + draw.rectangle(bbox2, outline="yellow", fill="green") + centre_point = (int(w/2), int(h/2)) + + # Act + ImageDraw.floodfill(im, centre_point, ImageColor.getrgb("red")) + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_floodfill.png")) + + def test_floodfill_border(self): + # Arrange + im = Image.new("RGB", (w, h)) + draw = ImageDraw.Draw(im) + draw.rectangle(bbox2, outline="yellow", fill="green") + centre_point = (int(w/2), int(h/2)) + + # Act + ImageDraw.floodfill( + im, centre_point, ImageColor.getrgb("red"), + border=ImageColor.getrgb("black")) + del draw + + # Assert + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_floodfill2.png")) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/tester.py b/test/tester.py index 77b38a15a..894d37883 100644 --- a/test/tester.py +++ b/test/tester.py @@ -23,6 +23,21 @@ class PillowTestCase(unittest.TestCase): a.tobytes(), b.tobytes(), msg or "got different content") + def assert_warning(self, warn_class, func): + import warnings + + with warnings.catch_warnings(record=True) as w: + # Cause all warnings to always be triggered. + warnings.simplefilter("always") + + # Hopefully trigger a warning. + func() + + # Verify some things. + self.assertEqual(len(w), 1) + assert issubclass(w[-1].category, warn_class) + self.assertIn("deprecated", str(w[-1].message)) + # # require that deprecation warnings are triggered # import warnings From d082c58043423e5eb510151ce394c003a92775e1 Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 3 Jun 2014 23:35:21 +0300 Subject: [PATCH 04/33] pep8/pyflakes --- .travis.yml | 2 ++ test/test_imagedraw.py | 8 ++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 68e985b9a..4a2250771 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,5 +45,7 @@ after_success: - pip install pep8 pyflakes - pep8 PIL/*.py - pyflakes PIL/*.py + - pep8 test/*.py + - pyflakes test/*.py - pep8 Tests/*.py - pyflakes Tests/*.py diff --git a/test/test_imagedraw.py b/test/test_imagedraw.py index 60e9b6f03..f9a5e5f6a 100644 --- a/test/test_imagedraw.py +++ b/test/test_imagedraw.py @@ -1,7 +1,5 @@ from tester import unittest, PillowTestCase, lena -from PIL import Image - from PIL import Image from PIL import ImageColor from PIL import ImageDraw @@ -146,7 +144,8 @@ class TestImageDraw(PillowTestCase): del draw # Assert - self.assert_image_equal(im, Image.open("Tests/images/imagedraw_pieslice.png")) + self.assert_image_equal( + im, Image.open("Tests/images/imagedraw_pieslice.png")) def test_pieslice1(self): self.helper_pieslice(bbox1) @@ -170,7 +169,6 @@ class TestImageDraw(PillowTestCase): def test_point1(self): self.helper_point(points1) - def test_point2(self): self.helper_point(points2) @@ -190,11 +188,9 @@ class TestImageDraw(PillowTestCase): def test_polygon1(self): self.helper_polygon(points1) - def test_polygon2(self): self.helper_polygon(points2) - def helper_rectangle(self, bbox): # Arrange im = Image.new("RGB", (w, h)) From 2e52f3ab46d98b7978f3d145c20dc38fbd84d90f Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Jun 2014 00:05:48 +0300 Subject: [PATCH 05/33] Move more tests; remove some redundant helpers --- Tests/test_000_sanity.py | 24 --------- Tests/test_image_histogram.py | 19 ------- Tests/test_imageenhance.py | 19 ------- Tests/test_lib_image.py | 30 ----------- test/test_000_sanity.py | 32 ++++++++++++ test/test_image_histogram.py | 26 ++++++++++ test/test_imageenhance.py | 28 +++++++++++ test/test_lib_image.py | 39 +++++++++++++++ test/tester.py | 94 ----------------------------------- 9 files changed, 125 insertions(+), 186 deletions(-) delete mode 100644 Tests/test_000_sanity.py delete mode 100644 Tests/test_image_histogram.py delete mode 100644 Tests/test_imageenhance.py delete mode 100644 Tests/test_lib_image.py create mode 100644 test/test_000_sanity.py create mode 100644 test/test_image_histogram.py create mode 100644 test/test_imageenhance.py create mode 100644 test/test_lib_image.py diff --git a/Tests/test_000_sanity.py b/Tests/test_000_sanity.py deleted file mode 100644 index a30786458..000000000 --- a/Tests/test_000_sanity.py +++ /dev/null @@ -1,24 +0,0 @@ -from __future__ import print_function -from tester import * - -import PIL -import PIL.Image - -# Make sure we have the binary extension -im = PIL.Image.core.new("L", (100, 100)) - -assert PIL.Image.VERSION[:3] == '1.1' - -# Create an image and do stuff with it. -im = PIL.Image.new("1", (100, 100)) -assert (im.mode, im.size) == ('1', (100, 100)) -assert len(im.tobytes()) == 1300 - -# Create images in all remaining major modes. -im = PIL.Image.new("L", (100, 100)) -im = PIL.Image.new("P", (100, 100)) -im = PIL.Image.new("RGB", (100, 100)) -im = PIL.Image.new("I", (100, 100)) -im = PIL.Image.new("F", (100, 100)) - -print("ok") diff --git a/Tests/test_image_histogram.py b/Tests/test_image_histogram.py deleted file mode 100644 index c86cb578a..000000000 --- a/Tests/test_image_histogram.py +++ /dev/null @@ -1,19 +0,0 @@ -from tester import * - -from PIL import Image - -def test_histogram(): - - def histogram(mode): - h = lena(mode).histogram() - return len(h), min(h), max(h) - - assert_equal(histogram("1"), (256, 0, 8872)) - assert_equal(histogram("L"), (256, 0, 199)) - assert_equal(histogram("I"), (256, 0, 199)) - assert_equal(histogram("F"), (256, 0, 199)) - assert_equal(histogram("P"), (256, 0, 2912)) - assert_equal(histogram("RGB"), (768, 0, 285)) - assert_equal(histogram("RGBA"), (1024, 0, 16384)) - assert_equal(histogram("CMYK"), (1024, 0, 16384)) - assert_equal(histogram("YCbCr"), (768, 0, 741)) diff --git a/Tests/test_imageenhance.py b/Tests/test_imageenhance.py deleted file mode 100644 index 04f16bfa5..000000000 --- a/Tests/test_imageenhance.py +++ /dev/null @@ -1,19 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageEnhance - -def test_sanity(): - - # FIXME: assert_image - assert_no_exception(lambda: ImageEnhance.Color(lena()).enhance(0.5)) - assert_no_exception(lambda: ImageEnhance.Contrast(lena()).enhance(0.5)) - assert_no_exception(lambda: ImageEnhance.Brightness(lena()).enhance(0.5)) - assert_no_exception(lambda: ImageEnhance.Sharpness(lena()).enhance(0.5)) - -def test_crash(): - - # crashes on small images - im = Image.new("RGB", (1, 1)) - assert_no_exception(lambda: ImageEnhance.Sharpness(im).enhance(0.5)) - diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py deleted file mode 100644 index 93aa694d8..000000000 --- a/Tests/test_lib_image.py +++ /dev/null @@ -1,30 +0,0 @@ -from tester import * - -from PIL import Image - -def test_setmode(): - - im = Image.new("L", (1, 1), 255) - im.im.setmode("1") - assert_equal(im.im.getpixel((0, 0)), 255) - im.im.setmode("L") - assert_equal(im.im.getpixel((0, 0)), 255) - - im = Image.new("1", (1, 1), 1) - im.im.setmode("L") - assert_equal(im.im.getpixel((0, 0)), 255) - im.im.setmode("1") - assert_equal(im.im.getpixel((0, 0)), 255) - - im = Image.new("RGB", (1, 1), (1, 2, 3)) - im.im.setmode("RGB") - assert_equal(im.im.getpixel((0, 0)), (1, 2, 3)) - im.im.setmode("RGBA") - assert_equal(im.im.getpixel((0, 0)), (1, 2, 3, 255)) - im.im.setmode("RGBX") - assert_equal(im.im.getpixel((0, 0)), (1, 2, 3, 255)) - im.im.setmode("RGB") - assert_equal(im.im.getpixel((0, 0)), (1, 2, 3)) - - assert_exception(ValueError, lambda: im.im.setmode("L")) - assert_exception(ValueError, lambda: im.im.setmode("RGBABCDE")) diff --git a/test/test_000_sanity.py b/test/test_000_sanity.py new file mode 100644 index 000000000..b536dd96a --- /dev/null +++ b/test/test_000_sanity.py @@ -0,0 +1,32 @@ +from tester import unittest, PillowTestCase + +import PIL +import PIL.Image + + +class TestSanity(PillowTestCase): + + def test_sanity(self): + + # Make sure we have the binary extension + im = PIL.Image.core.new("L", (100, 100)) + + self.assertEqual(PIL.Image.VERSION[:3], '1.1') + + # Create an image and do stuff with it. + im = PIL.Image.new("1", (100, 100)) + self.assertEqual((im.mode, im.size), ('1', (100, 100))) + self.assertEqual(len(im.tobytes()), 1300) + + # Create images in all remaining major modes. + im = PIL.Image.new("L", (100, 100)) + im = PIL.Image.new("P", (100, 100)) + im = PIL.Image.new("RGB", (100, 100)) + im = PIL.Image.new("I", (100, 100)) + im = PIL.Image.new("F", (100, 100)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_histogram.py b/test/test_image_histogram.py new file mode 100644 index 000000000..8a46149ff --- /dev/null +++ b/test/test_image_histogram.py @@ -0,0 +1,26 @@ +from tester import unittest, PillowTestCase, lena + + +class TestImageHistogram(PillowTestCase): + + def test_histogram(self): + + def histogram(mode): + h = lena(mode).histogram() + return len(h), min(h), max(h) + + self.assertEqual(histogram("1"), (256, 0, 8872)) + self.assertEqual(histogram("L"), (256, 0, 199)) + self.assertEqual(histogram("I"), (256, 0, 199)) + self.assertEqual(histogram("F"), (256, 0, 199)) + self.assertEqual(histogram("P"), (256, 0, 2912)) + self.assertEqual(histogram("RGB"), (768, 0, 285)) + self.assertEqual(histogram("RGBA"), (1024, 0, 16384)) + self.assertEqual(histogram("CMYK"), (1024, 0, 16384)) + self.assertEqual(histogram("YCbCr"), (768, 0, 741)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imageenhance.py b/test/test_imageenhance.py new file mode 100644 index 000000000..c126bf344 --- /dev/null +++ b/test/test_imageenhance.py @@ -0,0 +1,28 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image +from PIL import ImageEnhance + + +class TestImageEnhance(PillowTestCase): + + def test_sanity(self): + + # FIXME: assert_image + # Implicit asserts no exception: + ImageEnhance.Color(lena()).enhance(0.5) + ImageEnhance.Contrast(lena()).enhance(0.5) + ImageEnhance.Brightness(lena()).enhance(0.5) + ImageEnhance.Sharpness(lena()).enhance(0.5) + + def test_crash(self): + + # crashes on small images + im = Image.new("RGB", (1, 1)) + ImageEnhance.Sharpness(im).enhance(0.5) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_lib_image.py b/test/test_lib_image.py new file mode 100644 index 000000000..8694c1d51 --- /dev/null +++ b/test/test_lib_image.py @@ -0,0 +1,39 @@ +from tester import unittest, PillowTestCase + +from PIL import Image + + +class TestSanity(PillowTestCase): + + def test_setmode(self): + + im = Image.new("L", (1, 1), 255) + im.im.setmode("1") + self.assertEqual(im.im.getpixel((0, 0)), 255) + 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, lambda: im.im.setmode("L")) + self.assertRaises(ValueError, lambda: im.im.setmode("RGBABCDE")) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/tester.py b/test/tester.py index 894d37883..8df32e317 100644 --- a/test/tester.py +++ b/test/tester.py @@ -112,51 +112,6 @@ py3 = (sys.version_info >= (3, 0)) # assert_equal(a, b, msg) # # -# def assert_greater(a, b, msg=None): -# if a > b: -# success() -# else: -# failure(msg or "%r unexpectedly not greater than %r" % (a, b)) -# -# -# def assert_greater_equal(a, b, msg=None): -# if a >= b: -# success() -# else: -# failure( -# msg or "%r unexpectedly not greater than or equal to %r" -# % (a, b)) -# -# -# def assert_less(a, b, msg=None): -# if a < b: -# success() -# else: -# failure(msg or "%r unexpectedly not less than %r" % (a, b)) -# -# -# def assert_less_equal(a, b, msg=None): -# if a <= b: -# success() -# else: -# failure( -# msg or "%r unexpectedly not less than or equal to %r" % (a, b)) -# -# -# def assert_is_instance(a, b, msg=None): -# if isinstance(a, b): -# success() -# else: -# failure(msg or "got %r, expected %r" % (type(a), b)) -# -# -# def assert_in(a, b, msg=None): -# if a in b: -# success() -# else: -# failure(msg or "%r unexpectedly not in %r" % (a, b)) -# -# # def assert_match(v, pattern, msg=None): # import re # if re.match(pattern, v): @@ -165,55 +120,6 @@ py3 = (sys.version_info >= (3, 0)) # failure(msg or "got %r, doesn't match pattern %r" % (v, pattern)) # # -# def assert_exception(exc_class, func): -# import sys -# import traceback -# try: -# func() -# except exc_class: -# success() -# except: -# failure("expected %r exception, got %r" % ( -# exc_class.__name__, sys.exc_info()[0].__name__)) -# traceback.print_exc() -# else: -# failure( -# "expected %r exception, got no exception" % exc_class.__name__) -# -# -# def assert_no_exception(func): -# import sys -# import traceback -# try: -# func() -# except: -# failure("expected no exception, got %r" % sys.exc_info()[0].__name__) -# traceback.print_exc() -# else: -# success() -# -# -# def assert_warning(warn_class, func): -# # note: this assert calls func three times! -# import warnings -# -# def warn_error(message, category=UserWarning, **options): -# raise category(message) -# -# def warn_ignore(message, category=UserWarning, **options): -# pass -# warn = warnings.warn -# result = None -# try: -# warnings.warn = warn_ignore -# assert_no_exception(func) -# result = func() -# warnings.warn = warn_error -# assert_exception(warn_class, func) -# finally: -# warnings.warn = warn # restore -# return result -# # # helpers # # from io import BytesIO From c8626c6e93d1d05c1d25e649447b4a3d3b62ee8e Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Jun 2014 09:09:23 +0300 Subject: [PATCH 06/33] Move more tests --- Tests/test_image_array.py | 33 -------------- Tests/test_image_crop.py | 52 --------------------- Tests/test_image_filter.py | 82 --------------------------------- Tests/test_image_putdata.py | 40 ---------------- Tests/test_imagefilter.py | 31 ------------- Tests/test_imagestat.py | 52 --------------------- test/test_image_array.py | 46 +++++++++++++++++++ test/test_image_crop.py | 59 ++++++++++++++++++++++++ test/test_image_filter.py | 91 +++++++++++++++++++++++++++++++++++++ test/test_image_putdata.py | 48 +++++++++++++++++++ test/test_imagefilter.py | 37 +++++++++++++++ test/test_imagestat.py | 63 +++++++++++++++++++++++++ test/tester.py | 11 ----- test/tests.py | 19 -------- 14 files changed, 344 insertions(+), 320 deletions(-) delete mode 100644 Tests/test_image_array.py delete mode 100644 Tests/test_image_crop.py delete mode 100644 Tests/test_image_filter.py delete mode 100644 Tests/test_image_putdata.py delete mode 100644 Tests/test_imagefilter.py delete mode 100644 Tests/test_imagestat.py create mode 100644 test/test_image_array.py create mode 100644 test/test_image_crop.py create mode 100644 test/test_image_filter.py create mode 100644 test/test_image_putdata.py create mode 100644 test/test_imagefilter.py create mode 100644 test/test_imagestat.py delete mode 100644 test/tests.py diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py deleted file mode 100644 index 351621d3a..000000000 --- a/Tests/test_image_array.py +++ /dev/null @@ -1,33 +0,0 @@ -from tester import * - -from PIL import Image - -im = lena().resize((128, 100)) - -def test_toarray(): - def test(mode): - ai = im.convert(mode).__array_interface__ - return ai["shape"], ai["typestr"], len(ai["data"]) - # assert_equal(test("1"), ((100, 128), '|b1', 1600)) - assert_equal(test("L"), ((100, 128), '|u1', 12800)) - assert_equal(test("I"), ((100, 128), Image._ENDIAN + 'i4', 51200)) # FIXME: wrong? - assert_equal(test("F"), ((100, 128), Image._ENDIAN + 'f4', 51200)) # FIXME: wrong? - assert_equal(test("RGB"), ((100, 128, 3), '|u1', 38400)) - assert_equal(test("RGBA"), ((100, 128, 4), '|u1', 51200)) - assert_equal(test("RGBX"), ((100, 128, 4), '|u1', 51200)) - -def test_fromarray(): - def test(mode): - i = im.convert(mode) - a = i.__array_interface__ - a["strides"] = 1 # pretend it's non-contigous - i.__array_interface__ = a # patch in new version of attribute - out = Image.fromarray(i) - return out.mode, out.size, list(i.getdata()) == list(out.getdata()) - # assert_equal(test("1"), ("1", (128, 100), True)) - assert_equal(test("L"), ("L", (128, 100), True)) - assert_equal(test("I"), ("I", (128, 100), True)) - assert_equal(test("F"), ("F", (128, 100), True)) - assert_equal(test("RGB"), ("RGB", (128, 100), True)) - assert_equal(test("RGBA"), ("RGBA", (128, 100), True)) - assert_equal(test("RGBX"), ("RGBA", (128, 100), True)) diff --git a/Tests/test_image_crop.py b/Tests/test_image_crop.py deleted file mode 100644 index 973606309..000000000 --- a/Tests/test_image_crop.py +++ /dev/null @@ -1,52 +0,0 @@ -from tester import * - -from PIL import Image - -def test_crop(): - def crop(mode): - out = lena(mode).crop((50, 50, 100, 100)) - assert_equal(out.mode, mode) - assert_equal(out.size, (50, 50)) - for mode in "1", "P", "L", "RGB", "I", "F": - yield_test(crop, mode) - -def test_wide_crop(): - - def crop(*bbox): - i = im.crop(bbox) - h = i.histogram() - while h and not h[-1]: - del h[-1] - return tuple(h) - - im = Image.new("L", (100, 100), 1) - - assert_equal(crop(0, 0, 100, 100), (0, 10000)) - assert_equal(crop(25, 25, 75, 75), (0, 2500)) - - # sides - assert_equal(crop(-25, 0, 25, 50), (1250, 1250)) - assert_equal(crop(0, -25, 50, 25), (1250, 1250)) - assert_equal(crop(75, 0, 125, 50), (1250, 1250)) - assert_equal(crop(0, 75, 50, 125), (1250, 1250)) - - assert_equal(crop(-25, 25, 125, 75), (2500, 5000)) - assert_equal(crop(25, -25, 75, 125), (2500, 5000)) - - # corners - assert_equal(crop(-25, -25, 25, 25), (1875, 625)) - assert_equal(crop(75, -25, 125, 25), (1875, 625)) - assert_equal(crop(75, 75, 125, 125), (1875, 625)) - assert_equal(crop(-25, 75, 25, 125), (1875, 625)) - -# -------------------------------------------------------------------- - -def test_negative_crop(): - # Check negative crop size (@PIL171) - - im = Image.new("L", (512, 512)) - im = im.crop((400, 400, 200, 200)) - - assert_equal(im.size, (0, 0)) - assert_equal(len(im.getdata()), 0) - assert_exception(IndexError, lambda: im.getdata()[0]) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py deleted file mode 100644 index f61e0c954..000000000 --- a/Tests/test_image_filter.py +++ /dev/null @@ -1,82 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageFilter - -def test_sanity(): - - def filter(filter): - im = lena("L") - out = im.filter(filter) - assert_equal(out.mode, im.mode) - assert_equal(out.size, im.size) - - filter(ImageFilter.BLUR) - filter(ImageFilter.CONTOUR) - filter(ImageFilter.DETAIL) - filter(ImageFilter.EDGE_ENHANCE) - filter(ImageFilter.EDGE_ENHANCE_MORE) - filter(ImageFilter.EMBOSS) - filter(ImageFilter.FIND_EDGES) - filter(ImageFilter.SMOOTH) - filter(ImageFilter.SMOOTH_MORE) - filter(ImageFilter.SHARPEN) - filter(ImageFilter.MaxFilter) - filter(ImageFilter.MedianFilter) - filter(ImageFilter.MinFilter) - filter(ImageFilter.ModeFilter) - filter(ImageFilter.Kernel((3, 3), list(range(9)))) - - assert_exception(TypeError, lambda: filter("hello")) - -def test_crash(): - - # crashes on small images - im = Image.new("RGB", (1, 1)) - assert_no_exception(lambda: im.filter(ImageFilter.SMOOTH)) - - im = Image.new("RGB", (2, 2)) - assert_no_exception(lambda: im.filter(ImageFilter.SMOOTH)) - - im = Image.new("RGB", (3, 3)) - assert_no_exception(lambda: im.filter(ImageFilter.SMOOTH)) - -def test_modefilter(): - - def modefilter(mode): - im = Image.new(mode, (3, 3), None) - im.putdata(list(range(9))) - # image is: - # 0 1 2 - # 3 4 5 - # 6 7 8 - mod = im.filter(ImageFilter.ModeFilter).getpixel((1, 1)) - im.putdata([0, 0, 1, 2, 5, 1, 5, 2, 0]) # mode=0 - mod2 = im.filter(ImageFilter.ModeFilter).getpixel((1, 1)) - return mod, mod2 - - assert_equal(modefilter("1"), (4, 0)) - assert_equal(modefilter("L"), (4, 0)) - assert_equal(modefilter("P"), (4, 0)) - assert_equal(modefilter("RGB"), ((4, 0, 0), (0, 0, 0))) - -def test_rankfilter(): - - def rankfilter(mode): - im = Image.new(mode, (3, 3), None) - im.putdata(list(range(9))) - # image is: - # 0 1 2 - # 3 4 5 - # 6 7 8 - min = im.filter(ImageFilter.MinFilter).getpixel((1, 1)) - med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1)) - max = im.filter(ImageFilter.MaxFilter).getpixel((1, 1)) - return min, med, max - - assert_equal(rankfilter("1"), (0, 4, 8)) - assert_equal(rankfilter("L"), (0, 4, 8)) - assert_exception(ValueError, lambda: rankfilter("P")) - assert_equal(rankfilter("RGB"), ((0, 0, 0), (4, 0, 0), (8, 0, 0))) - assert_equal(rankfilter("I"), (0, 4, 8)) - assert_equal(rankfilter("F"), (0.0, 4.0, 8.0)) diff --git a/Tests/test_image_putdata.py b/Tests/test_image_putdata.py deleted file mode 100644 index e25359fdf..000000000 --- a/Tests/test_image_putdata.py +++ /dev/null @@ -1,40 +0,0 @@ -from tester import * - -import sys - -from PIL import Image - -def test_sanity(): - - im1 = lena() - - data = list(im1.getdata()) - - im2 = Image.new(im1.mode, im1.size, 0) - im2.putdata(data) - - assert_image_equal(im1, im2) - - # readonly - im2 = Image.new(im1.mode, im2.size, 0) - im2.readonly = 1 - im2.putdata(data) - - assert_false(im2.readonly) - assert_image_equal(im1, im2) - - -def test_long_integers(): - # see bug-200802-systemerror - def put(value): - im = Image.new("RGBA", (1, 1)) - im.putdata([value]) - return im.getpixel((0, 0)) - assert_equal(put(0xFFFFFFFF), (255, 255, 255, 255)) - assert_equal(put(0xFFFFFFFF), (255, 255, 255, 255)) - assert_equal(put(-1), (255, 255, 255, 255)) - assert_equal(put(-1), (255, 255, 255, 255)) - if sys.maxsize > 2**32: - assert_equal(put(sys.maxsize), (255, 255, 255, 255)) - else: - assert_equal(put(sys.maxsize), (255, 255, 255, 127)) diff --git a/Tests/test_imagefilter.py b/Tests/test_imagefilter.py deleted file mode 100644 index 214f88024..000000000 --- a/Tests/test_imagefilter.py +++ /dev/null @@ -1,31 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageFilter - -def test_sanity(): - # see test_image_filter for more tests - - assert_no_exception(lambda: ImageFilter.MaxFilter) - assert_no_exception(lambda: ImageFilter.MedianFilter) - assert_no_exception(lambda: ImageFilter.MinFilter) - assert_no_exception(lambda: ImageFilter.ModeFilter) - assert_no_exception(lambda: ImageFilter.Kernel((3, 3), list(range(9)))) - assert_no_exception(lambda: ImageFilter.GaussianBlur) - assert_no_exception(lambda: ImageFilter.GaussianBlur(5)) - assert_no_exception(lambda: ImageFilter.UnsharpMask) - assert_no_exception(lambda: ImageFilter.UnsharpMask(10)) - - assert_no_exception(lambda: ImageFilter.BLUR) - assert_no_exception(lambda: ImageFilter.CONTOUR) - assert_no_exception(lambda: ImageFilter.DETAIL) - assert_no_exception(lambda: ImageFilter.EDGE_ENHANCE) - assert_no_exception(lambda: ImageFilter.EDGE_ENHANCE_MORE) - assert_no_exception(lambda: ImageFilter.EMBOSS) - assert_no_exception(lambda: ImageFilter.FIND_EDGES) - assert_no_exception(lambda: ImageFilter.SMOOTH) - assert_no_exception(lambda: ImageFilter.SMOOTH_MORE) - assert_no_exception(lambda: ImageFilter.SHARPEN) - - - diff --git a/Tests/test_imagestat.py b/Tests/test_imagestat.py deleted file mode 100644 index 02a461e22..000000000 --- a/Tests/test_imagestat.py +++ /dev/null @@ -1,52 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageStat - -def test_sanity(): - - im = lena() - - st = ImageStat.Stat(im) - st = ImageStat.Stat(im.histogram()) - st = ImageStat.Stat(im, Image.new("1", im.size, 1)) - - assert_no_exception(lambda: st.extrema) - assert_no_exception(lambda: st.sum) - assert_no_exception(lambda: st.mean) - assert_no_exception(lambda: st.median) - assert_no_exception(lambda: st.rms) - assert_no_exception(lambda: st.sum2) - assert_no_exception(lambda: st.var) - assert_no_exception(lambda: st.stddev) - assert_exception(AttributeError, lambda: st.spam) - - assert_exception(TypeError, lambda: ImageStat.Stat(1)) - -def test_lena(): - - im = lena() - - st = ImageStat.Stat(im) - - # verify a few values - assert_equal(st.extrema[0], (61, 255)) - assert_equal(st.median[0], 197) - assert_equal(st.sum[0], 2954416) - assert_equal(st.sum[1], 2027250) - assert_equal(st.sum[2], 1727331) - -def test_constant(): - - im = Image.new("L", (128, 128), 128) - - st = ImageStat.Stat(im) - - assert_equal(st.extrema[0], (128, 128)) - assert_equal(st.sum[0], 128**3) - assert_equal(st.sum2[0], 128**4) - assert_equal(st.mean[0], 128) - assert_equal(st.median[0], 128) - assert_equal(st.rms[0], 128) - assert_equal(st.var[0], 0) - assert_equal(st.stddev[0], 0) diff --git a/test/test_image_array.py b/test/test_image_array.py new file mode 100644 index 000000000..97599f9f0 --- /dev/null +++ b/test/test_image_array.py @@ -0,0 +1,46 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image + +im = lena().resize((128, 100)) + + +class TestImageCrop(PillowTestCase): + + def test_toarray(self): + def test(mode): + ai = im.convert(mode).__array_interface__ + return ai["shape"], ai["typestr"], len(ai["data"]) + # self.assertEqual(test("1"), ((100, 128), '|b1', 1600)) + self.assertEqual(test("L"), ((100, 128), '|u1', 12800)) + + # FIXME: wrong? + self.assertEqual(test("I"), ((100, 128), Image._ENDIAN + 'i4', 51200)) + # FIXME: wrong? + self.assertEqual(test("F"), ((100, 128), Image._ENDIAN + 'f4', 51200)) + + self.assertEqual(test("RGB"), ((100, 128, 3), '|u1', 38400)) + self.assertEqual(test("RGBA"), ((100, 128, 4), '|u1', 51200)) + self.assertEqual(test("RGBX"), ((100, 128, 4), '|u1', 51200)) + + def test_fromarray(self): + def test(mode): + i = im.convert(mode) + a = i.__array_interface__ + a["strides"] = 1 # pretend it's non-contigous + i.__array_interface__ = a # patch in new version of attribute + out = Image.fromarray(i) + return out.mode, out.size, list(i.getdata()) == list(out.getdata()) + # self.assertEqual(test("1"), ("1", (128, 100), True)) + self.assertEqual(test("L"), ("L", (128, 100), True)) + self.assertEqual(test("I"), ("I", (128, 100), True)) + self.assertEqual(test("F"), ("F", (128, 100), True)) + self.assertEqual(test("RGB"), ("RGB", (128, 100), True)) + self.assertEqual(test("RGBA"), ("RGBA", (128, 100), True)) + self.assertEqual(test("RGBX"), ("RGBA", (128, 100), True)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_crop.py b/test/test_image_crop.py new file mode 100644 index 000000000..a81ae000a --- /dev/null +++ b/test/test_image_crop.py @@ -0,0 +1,59 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImageCrop(PillowTestCase): + + def test_crop(self): + def crop(mode): + out = lena(mode).crop((50, 50, 100, 100)) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, (50, 50)) + for mode in "1", "P", "L", "RGB", "I", "F": + crop(mode) + + def test_wide_crop(self): + + def crop(*bbox): + i = im.crop(bbox) + h = i.histogram() + while h and not h[-1]: + del h[-1] + return tuple(h) + + im = Image.new("L", (100, 100), 1) + + self.assertEqual(crop(0, 0, 100, 100), (0, 10000)) + self.assertEqual(crop(25, 25, 75, 75), (0, 2500)) + + # sides + self.assertEqual(crop(-25, 0, 25, 50), (1250, 1250)) + self.assertEqual(crop(0, -25, 50, 25), (1250, 1250)) + self.assertEqual(crop(75, 0, 125, 50), (1250, 1250)) + self.assertEqual(crop(0, 75, 50, 125), (1250, 1250)) + + self.assertEqual(crop(-25, 25, 125, 75), (2500, 5000)) + self.assertEqual(crop(25, -25, 75, 125), (2500, 5000)) + + # corners + self.assertEqual(crop(-25, -25, 25, 25), (1875, 625)) + self.assertEqual(crop(75, -25, 125, 25), (1875, 625)) + self.assertEqual(crop(75, 75, 125, 125), (1875, 625)) + self.assertEqual(crop(-25, 75, 25, 125), (1875, 625)) + + def test_negative_crop(self): + # Check negative crop size (@PIL171) + + im = Image.new("L", (512, 512)) + im = im.crop((400, 400, 200, 200)) + + self.assertEqual(im.size, (0, 0)) + self.assertEqual(len(im.getdata()), 0) + self.assertRaises(IndexError, lambda: im.getdata()[0]) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_filter.py b/test/test_image_filter.py new file mode 100644 index 000000000..02fa18c18 --- /dev/null +++ b/test/test_image_filter.py @@ -0,0 +1,91 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image +from PIL import ImageFilter + + +class TestImageFilter(PillowTestCase): + + def test_sanity(self): + + def filter(filter): + im = lena("L") + out = im.filter(filter) + self.assertEqual(out.mode, im.mode) + self.assertEqual(out.size, im.size) + + filter(ImageFilter.BLUR) + filter(ImageFilter.CONTOUR) + filter(ImageFilter.DETAIL) + filter(ImageFilter.EDGE_ENHANCE) + filter(ImageFilter.EDGE_ENHANCE_MORE) + filter(ImageFilter.EMBOSS) + filter(ImageFilter.FIND_EDGES) + filter(ImageFilter.SMOOTH) + filter(ImageFilter.SMOOTH_MORE) + filter(ImageFilter.SHARPEN) + filter(ImageFilter.MaxFilter) + filter(ImageFilter.MedianFilter) + filter(ImageFilter.MinFilter) + filter(ImageFilter.ModeFilter) + filter(ImageFilter.Kernel((3, 3), list(range(9)))) + + self.assertRaises(TypeError, lambda: filter("hello")) + + def test_crash(self): + + # crashes on small images + im = Image.new("RGB", (1, 1)) + im.filter(ImageFilter.SMOOTH) + + im = Image.new("RGB", (2, 2)) + im.filter(ImageFilter.SMOOTH) + + im = Image.new("RGB", (3, 3)) + im.filter(ImageFilter.SMOOTH) + + def test_modefilter(self): + + def modefilter(mode): + im = Image.new(mode, (3, 3), None) + im.putdata(list(range(9))) + # image is: + # 0 1 2 + # 3 4 5 + # 6 7 8 + mod = im.filter(ImageFilter.ModeFilter).getpixel((1, 1)) + im.putdata([0, 0, 1, 2, 5, 1, 5, 2, 0]) # mode=0 + mod2 = im.filter(ImageFilter.ModeFilter).getpixel((1, 1)) + return mod, mod2 + + self.assertEqual(modefilter("1"), (4, 0)) + self.assertEqual(modefilter("L"), (4, 0)) + self.assertEqual(modefilter("P"), (4, 0)) + self.assertEqual(modefilter("RGB"), ((4, 0, 0), (0, 0, 0))) + + def test_rankfilter(self): + + def rankfilter(mode): + im = Image.new(mode, (3, 3), None) + im.putdata(list(range(9))) + # image is: + # 0 1 2 + # 3 4 5 + # 6 7 8 + min = im.filter(ImageFilter.MinFilter).getpixel((1, 1)) + med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1)) + max = im.filter(ImageFilter.MaxFilter).getpixel((1, 1)) + return min, med, max + + self.assertEqual(rankfilter("1"), (0, 4, 8)) + self.assertEqual(rankfilter("L"), (0, 4, 8)) + self.assertRaises(ValueError, lambda: rankfilter("P")) + self.assertEqual(rankfilter("RGB"), ((0, 0, 0), (4, 0, 0), (8, 0, 0))) + self.assertEqual(rankfilter("I"), (0, 4, 8)) + self.assertEqual(rankfilter("F"), (0.0, 4.0, 8.0)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_putdata.py b/test/test_image_putdata.py new file mode 100644 index 000000000..16e938bf8 --- /dev/null +++ b/test/test_image_putdata.py @@ -0,0 +1,48 @@ +from tester import unittest, PillowTestCase, lena + +import sys + +from PIL import Image + + +class TestImagePutData(PillowTestCase): + + def test_sanity(self): + + im1 = lena() + + data = list(im1.getdata()) + + im2 = Image.new(im1.mode, im1.size, 0) + im2.putdata(data) + + self.assert_image_equal(im1, im2) + + # readonly + im2 = Image.new(im1.mode, im2.size, 0) + im2.readonly = 1 + im2.putdata(data) + + self.assertFalse(im2.readonly) + self.assert_image_equal(im1, im2) + + def test_long_integers(self): + # see bug-200802-systemerror + def put(value): + im = Image.new("RGBA", (1, 1)) + im.putdata([value]) + return im.getpixel((0, 0)) + self.assertEqual(put(0xFFFFFFFF), (255, 255, 255, 255)) + self.assertEqual(put(0xFFFFFFFF), (255, 255, 255, 255)) + self.assertEqual(put(-1), (255, 255, 255, 255)) + self.assertEqual(put(-1), (255, 255, 255, 255)) + if sys.maxsize > 2**32: + self.assertEqual(put(sys.maxsize), (255, 255, 255, 255)) + else: + self.assertEqual(put(sys.maxsize), (255, 255, 255, 127)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagefilter.py b/test/test_imagefilter.py new file mode 100644 index 000000000..1f01cb615 --- /dev/null +++ b/test/test_imagefilter.py @@ -0,0 +1,37 @@ +from tester import unittest, PillowTestCase + +from PIL import ImageFilter + + +class TestImageFilter(PillowTestCase): + + def test_sanity(self): + # see test_image_filter for more tests + + # Check these run. Exceptions cause failures. + ImageFilter.MaxFilter + ImageFilter.MedianFilter + ImageFilter.MinFilter + ImageFilter.ModeFilter + ImageFilter.Kernel((3, 3), list(range(9))) + ImageFilter.GaussianBlur + ImageFilter.GaussianBlur(5) + ImageFilter.UnsharpMask + ImageFilter.UnsharpMask(10) + + ImageFilter.BLUR + ImageFilter.CONTOUR + ImageFilter.DETAIL + ImageFilter.EDGE_ENHANCE + ImageFilter.EDGE_ENHANCE_MORE + ImageFilter.EMBOSS + ImageFilter.FIND_EDGES + ImageFilter.SMOOTH + ImageFilter.SMOOTH_MORE + ImageFilter.SHARPEN + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagestat.py b/test/test_imagestat.py new file mode 100644 index 000000000..fc228a886 --- /dev/null +++ b/test/test_imagestat.py @@ -0,0 +1,63 @@ +from tester import unittest, PillowTestCase, lena + +from PIL import Image +from PIL import ImageStat + + +class TestImageStat(PillowTestCase): + + def test_sanity(self): + + im = lena() + + st = ImageStat.Stat(im) + st = ImageStat.Stat(im.histogram()) + st = ImageStat.Stat(im, Image.new("1", im.size, 1)) + + # Check these run. Exceptions will cause failures. + st.extrema + st.sum + st.mean + st.median + st.rms + st.sum2 + st.var + st.stddev + + self.assertRaises(AttributeError, lambda: st.spam) + + self.assertRaises(TypeError, lambda: ImageStat.Stat(1)) + + def test_lena(self): + + im = lena() + + st = ImageStat.Stat(im) + + # verify a few values + self.assertEqual(st.extrema[0], (61, 255)) + self.assertEqual(st.median[0], 197) + self.assertEqual(st.sum[0], 2954416) + self.assertEqual(st.sum[1], 2027250) + self.assertEqual(st.sum[2], 1727331) + + def test_constant(self): + + im = Image.new("L", (128, 128), 128) + + st = ImageStat.Stat(im) + + self.assertEqual(st.extrema[0], (128, 128)) + self.assertEqual(st.sum[0], 128**3) + self.assertEqual(st.sum2[0], 128**4) + self.assertEqual(st.mean[0], 128) + self.assertEqual(st.median[0], 128) + self.assertEqual(st.rms[0], 128) + self.assertEqual(st.var[0], 0) + self.assertEqual(st.stddev[0], 0) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/tester.py b/test/tester.py index 8df32e317..418f1261f 100644 --- a/test/tester.py +++ b/test/tester.py @@ -161,17 +161,6 @@ def lena(mode="RGB", cache={}): # success() # # -# def assert_image_equal(a, b, msg=None): -# if a.mode != b.mode: -# failure(msg or "got mode %r, expected %r" % (a.mode, b.mode)) -# elif a.size != b.size: -# failure(msg or "got size %r, expected %r" % (a.size, b.size)) -# elif a.tobytes() != b.tobytes(): -# failure(msg or "got different content") -# else: -# success() -# -# # def assert_image_completely_equal(a, b, msg=None): # if a != b: # failure(msg or "images different") diff --git a/test/tests.py b/test/tests.py deleted file mode 100644 index 1bd308922..000000000 --- a/test/tests.py +++ /dev/null @@ -1,19 +0,0 @@ -from tester import unittest - - -class SomeTests(unittest.TestCase): - """ - Can we start moving the test suite here? - """ - - def test_suite_should_move_here(self): - """ - Great idea! - """ - self.assertTrue(True) - - -if __name__ == '__main__': - unittest.main() - -# End of file From f14b928ce68436ede7f4fcc083179dd89bddc1de Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Jun 2014 10:11:22 +0300 Subject: [PATCH 07/33] Rename tester.py to helper.py because it no longer drives the tests --- test/{tester.py => helper.py} | 0 test/test_000_sanity.py | 2 +- test/test_image_array.py | 2 +- test/test_image_crop.py | 2 +- test/test_image_filter.py | 2 +- test/test_image_frombytes.py | 2 +- test/test_image_getim.py | 2 +- test/test_image_histogram.py | 2 +- test/test_image_putdata.py | 2 +- test/test_image_tobytes.py | 2 +- test/test_imagedraw.py | 2 +- test/test_imageenhance.py | 2 +- test/test_imagefilter.py | 2 +- test/test_imagestat.py | 2 +- test/test_lib_image.py | 2 +- 15 files changed, 14 insertions(+), 14 deletions(-) rename test/{tester.py => helper.py} (100%) diff --git a/test/tester.py b/test/helper.py similarity index 100% rename from test/tester.py rename to test/helper.py diff --git a/test/test_000_sanity.py b/test/test_000_sanity.py index b536dd96a..22e582ec3 100644 --- a/test/test_000_sanity.py +++ b/test/test_000_sanity.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase +from helper import unittest, PillowTestCase import PIL import PIL.Image diff --git a/test/test_image_array.py b/test/test_image_array.py index 97599f9f0..a0f5f29e1 100644 --- a/test/test_image_array.py +++ b/test/test_image_array.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image diff --git a/test/test_image_crop.py b/test/test_image_crop.py index a81ae000a..da93fe7c8 100644 --- a/test/test_image_crop.py +++ b/test/test_image_crop.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image diff --git a/test/test_image_filter.py b/test/test_image_filter.py index 02fa18c18..4a85b0a2e 100644 --- a/test/test_image_filter.py +++ b/test/test_image_filter.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image from PIL import ImageFilter diff --git a/test/test_image_frombytes.py b/test/test_image_frombytes.py index ee68b6722..aad8046a1 100644 --- a/test/test_image_frombytes.py +++ b/test/test_image_frombytes.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image diff --git a/test/test_image_getim.py b/test/test_image_getim.py index 02744a529..d498d3923 100644 --- a/test/test_image_getim.py +++ b/test/test_image_getim.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena, py3 +from helper import unittest, PillowTestCase, lena, py3 class TestImageGetIm(PillowTestCase): diff --git a/test/test_image_histogram.py b/test/test_image_histogram.py index 8a46149ff..70f78a1fb 100644 --- a/test/test_image_histogram.py +++ b/test/test_image_histogram.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena class TestImageHistogram(PillowTestCase): diff --git a/test/test_image_putdata.py b/test/test_image_putdata.py index 16e938bf8..c7c3115aa 100644 --- a/test/test_image_putdata.py +++ b/test/test_image_putdata.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena import sys diff --git a/test/test_image_tobytes.py b/test/test_image_tobytes.py index ee7b4c9e6..3be9128c1 100644 --- a/test/test_image_tobytes.py +++ b/test/test_image_tobytes.py @@ -1,4 +1,4 @@ -from tester import unittest, lena +from helper import unittest, lena class TestImageToBytes(unittest.TestCase): diff --git a/test/test_imagedraw.py b/test/test_imagedraw.py index f9a5e5f6a..44403e50c 100644 --- a/test/test_imagedraw.py +++ b/test/test_imagedraw.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image from PIL import ImageColor diff --git a/test/test_imageenhance.py b/test/test_imageenhance.py index c126bf344..433c49cf6 100644 --- a/test/test_imageenhance.py +++ b/test/test_imageenhance.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image from PIL import ImageEnhance diff --git a/test/test_imagefilter.py b/test/test_imagefilter.py index 1f01cb615..f7edb409a 100644 --- a/test/test_imagefilter.py +++ b/test/test_imagefilter.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase +from helper import unittest, PillowTestCase from PIL import ImageFilter diff --git a/test/test_imagestat.py b/test/test_imagestat.py index fc228a886..4d30ff023 100644 --- a/test/test_imagestat.py +++ b/test/test_imagestat.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase, lena +from helper import unittest, PillowTestCase, lena from PIL import Image from PIL import ImageStat diff --git a/test/test_lib_image.py b/test/test_lib_image.py index 8694c1d51..e0a903b00 100644 --- a/test/test_lib_image.py +++ b/test/test_lib_image.py @@ -1,4 +1,4 @@ -from tester import unittest, PillowTestCase +from helper import unittest, PillowTestCase from PIL import Image From 184c380e106a48aac92f3fd58b70c8b3a82b6d28 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Jun 2014 16:18:04 +0300 Subject: [PATCH 08/33] Convert more tests, including a method to skip on ImportError --- Tests/test_image_copy.py | 12 ------------ Tests/test_imagegrab.py | 13 ------------- test/helper.py | 20 +++++++++++--------- test/test_image_copy.py | 20 ++++++++++++++++++++ test/test_imagegrab.py | 25 +++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 34 deletions(-) delete mode 100644 Tests/test_image_copy.py delete mode 100644 Tests/test_imagegrab.py create mode 100644 test/test_image_copy.py create mode 100644 test/test_imagegrab.py diff --git a/Tests/test_image_copy.py b/Tests/test_image_copy.py deleted file mode 100644 index 40a3dc496..000000000 --- a/Tests/test_image_copy.py +++ /dev/null @@ -1,12 +0,0 @@ -from tester import * - -from PIL import Image - -def test_copy(): - def copy(mode): - im = lena(mode) - out = im.copy() - assert_equal(out.mode, mode) - assert_equal(out.size, im.size) - for mode in "1", "P", "L", "RGB", "I", "F": - yield_test(copy, mode) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py deleted file mode 100644 index 67ff71960..000000000 --- a/Tests/test_imagegrab.py +++ /dev/null @@ -1,13 +0,0 @@ -from tester import * - -from PIL import Image -try: - from PIL import ImageGrab -except ImportError as v: - skip(v) - -def test_grab(): - im = ImageGrab.grab() - assert_image(im, im.mode, im.size) - - diff --git a/test/helper.py b/test/helper.py index 418f1261f..8c45858b6 100644 --- a/test/helper.py +++ b/test/helper.py @@ -12,6 +12,17 @@ else: class PillowTestCase(unittest.TestCase): + def assert_image(self, im, mode, size, msg=None): + if mode is not None: + self.assertEqual( + im.mode, mode, + msg or "got mode %r, expected %r" % (im.mode, mode)) + + if size is not None: + self.assertEqual( + im.size, size, + msg or "got size %r, expected %r" % (im.size, size)) + def assert_image_equal(self, a, b, msg=None): self.assertEqual( a.mode, b.mode, @@ -152,15 +163,6 @@ def lena(mode="RGB", cache={}): return im -# def assert_image(im, mode, size, msg=None): -# if mode is not None and im.mode != mode: -# failure(msg or "got mode %r, expected %r" % (im.mode, mode)) -# elif size is not None and im.size != size: -# failure(msg or "got size %r, expected %r" % (im.size, size)) -# else: -# success() -# -# # def assert_image_completely_equal(a, b, msg=None): # if a != b: # failure(msg or "images different") diff --git a/test/test_image_copy.py b/test/test_image_copy.py new file mode 100644 index 000000000..a7882db94 --- /dev/null +++ b/test/test_image_copy.py @@ -0,0 +1,20 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImageCopy(PillowTestCase): + + def test_copy(self): + def copy(mode): + im = lena(mode) + out = im.copy() + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size) + for mode in "1", "P", "L", "RGB", "I", "F": + copy(mode) + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagegrab.py b/test/test_imagegrab.py new file mode 100644 index 000000000..fb953f435 --- /dev/null +++ b/test/test_imagegrab.py @@ -0,0 +1,25 @@ +from helper import unittest, PillowTestCase + +try: + from PIL import ImageGrab + + class TestImageCopy(PillowTestCase): + + def test_grab(self): + im = ImageGrab.grab() + self.assert_image(im, im.mode, im.size) + + def test_grab2(self): + im = ImageGrab.grab() + self.assert_image(im, im.mode, im.size) + +except ImportError as v: + class TestImageCopy(PillowTestCase): + def test_skip(self): + self.skipTest(v) + + +if __name__ == '__main__': + unittest.main() + +# End of file From b7eb1de922791f1604aac91c032de5cf4e3e2490 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Jun 2014 17:09:59 +0300 Subject: [PATCH 09/33] Fix skipping --- test/test_imagegrab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_imagegrab.py b/test/test_imagegrab.py index fb953f435..2275d34a1 100644 --- a/test/test_imagegrab.py +++ b/test/test_imagegrab.py @@ -13,10 +13,10 @@ try: im = ImageGrab.grab() self.assert_image(im, im.mode, im.size) -except ImportError as v: +except ImportError: class TestImageCopy(PillowTestCase): def test_skip(self): - self.skipTest(v) + self.skipTest("ImportError") if __name__ == '__main__': From d385dd81a7c3a425e0052f81bbbaa5e2ebb5a575 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 11:39:29 +0300 Subject: [PATCH 10/33] Converttest_image_load.py --- Tests/test_image_load.py | 27 --------------------------- test/test_image_load.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) delete mode 100644 Tests/test_image_load.py create mode 100644 test/test_image_load.py diff --git a/Tests/test_image_load.py b/Tests/test_image_load.py deleted file mode 100644 index b385b9686..000000000 --- a/Tests/test_image_load.py +++ /dev/null @@ -1,27 +0,0 @@ -from tester import * - -from PIL import Image - -import os - -def test_sanity(): - - im = lena() - - pix = im.load() - - assert_equal(pix[0, 0], (223, 162, 133)) - -def test_close(): - im = Image.open("Images/lena.gif") - assert_no_exception(lambda: im.close()) - assert_exception(ValueError, lambda: im.load()) - assert_exception(ValueError, lambda: im.getpixel((0,0))) - -def test_contextmanager(): - fn = None - with Image.open("Images/lena.gif") as im: - fn = im.fp.fileno() - assert_no_exception(lambda: os.fstat(fn)) - - assert_exception(OSError, lambda: os.fstat(fn)) diff --git a/test/test_image_load.py b/test/test_image_load.py new file mode 100644 index 000000000..80f505e6c --- /dev/null +++ b/test/test_image_load.py @@ -0,0 +1,35 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + +import os + + +class TestImageLoad(PillowTestCase): + + def test_sanity(self): + + im = lena() + + pix = im.load() + + self.assertEqual(pix[0, 0], (223, 162, 133)) + + def test_close(self): + im = Image.open("Images/lena.gif") + im.close() + self.assert_exception(ValueError, lambda: im.load()) + self.assert_exception(ValueError, lambda: im.getpixel((0, 0))) + + def test_contextmanager(self): + fn = None + with Image.open("Images/lena.gif") as im: + fn = im.fp.fileno() + os.fstat(fn) + + self.assert_exception(OSError, lambda: os.fstat(fn)) + +if __name__ == '__main__': + unittest.main() + +# End of file From 133120c6a655e933186f6da64a2b5548f6431d97 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 11:45:01 +0300 Subject: [PATCH 11/33] assert_exception is assertRaises --- .travis.yml | 4 ++-- test/test_image_load.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a2250771..7e4e5e9c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,13 +30,13 @@ script: # Don't cover PyPy: it fails intermittently and is x5.8 slower (#640) - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python selftest.py; fi - - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python Tests/run.py; fi - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then nosetests test/; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python Tests/run.py; fi # Cover the others - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* selftest.py; fi - - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then python Tests/run.py --coverage; fi - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* -m nose test/; fi + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then python Tests/run.py --coverage; fi after_success: diff --git a/test/test_image_load.py b/test/test_image_load.py index 80f505e6c..2001c233a 100644 --- a/test/test_image_load.py +++ b/test/test_image_load.py @@ -18,8 +18,8 @@ class TestImageLoad(PillowTestCase): def test_close(self): im = Image.open("Images/lena.gif") im.close() - self.assert_exception(ValueError, lambda: im.load()) - self.assert_exception(ValueError, lambda: im.getpixel((0, 0))) + self.assertRaises(ValueError, lambda: im.load()) + self.assertRaises(ValueError, lambda: im.getpixel((0, 0))) def test_contextmanager(self): fn = None @@ -27,7 +27,7 @@ class TestImageLoad(PillowTestCase): fn = im.fp.fileno() os.fstat(fn) - self.assert_exception(OSError, lambda: os.fstat(fn)) + self.assertRaises(OSError, lambda: os.fstat(fn)) if __name__ == '__main__': unittest.main() From 82df06243c1aa5d2c5d7f75262e83dfad7620f54 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 12:26:54 +0300 Subject: [PATCH 12/33] Convert more tests --- test/test_image_tobitmap.py | 24 ++++++++++++ test/test_imagechops.py | 74 +++++++++++++++++++++++++++++++++++ test/test_imagemath.py | 78 +++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 test/test_image_tobitmap.py create mode 100644 test/test_imagechops.py create mode 100644 test/test_imagemath.py diff --git a/test/test_image_tobitmap.py b/test/test_image_tobitmap.py new file mode 100644 index 000000000..00e5af0cd --- /dev/null +++ b/test/test_image_tobitmap.py @@ -0,0 +1,24 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImage(PillowTestCase): + +def test_sanity(self): + + self.assertRaises(ValueError, lambda: lena().tobitmap()) + assert_no_exception(lambda: lena().convert("1").tobitmap()) + + im1 = lena().convert("1") + + bitmap = im1.tobitmap() + + assert_true(isinstance(bitmap, bytes)) + assert_image_equal(im1, fromstring(bitmap)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagechops.py b/test/test_imagechops.py new file mode 100644 index 000000000..ec162d52f --- /dev/null +++ b/test/test_imagechops.py @@ -0,0 +1,74 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image +from PIL import ImageChops + + +class TestImage(PillowTestCase): + + def test_sanity(self): + + im = lena("L") + + ImageChops.constant(im, 128) + ImageChops.duplicate(im) + ImageChops.invert(im) + ImageChops.lighter(im, im) + ImageChops.darker(im, im) + ImageChops.difference(im, im) + ImageChops.multiply(im, im) + ImageChops.screen(im, im) + + ImageChops.add(im, im) + ImageChops.add(im, im, 2.0) + ImageChops.add(im, im, 2.0, 128) + ImageChops.subtract(im, im) + ImageChops.subtract(im, im, 2.0) + ImageChops.subtract(im, im, 2.0, 128) + + ImageChops.add_modulo(im, im) + ImageChops.subtract_modulo(im, im) + + ImageChops.blend(im, im, 0.5) + ImageChops.composite(im, im, im) + + ImageChops.offset(im, 10) + ImageChops.offset(im, 10, 20) + + def test_logical(self): + + def table(op, a, b): + out = [] + for x in (a, b): + imx = Image.new("1", (1, 1), x) + for y in (a, b): + imy = Image.new("1", (1, 1), y) + out.append(op(imx, imy).getpixel((0, 0))) + return tuple(out) + + self.assertEqual( + table(ImageChops.logical_and, 0, 1), (0, 0, 0, 255)) + self.assertEqual( + table(ImageChops.logical_or, 0, 1), (0, 255, 255, 255)) + self.assertEqual( + table(ImageChops.logical_xor, 0, 1), (0, 255, 255, 0)) + + self.assertEqual( + table(ImageChops.logical_and, 0, 128), (0, 0, 0, 255)) + self.assertEqual( + table(ImageChops.logical_or, 0, 128), (0, 255, 255, 255)) + self.assertEqual( + table(ImageChops.logical_xor, 0, 128), (0, 255, 255, 0)) + + self.assertEqual( + table(ImageChops.logical_and, 0, 255), (0, 0, 0, 255)) + self.assertEqual( + table(ImageChops.logical_or, 0, 255), (0, 255, 255, 255)) + self.assertEqual( + table(ImageChops.logical_xor, 0, 255), (0, 255, 255, 0)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagemath.py b/test/test_imagemath.py new file mode 100644 index 000000000..17d43d25a --- /dev/null +++ b/test/test_imagemath.py @@ -0,0 +1,78 @@ +from helper import unittest, PillowTestCase + +from PIL import Image +from PIL import ImageMath + + +def pixel(im): + if hasattr(im, "im"): + return "%s %r" % (im.mode, im.getpixel((0, 0))) + else: + if isinstance(im, type(0)): + return int(im) # hack to deal with booleans + print(im) + +A = Image.new("L", (1, 1), 1) +B = Image.new("L", (1, 1), 2) +F = Image.new("F", (1, 1), 3) +I = Image.new("I", (1, 1), 4) + +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_ops(self): + + self.assertEqual(pixel(ImageMath.eval("-A", images)), "I -1") + self.assertEqual(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") + + 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") + + 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_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") + + +if __name__ == '__main__': + unittest.main() + +# End of file From 1f94ea1fc613bc0fb1489235f84b673d8ca06d4a Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 12:30:59 +0300 Subject: [PATCH 13/33] Merge --- Tests/test_image_tobitmap.py | 15 --------- Tests/test_imagechops.py | 56 -------------------------------- Tests/test_imagemath.py | 62 ------------------------------------ 3 files changed, 133 deletions(-) delete mode 100644 Tests/test_image_tobitmap.py delete mode 100644 Tests/test_imagechops.py delete mode 100644 Tests/test_imagemath.py diff --git a/Tests/test_image_tobitmap.py b/Tests/test_image_tobitmap.py deleted file mode 100644 index 6fb10dd53..000000000 --- a/Tests/test_image_tobitmap.py +++ /dev/null @@ -1,15 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - - assert_exception(ValueError, lambda: lena().tobitmap()) - assert_no_exception(lambda: lena().convert("1").tobitmap()) - - im1 = lena().convert("1") - - bitmap = im1.tobitmap() - - assert_true(isinstance(bitmap, bytes)) - assert_image_equal(im1, fromstring(bitmap)) diff --git a/Tests/test_imagechops.py b/Tests/test_imagechops.py deleted file mode 100644 index 16eaaf55e..000000000 --- a/Tests/test_imagechops.py +++ /dev/null @@ -1,56 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageChops - -def test_sanity(): - - im = lena("L") - - ImageChops.constant(im, 128) - ImageChops.duplicate(im) - ImageChops.invert(im) - ImageChops.lighter(im, im) - ImageChops.darker(im, im) - ImageChops.difference(im, im) - ImageChops.multiply(im, im) - ImageChops.screen(im, im) - - ImageChops.add(im, im) - ImageChops.add(im, im, 2.0) - ImageChops.add(im, im, 2.0, 128) - ImageChops.subtract(im, im) - ImageChops.subtract(im, im, 2.0) - ImageChops.subtract(im, im, 2.0, 128) - - ImageChops.add_modulo(im, im) - ImageChops.subtract_modulo(im, im) - - ImageChops.blend(im, im, 0.5) - ImageChops.composite(im, im, im) - - ImageChops.offset(im, 10) - ImageChops.offset(im, 10, 20) - -def test_logical(): - - def table(op, a, b): - out = [] - for x in (a, b): - imx = Image.new("1", (1, 1), x) - for y in (a, b): - imy = Image.new("1", (1, 1), y) - out.append(op(imx, imy).getpixel((0, 0))) - return tuple(out) - - assert_equal(table(ImageChops.logical_and, 0, 1), (0, 0, 0, 255)) - assert_equal(table(ImageChops.logical_or, 0, 1), (0, 255, 255, 255)) - assert_equal(table(ImageChops.logical_xor, 0, 1), (0, 255, 255, 0)) - - assert_equal(table(ImageChops.logical_and, 0, 128), (0, 0, 0, 255)) - assert_equal(table(ImageChops.logical_or, 0, 128), (0, 255, 255, 255)) - assert_equal(table(ImageChops.logical_xor, 0, 128), (0, 255, 255, 0)) - - assert_equal(table(ImageChops.logical_and, 0, 255), (0, 0, 0, 255)) - assert_equal(table(ImageChops.logical_or, 0, 255), (0, 255, 255, 255)) - assert_equal(table(ImageChops.logical_xor, 0, 255), (0, 255, 255, 0)) diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py deleted file mode 100644 index eaeb711ba..000000000 --- a/Tests/test_imagemath.py +++ /dev/null @@ -1,62 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageMath - -def pixel(im): - if hasattr(im, "im"): - return "%s %r" % (im.mode, im.getpixel((0, 0))) - else: - if isinstance(im, type(0)): - return int(im) # hack to deal with booleans - print(im) - -A = Image.new("L", (1, 1), 1) -B = Image.new("L", (1, 1), 2) -F = Image.new("F", (1, 1), 3) -I = Image.new("I", (1, 1), 4) - -images = {"A": A, "B": B, "F": F, "I": I} - -def test_sanity(): - assert_equal(ImageMath.eval("1"), 1) - assert_equal(ImageMath.eval("1+A", A=2), 3) - assert_equal(pixel(ImageMath.eval("A+B", A=A, B=B)), "I 3") - assert_equal(pixel(ImageMath.eval("A+B", images)), "I 3") - assert_equal(pixel(ImageMath.eval("float(A)+B", images)), "F 3.0") - assert_equal(pixel(ImageMath.eval("int(float(A)+B)", images)), "I 3") - -def test_ops(): - - assert_equal(pixel(ImageMath.eval("-A", images)), "I -1") - assert_equal(pixel(ImageMath.eval("+B", images)), "L 2") - - assert_equal(pixel(ImageMath.eval("A+B", images)), "I 3") - assert_equal(pixel(ImageMath.eval("A-B", images)), "I -1") - assert_equal(pixel(ImageMath.eval("A*B", images)), "I 2") - assert_equal(pixel(ImageMath.eval("A/B", images)), "I 0") - assert_equal(pixel(ImageMath.eval("B**2", images)), "I 4") - assert_equal(pixel(ImageMath.eval("B**33", images)), "I 2147483647") - - assert_equal(pixel(ImageMath.eval("float(A)+B", images)), "F 3.0") - assert_equal(pixel(ImageMath.eval("float(A)-B", images)), "F -1.0") - assert_equal(pixel(ImageMath.eval("float(A)*B", images)), "F 2.0") - assert_equal(pixel(ImageMath.eval("float(A)/B", images)), "F 0.5") - assert_equal(pixel(ImageMath.eval("float(B)**2", images)), "F 4.0") - assert_equal(pixel(ImageMath.eval("float(B)**33", images)), "F 8589934592.0") - -def test_logical(): - assert_equal(pixel(ImageMath.eval("not A", images)), 0) - assert_equal(pixel(ImageMath.eval("A and B", images)), "L 2") - assert_equal(pixel(ImageMath.eval("A or B", images)), "L 1") - -def test_convert(): - assert_equal(pixel(ImageMath.eval("convert(A+B, 'L')", images)), "L 3") - assert_equal(pixel(ImageMath.eval("convert(A+B, '1')", images)), "1 0") - assert_equal(pixel(ImageMath.eval("convert(A+B, 'RGB')", images)), "RGB (3, 3, 3)") - -def test_compare(): - assert_equal(pixel(ImageMath.eval("min(A, B)", images)), "I 1") - assert_equal(pixel(ImageMath.eval("max(A, B)", images)), "I 2") - assert_equal(pixel(ImageMath.eval("A == 1", images)), "I 1") - assert_equal(pixel(ImageMath.eval("A == 2", images)), "I 0") From 34db40ddce99e6c4daf9b64873689d1249cb7a0f Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 12:39:15 +0300 Subject: [PATCH 14/33] Convert test_image_tobitmap.py --- test/helper.py | 31 +++++++++++++++---------------- test/test_image_tobitmap.py | 18 ++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/test/helper.py b/test/helper.py index 8c45858b6..0958c56a9 100644 --- a/test/helper.py +++ b/test/helper.py @@ -129,22 +129,21 @@ py3 = (sys.version_info >= (3, 0)) # success() # else: # failure(msg or "got %r, doesn't match pattern %r" % (v, pattern)) -# -# -# # helpers -# -# from io import BytesIO -# -# -# def fromstring(data): -# from PIL import Image -# return Image.open(BytesIO(data)) -# -# -# def tostring(im, format, **options): -# out = BytesIO() -# im.save(out, format, **options) -# return out.getvalue() + + +# helpers + +def fromstring(data): + from io import BytesIO + from PIL import Image + return Image.open(BytesIO(data)) + + +def tostring(im, format, **options): + from io import BytesIO + out = BytesIO() + im.save(out, format, **options) + return out.getvalue() def lena(mode="RGB", cache={}): diff --git a/test/test_image_tobitmap.py b/test/test_image_tobitmap.py index 00e5af0cd..4e2a16df0 100644 --- a/test/test_image_tobitmap.py +++ b/test/test_image_tobitmap.py @@ -1,21 +1,19 @@ -from helper import unittest, PillowTestCase, lena - -from PIL import Image +from helper import unittest, PillowTestCase, lena, fromstring class TestImage(PillowTestCase): -def test_sanity(self): + def test_sanity(self): - self.assertRaises(ValueError, lambda: lena().tobitmap()) - assert_no_exception(lambda: lena().convert("1").tobitmap()) + self.assertRaises(ValueError, lambda: lena().tobitmap()) + lena().convert("1").tobitmap() - im1 = lena().convert("1") + im1 = lena().convert("1") - bitmap = im1.tobitmap() + bitmap = im1.tobitmap() - assert_true(isinstance(bitmap, bytes)) - assert_image_equal(im1, fromstring(bitmap)) + self.assertIsInstance(bitmap, bytes) + self.assert_image_equal(im1, fromstring(bitmap)) if __name__ == '__main__': From 3fda42d280f13928b7647d97252331dd768b5bd7 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 13:39:45 +0300 Subject: [PATCH 15/33] Convert more tests --- Tests/test_file_fli.py | 14 ---- Tests/test_image_getbbox.py | 36 --------- Tests/test_image_getpixel.py | 49 ------------ Tests/test_imagefont.py | 135 -------------------------------- test/helper.py | 46 +++++------ test/test_file_fli.py | 23 ++++++ test/test_image_getbbox.py | 45 +++++++++++ test/test_image_getpixel.py | 54 +++++++++++++ test/test_imagefont.py | 145 +++++++++++++++++++++++++++++++++++ 9 files changed, 290 insertions(+), 257 deletions(-) delete mode 100644 Tests/test_file_fli.py delete mode 100644 Tests/test_image_getbbox.py delete mode 100644 Tests/test_image_getpixel.py delete mode 100644 Tests/test_imagefont.py create mode 100644 test/test_file_fli.py create mode 100644 test/test_image_getbbox.py create mode 100644 test/test_image_getpixel.py create mode 100644 test/test_imagefont.py diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py deleted file mode 100644 index 4e06a732e..000000000 --- a/Tests/test_file_fli.py +++ /dev/null @@ -1,14 +0,0 @@ -from tester import * - -from PIL import Image - -# sample ppm stream -file = "Images/lena.fli" -data = open(file, "rb").read() - -def test_sanity(): - im = Image.open(file) - im.load() - assert_equal(im.mode, "P") - assert_equal(im.size, (128, 128)) - assert_equal(im.format, "FLI") diff --git a/Tests/test_image_getbbox.py b/Tests/test_image_getbbox.py deleted file mode 100644 index c0f846169..000000000 --- a/Tests/test_image_getbbox.py +++ /dev/null @@ -1,36 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - - bbox = lena().getbbox() - assert_true(isinstance(bbox, tuple)) - -def test_bbox(): - - # 8-bit mode - im = Image.new("L", (100, 100), 0) - assert_equal(im.getbbox(), None) - - im.paste(255, (10, 25, 90, 75)) - assert_equal(im.getbbox(), (10, 25, 90, 75)) - - im.paste(255, (25, 10, 75, 90)) - assert_equal(im.getbbox(), (10, 10, 90, 90)) - - im.paste(255, (-10, -10, 110, 110)) - assert_equal(im.getbbox(), (0, 0, 100, 100)) - - # 32-bit mode - im = Image.new("RGB", (100, 100), 0) - assert_equal(im.getbbox(), None) - - im.paste(255, (10, 25, 90, 75)) - assert_equal(im.getbbox(), (10, 25, 90, 75)) - - im.paste(255, (25, 10, 75, 90)) - assert_equal(im.getbbox(), (10, 10, 90, 90)) - - im.paste(255, (-10, -10, 110, 110)) - assert_equal(im.getbbox(), (0, 0, 100, 100)) diff --git a/Tests/test_image_getpixel.py b/Tests/test_image_getpixel.py deleted file mode 100644 index da3d8115e..000000000 --- a/Tests/test_image_getpixel.py +++ /dev/null @@ -1,49 +0,0 @@ -from tester import * - -from PIL import Image - -Image.USE_CFFI_ACCESS=False - -def color(mode): - bands = Image.getmodebands(mode) - if bands == 1: - return 1 - else: - return tuple(range(1, bands+1)) - - - -def check(mode, c=None): - if not c: - c = color(mode) - - #check putpixel - im = Image.new(mode, (1, 1), None) - im.putpixel((0, 0), c) - assert_equal(im.getpixel((0, 0)), c, - "put/getpixel roundtrip failed for mode %s, color %s" % - (mode, c)) - - # check inital color - im = Image.new(mode, (1, 1), c) - assert_equal(im.getpixel((0, 0)), c, - "initial color failed for mode %s, color %s " % - (mode, color)) - -def test_basic(): - for mode in ("1", "L", "LA", "I", "I;16", "I;16B", "F", - "P", "PA", "RGB", "RGBA", "RGBX", "CMYK","YCbCr"): - check(mode) - -def test_signedness(): - # see https://github.com/python-pillow/Pillow/issues/452 - # pixelaccess is using signed int* instead of uint* - for mode in ("I;16", "I;16B"): - check(mode, 2**15-1) - check(mode, 2**15) - check(mode, 2**15+1) - check(mode, 2**16-1) - - - - diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py deleted file mode 100644 index 9ac2cdd89..000000000 --- a/Tests/test_imagefont.py +++ /dev/null @@ -1,135 +0,0 @@ -from tester import * - -from PIL import Image -from io import BytesIO -import os - -try: - from PIL import ImageFont - ImageFont.core.getfont # check if freetype is available -except ImportError: - skip() - -from PIL import ImageDraw - -font_path = "Tests/fonts/FreeMono.ttf" -font_size=20 - -def test_sanity(): - assert_match(ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$") - -def test_font_with_name(): - assert_no_exception(lambda: ImageFont.truetype(font_path, font_size)) - assert_no_exception(lambda: _render(font_path)) - _clean() - -def _font_as_bytes(): - with open(font_path, 'rb') as f: - font_bytes = BytesIO(f.read()) - return font_bytes - -def test_font_with_filelike(): - assert_no_exception(lambda: ImageFont.truetype(_font_as_bytes(), font_size)) - assert_no_exception(lambda: _render(_font_as_bytes())) - # Usage note: making two fonts from the same buffer fails. - #shared_bytes = _font_as_bytes() - #assert_no_exception(lambda: _render(shared_bytes)) - #assert_exception(Exception, lambda: _render(shared_bytes)) - _clean() - -def test_font_with_open_file(): - with open(font_path, 'rb') as f: - assert_no_exception(lambda: _render(f)) - _clean() - -def test_font_old_parameters(): - assert_warning(DeprecationWarning, lambda: ImageFont.truetype(filename=font_path, size=font_size)) - -def _render(font): - txt = "Hello World!" - ttf = ImageFont.truetype(font, font_size) - w, h = ttf.getsize(txt) - img = Image.new("RGB", (256, 64), "white") - d = ImageDraw.Draw(img) - d.text((10, 10), txt, font=ttf, fill='black') - - img.save('font.png') - return img - -def _clean(): - os.unlink('font.png') - -def test_render_equal(): - img_path = _render(font_path) - with open(font_path, 'rb') as f: - font_filelike = BytesIO(f.read()) - img_filelike = _render(font_filelike) - - assert_image_equal(img_path, img_filelike) - _clean() - - -def test_render_multiline(): - im = Image.new(mode='RGB', size=(300,100)) - draw = ImageDraw.Draw(im) - ttf = ImageFont.truetype(font_path, font_size) - line_spacing = draw.textsize('A', font=ttf)[1] + 8 - lines = ['hey you', 'you are awesome', 'this looks awkward'] - y = 0 - for line in lines: - draw.text((0, y), line, font=ttf) - y += line_spacing - - - target = 'Tests/images/multiline_text.png' - target_img = Image.open(target) - - # some versions of freetype have different horizontal spacing. - # setting a tight epsilon, I'm showing the original test failure - # at epsilon = ~38. - assert_image_similar(im, target_img,.5) - - -def test_rotated_transposed_font(): - img_grey = Image.new("L", (100, 100)) - draw = ImageDraw.Draw(img_grey) - word = "testing" - font = ImageFont.truetype(font_path, font_size) - - orientation = Image.ROTATE_90 - transposed_font = ImageFont.TransposedFont(font, orientation=orientation) - - # Original font - draw.setfont(font) - box_size_a = draw.textsize(word) - - # Rotated font - draw.setfont(transposed_font) - box_size_b = draw.textsize(word) - - # Check (w,h) of box a is (h,w) of box b - assert_equal(box_size_a[0], box_size_b[1]) - assert_equal(box_size_a[1], box_size_b[0]) - - -def test_unrotated_transposed_font(): - img_grey = Image.new("L", (100, 100)) - draw = ImageDraw.Draw(img_grey) - word = "testing" - font = ImageFont.truetype(font_path, font_size) - - orientation = None - transposed_font = ImageFont.TransposedFont(font, orientation=orientation) - - # Original font - draw.setfont(font) - box_size_a = draw.textsize(word) - - # Rotated font - draw.setfont(transposed_font) - box_size_b = draw.textsize(word) - - # Check boxes a and b are same size - assert_equal(box_size_a, box_size_b) - - diff --git a/test/helper.py b/test/helper.py index 0958c56a9..e6684b845 100644 --- a/test/helper.py +++ b/test/helper.py @@ -34,6 +34,29 @@ class PillowTestCase(unittest.TestCase): a.tobytes(), b.tobytes(), msg or "got different content") + def assert_image_similar(self, a, b, epsilon, msg=None): + epsilon = float(epsilon) + self.assertEqual( + a.mode, b.mode, + msg or "got mode %r, expected %r" % (a.mode, b.mode)) + self.assertEqual( + a.size, b.size, + msg or "got size %r, expected %r" % (a.size, b.size)) + + diff = 0 + try: + ord(b'0') + for abyte, bbyte in zip(a.tobytes(), b.tobytes()): + diff += abs(ord(abyte)-ord(bbyte)) + except: + for abyte, bbyte in zip(a.tobytes(), b.tobytes()): + diff += abs(abyte-bbyte) + ave_diff = float(diff)/(a.size[0]*a.size[1]) + self.assertGreaterEqual( + epsilon, ave_diff, + msg or "average pixel value difference %.4f > epsilon %.4f" % ( + ave_diff, epsilon)) + def assert_warning(self, warn_class, func): import warnings @@ -169,29 +192,6 @@ def lena(mode="RGB", cache={}): # success() # # -# def assert_image_similar(a, b, epsilon, msg=None): -# epsilon = float(epsilon) -# if a.mode != b.mode: -# return failure(msg or "got mode %r, expected %r" % (a.mode, b.mode)) -# elif a.size != b.size: -# return failure(msg or "got size %r, expected %r" % (a.size, b.size)) -# diff = 0 -# try: -# ord(b'0') -# for abyte, bbyte in zip(a.tobytes(), b.tobytes()): -# diff += abs(ord(abyte)-ord(bbyte)) -# except: -# for abyte, bbyte in zip(a.tobytes(), b.tobytes()): -# diff += abs(abyte-bbyte) -# ave_diff = float(diff)/(a.size[0]*a.size[1]) -# if epsilon < ave_diff: -# return failure( -# msg or "average pixel value difference %.4f > epsilon %.4f" % ( -# ave_diff, epsilon)) -# else: -# return success() -# -# # def tempfile(template, *extra): # import os # import os.path diff --git a/test/test_file_fli.py b/test/test_file_fli.py new file mode 100644 index 000000000..dd22a58f9 --- /dev/null +++ b/test/test_file_fli.py @@ -0,0 +1,23 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + +# sample ppm stream +file = "Images/lena.fli" +data = open(file, "rb").read() + + +class TestImage(PillowTestCase): + + def test_sanity(self): + im = Image.open(file) + im.load() + self.assertEqual(im.mode, "P") + self.assertEqual(im.size, (128, 128)) + self.assertEqual(im.format, "FLI") + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_getbbox.py b/test/test_image_getbbox.py new file mode 100644 index 000000000..83a6a3dec --- /dev/null +++ b/test/test_image_getbbox.py @@ -0,0 +1,45 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImage(PillowTestCase): + + def test_sanity(self): + + bbox = lena().getbbox() + self.assertIsInstance(bbox, tuple) + + def test_bbox(self): + + # 8-bit mode + im = Image.new("L", (100, 100), 0) + self.assertEqual(im.getbbox(), None) + + im.paste(255, (10, 25, 90, 75)) + self.assertEqual(im.getbbox(), (10, 25, 90, 75)) + + im.paste(255, (25, 10, 75, 90)) + self.assertEqual(im.getbbox(), (10, 10, 90, 90)) + + im.paste(255, (-10, -10, 110, 110)) + self.assertEqual(im.getbbox(), (0, 0, 100, 100)) + + # 32-bit mode + im = Image.new("RGB", (100, 100), 0) + self.assertEqual(im.getbbox(), None) + + im.paste(255, (10, 25, 90, 75)) + self.assertEqual(im.getbbox(), (10, 25, 90, 75)) + + im.paste(255, (25, 10, 75, 90)) + self.assertEqual(im.getbbox(), (10, 10, 90, 90)) + + im.paste(255, (-10, -10, 110, 110)) + self.assertEqual(im.getbbox(), (0, 0, 100, 100)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_getpixel.py b/test/test_image_getpixel.py new file mode 100644 index 000000000..de5f185ec --- /dev/null +++ b/test/test_image_getpixel.py @@ -0,0 +1,54 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + +Image.USE_CFFI_ACCESS = False + + +class TestImage(PillowTestCase): + + def color(self, mode): + bands = Image.getmodebands(mode) + if bands == 1: + return 1 + else: + return tuple(range(1, bands+1)) + + def check(self, mode, c=None): + if not c: + c = self.color(mode) + + # check putpixel + im = Image.new(mode, (1, 1), None) + im.putpixel((0, 0), c) + self.assertEqual( + im.getpixel((0, 0)), c, + "put/getpixel roundtrip failed for mode %s, color %s" % + (mode, c)) + + # check inital color + im = Image.new(mode, (1, 1), c) + self.assertEqual( + im.getpixel((0, 0)), c, + "initial color failed for mode %s, color %s " % + (mode, self.color)) + + def test_basic(self): + for mode in ("1", "L", "LA", "I", "I;16", "I;16B", "F", + "P", "PA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"): + self.check(mode) + + def test_signedness(self): + # see https://github.com/python-pillow/Pillow/issues/452 + # pixelaccess is using signed int* instead of uint* + for mode in ("I;16", "I;16B"): + self.check(mode, 2**15-1) + self.check(mode, 2**15) + self.check(mode, 2**15+1) + self.check(mode, 2**16-1) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagefont.py b/test/test_imagefont.py new file mode 100644 index 000000000..17cb38cc2 --- /dev/null +++ b/test/test_imagefont.py @@ -0,0 +1,145 @@ +from helper import unittest, PillowTestCase + +from PIL import Image +from PIL import ImageDraw +from io import BytesIO +import os + +font_path = "Tests/fonts/FreeMono.ttf" +font_size = 20 + + +try: + from PIL import ImageFont + ImageFont.core.getfont # check if freetype is available + + class TestImageFont(PillowTestCase): + + def test_sanity(self): + self.assertRegexpMatches( + ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$") + + def test_font_with_name(self): + ImageFont.truetype(font_path, font_size) + self._render(font_path) + self._clean() + + def _font_as_bytes(self): + with open(font_path, 'rb') as f: + font_bytes = BytesIO(f.read()) + return font_bytes + + def test_font_with_filelike(self): + ImageFont.truetype(self._font_as_bytes(), font_size) + self._render(self._font_as_bytes()) + # Usage note: making two fonts from the same buffer fails. + # shared_bytes = self._font_as_bytes() + # self._render(shared_bytes) + # self.assertRaises(Exception, lambda: _render(shared_bytes)) + self._clean() + + def test_font_with_open_file(self): + with open(font_path, 'rb') as f: + self._render(f) + self._clean() + + def test_font_old_parameters(self): + self.assert_warning( + DeprecationWarning, + lambda: ImageFont.truetype(filename=font_path, size=font_size)) + + def _render(self, font): + txt = "Hello World!" + ttf = ImageFont.truetype(font, font_size) + w, h = ttf.getsize(txt) + img = Image.new("RGB", (256, 64), "white") + d = ImageDraw.Draw(img) + d.text((10, 10), txt, font=ttf, fill='black') + + img.save('font.png') + return img + + def _clean(self): + os.unlink('font.png') + + def test_render_equal(self): + img_path = self._render(font_path) + with open(font_path, 'rb') as f: + font_filelike = BytesIO(f.read()) + img_filelike = self._render(font_filelike) + + self.assert_image_equal(img_path, img_filelike) + self._clean() + + def test_render_multiline(self): + im = Image.new(mode='RGB', size=(300, 100)) + draw = ImageDraw.Draw(im) + ttf = ImageFont.truetype(font_path, font_size) + line_spacing = draw.textsize('A', font=ttf)[1] + 8 + lines = ['hey you', 'you are awesome', 'this looks awkward'] + y = 0 + for line in lines: + draw.text((0, y), line, font=ttf) + y += line_spacing + + target = 'Tests/images/multiline_text.png' + target_img = Image.open(target) + + # some versions of freetype have different horizontal spacing. + # setting a tight epsilon, I'm showing the original test failure + # at epsilon = ~38. + self.assert_image_similar(im, target_img, .5) + + def test_rotated_transposed_font(self): + img_grey = Image.new("L", (100, 100)) + draw = ImageDraw.Draw(img_grey) + word = "testing" + font = ImageFont.truetype(font_path, font_size) + + orientation = Image.ROTATE_90 + transposed_font = ImageFont.TransposedFont( + font, orientation=orientation) + + # Original font + draw.setfont(font) + box_size_a = draw.textsize(word) + + # Rotated font + draw.setfont(transposed_font) + box_size_b = draw.textsize(word) + + # Check (w,h) of box a is (h,w) of box b + self.assertEqual(box_size_a[0], box_size_b[1]) + self.assertEqual(box_size_a[1], box_size_b[0]) + + def test_unrotated_transposed_font(self): + img_grey = Image.new("L", (100, 100)) + draw = ImageDraw.Draw(img_grey) + word = "testing" + font = ImageFont.truetype(font_path, font_size) + + orientation = None + transposed_font = ImageFont.TransposedFont( + font, orientation=orientation) + + # Original font + draw.setfont(font) + box_size_a = draw.textsize(word) + + # Rotated font + draw.setfont(transposed_font) + box_size_b = draw.textsize(word) + + # Check boxes a and b are same size + self.assertEqual(box_size_a, box_size_b) + +except ImportError: + class TestImageFont(PillowTestCase): + def test_skip(self): + self.skipTest("ImportError") + + +if __name__ == '__main__': + unittest.main() + +# End of file From 3249d8f3a5728b90ac961f19dc04381d1b390ca2 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 14:01:57 +0300 Subject: [PATCH 16/33] Convert test_image_putpixel.py. More changes needed for test_cffi.py --- Tests/test_cffi.py | 99 ------------------------------ Tests/test_image_putpalette.py | 28 --------- Tests/test_image_putpixel.py | 45 -------------- test/test_cffi.py | 109 +++++++++++++++++++++++++++++++++ test/test_image_putpalette.py | 36 +++++++++++ test/test_image_putpixel.py | 50 +++++++++++++++ 6 files changed, 195 insertions(+), 172 deletions(-) delete mode 100644 Tests/test_cffi.py delete mode 100644 Tests/test_image_putpalette.py delete mode 100644 Tests/test_image_putpixel.py create mode 100644 test/test_cffi.py create mode 100644 test/test_image_putpalette.py create mode 100644 test/test_image_putpixel.py diff --git a/Tests/test_cffi.py b/Tests/test_cffi.py deleted file mode 100644 index 1c0d8d31e..000000000 --- a/Tests/test_cffi.py +++ /dev/null @@ -1,99 +0,0 @@ -from tester import * - -try: - import cffi -except: - skip() - -from PIL import Image, PyAccess - -import test_image_putpixel as put -import test_image_getpixel as get - - -Image.USE_CFFI_ACCESS = True - -def test_put(): - put.test_sanity() - -def test_get(): - get.test_basic() - get.test_signedness() - -def _test_get_access(im): - """ Do we get the same thing as the old pixel access """ - - """ Using private interfaces, forcing a capi access and a pyaccess for the same image """ - caccess = im.im.pixel_access(False) - access = PyAccess.new(im, False) - - w,h = im.size - for x in range(0,w,10): - for y in range(0,h,10): - assert_equal(access[(x,y)], caccess[(x,y)]) - -def test_get_vs_c(): - _test_get_access(lena('RGB')) - _test_get_access(lena('RGBA')) - _test_get_access(lena('L')) - _test_get_access(lena('LA')) - _test_get_access(lena('1')) - _test_get_access(lena('P')) - #_test_get_access(lena('PA')) # PA -- how do I make a PA image??? - _test_get_access(lena('F')) - - im = Image.new('I;16', (10,10), 40000) - _test_get_access(im) - im = Image.new('I;16L', (10,10), 40000) - _test_get_access(im) - im = Image.new('I;16B', (10,10), 40000) - _test_get_access(im) - - im = Image.new('I', (10,10), 40000) - _test_get_access(im) - # These don't actually appear to be modes that I can actually make, - # as unpack sets them directly into the I mode. - #im = Image.new('I;32L', (10,10), -2**10) - #_test_get_access(im) - #im = Image.new('I;32B', (10,10), 2**10) - #_test_get_access(im) - - - -def _test_set_access(im, color): - """ Are we writing the correct bits into the image? """ - - """ Using private interfaces, forcing a capi access and a pyaccess for the same image """ - caccess = im.im.pixel_access(False) - access = PyAccess.new(im, False) - - w,h = im.size - for x in range(0,w,10): - for y in range(0,h,10): - access[(x,y)] = color - assert_equal(color, caccess[(x,y)]) - -def test_set_vs_c(): - _test_set_access(lena('RGB'), (255, 128,0) ) - _test_set_access(lena('RGBA'), (255, 192, 128, 0)) - _test_set_access(lena('L'), 128) - _test_set_access(lena('LA'), (128,128)) - _test_set_access(lena('1'), 255) - _test_set_access(lena('P') , 128) - ##_test_set_access(i, (128,128)) #PA -- undone how to make - _test_set_access(lena('F'), 1024.0) - - im = Image.new('I;16', (10,10), 40000) - _test_set_access(im, 45000) - im = Image.new('I;16L', (10,10), 40000) - _test_set_access(im, 45000) - im = Image.new('I;16B', (10,10), 40000) - _test_set_access(im, 45000) - - - im = Image.new('I', (10,10), 40000) - _test_set_access(im, 45000) -# im = Image.new('I;32L', (10,10), -(2**10)) -# _test_set_access(im, -(2**13)+1) - #im = Image.new('I;32B', (10,10), 2**10) - #_test_set_access(im, 2**13-1) diff --git a/Tests/test_image_putpalette.py b/Tests/test_image_putpalette.py deleted file mode 100644 index b7ebb8853..000000000 --- a/Tests/test_image_putpalette.py +++ /dev/null @@ -1,28 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImagePalette - -def test_putpalette(): - def palette(mode): - im = lena(mode).copy() - im.putpalette(list(range(256))*3) - p = im.getpalette() - if p: - return im.mode, p[:10] - return im.mode - assert_exception(ValueError, lambda: palette("1")) - assert_equal(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) - assert_equal(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) - assert_exception(ValueError, lambda: palette("I")) - assert_exception(ValueError, lambda: palette("F")) - assert_exception(ValueError, lambda: palette("RGB")) - assert_exception(ValueError, lambda: palette("RGBA")) - assert_exception(ValueError, lambda: palette("YCbCr")) - -def test_imagepalette(): - im = lena("P") - assert_no_exception(lambda: im.putpalette(ImagePalette.negative())) - assert_no_exception(lambda: im.putpalette(ImagePalette.random())) - assert_no_exception(lambda: im.putpalette(ImagePalette.sepia())) - assert_no_exception(lambda: im.putpalette(ImagePalette.wedge())) diff --git a/Tests/test_image_putpixel.py b/Tests/test_image_putpixel.py deleted file mode 100644 index 5f19237cb..000000000 --- a/Tests/test_image_putpixel.py +++ /dev/null @@ -1,45 +0,0 @@ -from tester import * - -from PIL import Image - -Image.USE_CFFI_ACCESS=False - -def test_sanity(): - - im1 = lena() - im2 = Image.new(im1.mode, im1.size, 0) - - for y in range(im1.size[1]): - for x in range(im1.size[0]): - pos = x, y - im2.putpixel(pos, im1.getpixel(pos)) - - assert_image_equal(im1, im2) - - im2 = Image.new(im1.mode, im1.size, 0) - im2.readonly = 1 - - for y in range(im1.size[1]): - for x in range(im1.size[0]): - pos = x, y - im2.putpixel(pos, im1.getpixel(pos)) - - assert_false(im2.readonly) - assert_image_equal(im1, im2) - - im2 = Image.new(im1.mode, im1.size, 0) - - pix1 = im1.load() - pix2 = im2.load() - - for y in range(im1.size[1]): - for x in range(im1.size[0]): - pix2[x, y] = pix1[x, y] - - assert_image_equal(im1, im2) - - - - -# see test_image_getpixel for more tests - diff --git a/test/test_cffi.py b/test/test_cffi.py new file mode 100644 index 000000000..1492af32d --- /dev/null +++ b/test/test_cffi.py @@ -0,0 +1,109 @@ +from helper import unittest, PillowTestCase, lena + +try: + import cffi + + from PIL import Image, PyAccess + + import test_image_putpixel as put + import test_image_getpixel as get + + class TestCffi(PillowTestCase): + + Image.USE_CFFI_ACCESS = True + + def test_put(self): + put.test_sanity() + + def test_get(self): + get.test_basic() + get.test_signedness() + + def _test_get_access(self, im): + """ Do we get the same thing as the old pixel access """ + + """ Using private interfaces, forcing a capi access and a pyaccess + for the same image """ + caccess = im.im.pixel_access(False) + access = PyAccess.new(im, False) + + w, h = im.size + for x in range(0, w, 10): + for y in range(0, h, 10): + self.assertEqual(access[(x, y)], caccess[(x, y)]) + + def test_get_vs_c(self): + self._test_get_access(lena('RGB')) + self._test_get_access(lena('RGBA')) + self._test_get_access(lena('L')) + self._test_get_access(lena('LA')) + self._test_get_access(lena('1')) + self._test_get_access(lena('P')) + # PA -- how do I make a PA image??? + # self._test_get_access(lena('PA')) + self._test_get_access(lena('F')) + + im = Image.new('I;16', (10, 10), 40000) + self._test_get_access(im) + im = Image.new('I;16L', (10, 10), 40000) + self._test_get_access(im) + im = Image.new('I;16B', (10, 10), 40000) + self._test_get_access(im) + + im = Image.new('I', (10, 10), 40000) + self._test_get_access(im) + # These don't actually appear to be modes that I can actually make, + # as unpack sets them directly into the I mode. + # im = Image.new('I;32L', (10, 10), -2**10) + # self._test_get_access(im) + # im = Image.new('I;32B', (10, 10), 2**10) + # self._test_get_access(im) + + def _test_set_access(self, im, color): + """ Are we writing the correct bits into the image? """ + + """ Using private interfaces, forcing a capi access and a pyaccess + for the same image """ + caccess = im.im.pixel_access(False) + access = PyAccess.new(im, False) + + w, h = im.size + for x in range(0, w, 10): + for y in range(0, h, 10): + access[(x, y)] = color + self.assertEqual(color, caccess[(x, y)]) + + def test_set_vs_c(self): + self._test_set_access(lena('RGB'), (255, 128, 0)) + self._test_set_access(lena('RGBA'), (255, 192, 128, 0)) + self._test_set_access(lena('L'), 128) + self._test_set_access(lena('LA'), (128, 128)) + self._test_set_access(lena('1'), 255) + self._test_set_access(lena('P'), 128) + # self._test_set_access(i, (128, 128)) #PA -- undone how to make + self._test_set_access(lena('F'), 1024.0) + + im = Image.new('I;16', (10, 10), 40000) + self._test_set_access(im, 45000) + im = Image.new('I;16L', (10, 10), 40000) + self._test_set_access(im, 45000) + im = Image.new('I;16B', (10, 10), 40000) + self._test_set_access(im, 45000) + + im = Image.new('I', (10, 10), 40000) + self._test_set_access(im, 45000) + # im = Image.new('I;32L', (10, 10), -(2**10)) + # self._test_set_access(im, -(2**13)+1) + # im = Image.new('I;32B', (10, 10), 2**10) + # self._test_set_access(im, 2**13-1) + +except ImportError: + class TestCffi(PillowTestCase): + def test_skip(self): + self.skipTest("ImportError") + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_putpalette.py b/test/test_image_putpalette.py new file mode 100644 index 000000000..b77dcbf00 --- /dev/null +++ b/test/test_image_putpalette.py @@ -0,0 +1,36 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import ImagePalette + + +class TestImage(PillowTestCase): + + def test_putpalette(self): + def palette(mode): + im = lena(mode).copy() + im.putpalette(list(range(256))*3) + p = im.getpalette() + if p: + return im.mode, p[:10] + return im.mode + self.assertRaises(ValueError, lambda: palette("1")) + self.assertEqual(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) + self.assertEqual(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) + self.assertRaises(ValueError, lambda: palette("I")) + self.assertRaises(ValueError, lambda: palette("F")) + self.assertRaises(ValueError, lambda: palette("RGB")) + self.assertRaises(ValueError, lambda: palette("RGBA")) + self.assertRaises(ValueError, lambda: palette("YCbCr")) + + def test_imagepalette(self): + im = lena("P") + im.putpalette(ImagePalette.negative()) + im.putpalette(ImagePalette.random()) + im.putpalette(ImagePalette.sepia()) + im.putpalette(ImagePalette.wedge()) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_putpixel.py b/test/test_image_putpixel.py new file mode 100644 index 000000000..a7f5dc2bb --- /dev/null +++ b/test/test_image_putpixel.py @@ -0,0 +1,50 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + +Image.USE_CFFI_ACCESS = False + + +class TestImagePutPixel(PillowTestCase): + + def test_sanity(self): + + im1 = lena() + im2 = Image.new(im1.mode, im1.size, 0) + + for y in range(im1.size[1]): + for x in range(im1.size[0]): + pos = x, y + im2.putpixel(pos, im1.getpixel(pos)) + + self.assert_image_equal(im1, im2) + + im2 = Image.new(im1.mode, im1.size, 0) + im2.readonly = 1 + + for y in range(im1.size[1]): + for x in range(im1.size[0]): + pos = x, y + im2.putpixel(pos, im1.getpixel(pos)) + + self.assertFalse(im2.readonly) + self.assert_image_equal(im1, im2) + + im2 = Image.new(im1.mode, im1.size, 0) + + pix1 = im1.load() + pix2 = im2.load() + + for y in range(im1.size[1]): + for x in range(im1.size[0]): + pix2[x, y] = pix1[x, y] + + self.assert_image_equal(im1, im2) + + # see test_image_getpixel for more tests + + +if __name__ == '__main__': + unittest.main() + +# End of file From 2833cb42b45df2025bc408584e8cd8c828f3a6fa Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 14:19:29 +0300 Subject: [PATCH 17/33] Fixes for test_cffi.py --- test/test_cffi.py | 18 +++++++++++------- test/test_image_getpixel.py | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/test/test_cffi.py b/test/test_cffi.py index 1492af32d..638386ac4 100644 --- a/test/test_cffi.py +++ b/test/test_cffi.py @@ -5,19 +5,23 @@ try: from PIL import Image, PyAccess - import test_image_putpixel as put - import test_image_getpixel as get + from test_image_putpixel import TestImagePutPixel + from test_image_getpixel import TestImageGetPixel - class TestCffi(PillowTestCase): + Image.USE_CFFI_ACCESS = True - Image.USE_CFFI_ACCESS = True + class TestCffiPutPixel(TestImagePutPixel): def test_put(self): - put.test_sanity() + self.test_sanity() + + class TestCffiGetPixel(TestImageGetPixel): def test_get(self): - get.test_basic() - get.test_signedness() + self.test_basic() + self.test_signedness() + + class TestCffi(PillowTestCase): def _test_get_access(self, im): """ Do we get the same thing as the old pixel access """ diff --git a/test/test_image_getpixel.py b/test/test_image_getpixel.py index de5f185ec..9749ba836 100644 --- a/test/test_image_getpixel.py +++ b/test/test_image_getpixel.py @@ -5,7 +5,7 @@ from PIL import Image Image.USE_CFFI_ACCESS = False -class TestImage(PillowTestCase): +class TestImageGetPixel(PillowTestCase): def color(self, mode): bands = Image.getmodebands(mode) From 07aa1a56bbddd79128a81584eb0ec8f903c5192f Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 15:31:22 +0300 Subject: [PATCH 18/33] Save test_cffi for another day --- Tests/test_cffi.py | 99 ++++++++++++++++++++++++++++++ Tests/test_image_getpixel.py | 49 +++++++++++++++ Tests/test_image_putpixel.py | 45 ++++++++++++++ test/test_cffi.py | 113 ----------------------------------- test/test_image_getpixel.py | 54 ----------------- test/test_image_putpixel.py | 50 ---------------- 6 files changed, 193 insertions(+), 217 deletions(-) create mode 100644 Tests/test_cffi.py create mode 100644 Tests/test_image_getpixel.py create mode 100644 Tests/test_image_putpixel.py delete mode 100644 test/test_cffi.py delete mode 100644 test/test_image_getpixel.py delete mode 100644 test/test_image_putpixel.py diff --git a/Tests/test_cffi.py b/Tests/test_cffi.py new file mode 100644 index 000000000..1c0d8d31e --- /dev/null +++ b/Tests/test_cffi.py @@ -0,0 +1,99 @@ +from tester import * + +try: + import cffi +except: + skip() + +from PIL import Image, PyAccess + +import test_image_putpixel as put +import test_image_getpixel as get + + +Image.USE_CFFI_ACCESS = True + +def test_put(): + put.test_sanity() + +def test_get(): + get.test_basic() + get.test_signedness() + +def _test_get_access(im): + """ Do we get the same thing as the old pixel access """ + + """ Using private interfaces, forcing a capi access and a pyaccess for the same image """ + caccess = im.im.pixel_access(False) + access = PyAccess.new(im, False) + + w,h = im.size + for x in range(0,w,10): + for y in range(0,h,10): + assert_equal(access[(x,y)], caccess[(x,y)]) + +def test_get_vs_c(): + _test_get_access(lena('RGB')) + _test_get_access(lena('RGBA')) + _test_get_access(lena('L')) + _test_get_access(lena('LA')) + _test_get_access(lena('1')) + _test_get_access(lena('P')) + #_test_get_access(lena('PA')) # PA -- how do I make a PA image??? + _test_get_access(lena('F')) + + im = Image.new('I;16', (10,10), 40000) + _test_get_access(im) + im = Image.new('I;16L', (10,10), 40000) + _test_get_access(im) + im = Image.new('I;16B', (10,10), 40000) + _test_get_access(im) + + im = Image.new('I', (10,10), 40000) + _test_get_access(im) + # These don't actually appear to be modes that I can actually make, + # as unpack sets them directly into the I mode. + #im = Image.new('I;32L', (10,10), -2**10) + #_test_get_access(im) + #im = Image.new('I;32B', (10,10), 2**10) + #_test_get_access(im) + + + +def _test_set_access(im, color): + """ Are we writing the correct bits into the image? """ + + """ Using private interfaces, forcing a capi access and a pyaccess for the same image """ + caccess = im.im.pixel_access(False) + access = PyAccess.new(im, False) + + w,h = im.size + for x in range(0,w,10): + for y in range(0,h,10): + access[(x,y)] = color + assert_equal(color, caccess[(x,y)]) + +def test_set_vs_c(): + _test_set_access(lena('RGB'), (255, 128,0) ) + _test_set_access(lena('RGBA'), (255, 192, 128, 0)) + _test_set_access(lena('L'), 128) + _test_set_access(lena('LA'), (128,128)) + _test_set_access(lena('1'), 255) + _test_set_access(lena('P') , 128) + ##_test_set_access(i, (128,128)) #PA -- undone how to make + _test_set_access(lena('F'), 1024.0) + + im = Image.new('I;16', (10,10), 40000) + _test_set_access(im, 45000) + im = Image.new('I;16L', (10,10), 40000) + _test_set_access(im, 45000) + im = Image.new('I;16B', (10,10), 40000) + _test_set_access(im, 45000) + + + im = Image.new('I', (10,10), 40000) + _test_set_access(im, 45000) +# im = Image.new('I;32L', (10,10), -(2**10)) +# _test_set_access(im, -(2**13)+1) + #im = Image.new('I;32B', (10,10), 2**10) + #_test_set_access(im, 2**13-1) diff --git a/Tests/test_image_getpixel.py b/Tests/test_image_getpixel.py new file mode 100644 index 000000000..da3d8115e --- /dev/null +++ b/Tests/test_image_getpixel.py @@ -0,0 +1,49 @@ +from tester import * + +from PIL import Image + +Image.USE_CFFI_ACCESS=False + +def color(mode): + bands = Image.getmodebands(mode) + if bands == 1: + return 1 + else: + return tuple(range(1, bands+1)) + + + +def check(mode, c=None): + if not c: + c = color(mode) + + #check putpixel + im = Image.new(mode, (1, 1), None) + im.putpixel((0, 0), c) + assert_equal(im.getpixel((0, 0)), c, + "put/getpixel roundtrip failed for mode %s, color %s" % + (mode, c)) + + # check inital color + im = Image.new(mode, (1, 1), c) + assert_equal(im.getpixel((0, 0)), c, + "initial color failed for mode %s, color %s " % + (mode, color)) + +def test_basic(): + for mode in ("1", "L", "LA", "I", "I;16", "I;16B", "F", + "P", "PA", "RGB", "RGBA", "RGBX", "CMYK","YCbCr"): + check(mode) + +def test_signedness(): + # see https://github.com/python-pillow/Pillow/issues/452 + # pixelaccess is using signed int* instead of uint* + for mode in ("I;16", "I;16B"): + check(mode, 2**15-1) + check(mode, 2**15) + check(mode, 2**15+1) + check(mode, 2**16-1) + + + + diff --git a/Tests/test_image_putpixel.py b/Tests/test_image_putpixel.py new file mode 100644 index 000000000..5f19237cb --- /dev/null +++ b/Tests/test_image_putpixel.py @@ -0,0 +1,45 @@ +from tester import * + +from PIL import Image + +Image.USE_CFFI_ACCESS=False + +def test_sanity(): + + im1 = lena() + im2 = Image.new(im1.mode, im1.size, 0) + + for y in range(im1.size[1]): + for x in range(im1.size[0]): + pos = x, y + im2.putpixel(pos, im1.getpixel(pos)) + + assert_image_equal(im1, im2) + + im2 = Image.new(im1.mode, im1.size, 0) + im2.readonly = 1 + + for y in range(im1.size[1]): + for x in range(im1.size[0]): + pos = x, y + im2.putpixel(pos, im1.getpixel(pos)) + + assert_false(im2.readonly) + assert_image_equal(im1, im2) + + im2 = Image.new(im1.mode, im1.size, 0) + + pix1 = im1.load() + pix2 = im2.load() + + for y in range(im1.size[1]): + for x in range(im1.size[0]): + pix2[x, y] = pix1[x, y] + + assert_image_equal(im1, im2) + + + + +# see test_image_getpixel for more tests + diff --git a/test/test_cffi.py b/test/test_cffi.py deleted file mode 100644 index 638386ac4..000000000 --- a/test/test_cffi.py +++ /dev/null @@ -1,113 +0,0 @@ -from helper import unittest, PillowTestCase, lena - -try: - import cffi - - from PIL import Image, PyAccess - - from test_image_putpixel import TestImagePutPixel - from test_image_getpixel import TestImageGetPixel - - Image.USE_CFFI_ACCESS = True - - class TestCffiPutPixel(TestImagePutPixel): - - def test_put(self): - self.test_sanity() - - class TestCffiGetPixel(TestImageGetPixel): - - def test_get(self): - self.test_basic() - self.test_signedness() - - class TestCffi(PillowTestCase): - - def _test_get_access(self, im): - """ Do we get the same thing as the old pixel access """ - - """ Using private interfaces, forcing a capi access and a pyaccess - for the same image """ - caccess = im.im.pixel_access(False) - access = PyAccess.new(im, False) - - w, h = im.size - for x in range(0, w, 10): - for y in range(0, h, 10): - self.assertEqual(access[(x, y)], caccess[(x, y)]) - - def test_get_vs_c(self): - self._test_get_access(lena('RGB')) - self._test_get_access(lena('RGBA')) - self._test_get_access(lena('L')) - self._test_get_access(lena('LA')) - self._test_get_access(lena('1')) - self._test_get_access(lena('P')) - # PA -- how do I make a PA image??? - # self._test_get_access(lena('PA')) - self._test_get_access(lena('F')) - - im = Image.new('I;16', (10, 10), 40000) - self._test_get_access(im) - im = Image.new('I;16L', (10, 10), 40000) - self._test_get_access(im) - im = Image.new('I;16B', (10, 10), 40000) - self._test_get_access(im) - - im = Image.new('I', (10, 10), 40000) - self._test_get_access(im) - # These don't actually appear to be modes that I can actually make, - # as unpack sets them directly into the I mode. - # im = Image.new('I;32L', (10, 10), -2**10) - # self._test_get_access(im) - # im = Image.new('I;32B', (10, 10), 2**10) - # self._test_get_access(im) - - def _test_set_access(self, im, color): - """ Are we writing the correct bits into the image? """ - - """ Using private interfaces, forcing a capi access and a pyaccess - for the same image """ - caccess = im.im.pixel_access(False) - access = PyAccess.new(im, False) - - w, h = im.size - for x in range(0, w, 10): - for y in range(0, h, 10): - access[(x, y)] = color - self.assertEqual(color, caccess[(x, y)]) - - def test_set_vs_c(self): - self._test_set_access(lena('RGB'), (255, 128, 0)) - self._test_set_access(lena('RGBA'), (255, 192, 128, 0)) - self._test_set_access(lena('L'), 128) - self._test_set_access(lena('LA'), (128, 128)) - self._test_set_access(lena('1'), 255) - self._test_set_access(lena('P'), 128) - # self._test_set_access(i, (128, 128)) #PA -- undone how to make - self._test_set_access(lena('F'), 1024.0) - - im = Image.new('I;16', (10, 10), 40000) - self._test_set_access(im, 45000) - im = Image.new('I;16L', (10, 10), 40000) - self._test_set_access(im, 45000) - im = Image.new('I;16B', (10, 10), 40000) - self._test_set_access(im, 45000) - - im = Image.new('I', (10, 10), 40000) - self._test_set_access(im, 45000) - # im = Image.new('I;32L', (10, 10), -(2**10)) - # self._test_set_access(im, -(2**13)+1) - # im = Image.new('I;32B', (10, 10), 2**10) - # self._test_set_access(im, 2**13-1) - -except ImportError: - class TestCffi(PillowTestCase): - def test_skip(self): - self.skipTest("ImportError") - - -if __name__ == '__main__': - unittest.main() - -# End of file diff --git a/test/test_image_getpixel.py b/test/test_image_getpixel.py deleted file mode 100644 index 9749ba836..000000000 --- a/test/test_image_getpixel.py +++ /dev/null @@ -1,54 +0,0 @@ -from helper import unittest, PillowTestCase - -from PIL import Image - -Image.USE_CFFI_ACCESS = False - - -class TestImageGetPixel(PillowTestCase): - - def color(self, mode): - bands = Image.getmodebands(mode) - if bands == 1: - return 1 - else: - return tuple(range(1, bands+1)) - - def check(self, mode, c=None): - if not c: - c = self.color(mode) - - # check putpixel - im = Image.new(mode, (1, 1), None) - im.putpixel((0, 0), c) - self.assertEqual( - im.getpixel((0, 0)), c, - "put/getpixel roundtrip failed for mode %s, color %s" % - (mode, c)) - - # check inital color - im = Image.new(mode, (1, 1), c) - self.assertEqual( - im.getpixel((0, 0)), c, - "initial color failed for mode %s, color %s " % - (mode, self.color)) - - def test_basic(self): - for mode in ("1", "L", "LA", "I", "I;16", "I;16B", "F", - "P", "PA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"): - self.check(mode) - - def test_signedness(self): - # see https://github.com/python-pillow/Pillow/issues/452 - # pixelaccess is using signed int* instead of uint* - for mode in ("I;16", "I;16B"): - self.check(mode, 2**15-1) - self.check(mode, 2**15) - self.check(mode, 2**15+1) - self.check(mode, 2**16-1) - - -if __name__ == '__main__': - unittest.main() - -# End of file diff --git a/test/test_image_putpixel.py b/test/test_image_putpixel.py deleted file mode 100644 index a7f5dc2bb..000000000 --- a/test/test_image_putpixel.py +++ /dev/null @@ -1,50 +0,0 @@ -from helper import unittest, PillowTestCase, lena - -from PIL import Image - -Image.USE_CFFI_ACCESS = False - - -class TestImagePutPixel(PillowTestCase): - - def test_sanity(self): - - im1 = lena() - im2 = Image.new(im1.mode, im1.size, 0) - - for y in range(im1.size[1]): - for x in range(im1.size[0]): - pos = x, y - im2.putpixel(pos, im1.getpixel(pos)) - - self.assert_image_equal(im1, im2) - - im2 = Image.new(im1.mode, im1.size, 0) - im2.readonly = 1 - - for y in range(im1.size[1]): - for x in range(im1.size[0]): - pos = x, y - im2.putpixel(pos, im1.getpixel(pos)) - - self.assertFalse(im2.readonly) - self.assert_image_equal(im1, im2) - - im2 = Image.new(im1.mode, im1.size, 0) - - pix1 = im1.load() - pix2 = im2.load() - - for y in range(im1.size[1]): - for x in range(im1.size[0]): - pix2[x, y] = pix1[x, y] - - self.assert_image_equal(im1, im2) - - # see test_image_getpixel for more tests - - -if __name__ == '__main__': - unittest.main() - -# End of file From 8a870c0ee9af404685c3cbf2787169b00d99d397 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 16:13:58 +0300 Subject: [PATCH 19/33] Convert more tests --- Tests/test_bmp_reference.py | 86 -------------------------------- Tests/test_file_psd.py | 14 ------ Tests/test_image_getcolors.py | 64 ------------------------ Tests/test_image_rotate.py | 15 ------ Tests/test_image_thumbnail.py | 36 -------------- Tests/test_imageops.py | 81 ------------------------------ test/mini.py | 5 ++ test/test_bmp_reference.py | 94 +++++++++++++++++++++++++++++++++++ test/test_file_psd.py | 23 +++++++++ test/test_image_getcolors.py | 74 +++++++++++++++++++++++++++ test/test_image_rotate.py | 22 ++++++++ test/test_image_thumbnail.py | 43 ++++++++++++++++ test/test_imageops.py | 85 +++++++++++++++++++++++++++++++ 13 files changed, 346 insertions(+), 296 deletions(-) delete mode 100644 Tests/test_bmp_reference.py delete mode 100644 Tests/test_file_psd.py delete mode 100644 Tests/test_image_getcolors.py delete mode 100644 Tests/test_image_rotate.py delete mode 100644 Tests/test_image_thumbnail.py delete mode 100644 Tests/test_imageops.py create mode 100644 test/mini.py create mode 100644 test/test_bmp_reference.py create mode 100644 test/test_file_psd.py create mode 100644 test/test_image_getcolors.py create mode 100644 test/test_image_rotate.py create mode 100644 test/test_image_thumbnail.py create mode 100644 test/test_imageops.py diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py deleted file mode 100644 index 99818229f..000000000 --- a/Tests/test_bmp_reference.py +++ /dev/null @@ -1,86 +0,0 @@ -from tester import * - -from PIL import Image -import os - -base = os.path.join('Tests', 'images', 'bmp') - - -def get_files(d, ext='.bmp'): - return [os.path.join(base,d,f) for f - in os.listdir(os.path.join(base, d)) if ext in f] - -def test_bad(): - """ These shouldn't crash/dos, but they shouldn't return anything either """ - for f in get_files('b'): - try: - im = Image.open(f) - im.load() - except Exception as msg: - pass - # print ("Bad Image %s: %s" %(f,msg)) - -def test_questionable(): - """ These shouldn't crash/dos, but its not well defined that these are in spec """ - for f in get_files('q'): - try: - im = Image.open(f) - im.load() - except Exception as msg: - pass - # print ("Bad Image %s: %s" %(f,msg)) - - -def test_good(): - """ These should all work. There's a set of target files in the - html directory that we can compare against. """ - - # Target files, if they're not just replacing the extension - file_map = { 'pal1wb.bmp': 'pal1.png', - 'pal4rle.bmp': 'pal4.png', - 'pal8-0.bmp': 'pal8.png', - 'pal8rle.bmp': 'pal8.png', - 'pal8topdown.bmp': 'pal8.png', - 'pal8nonsquare.bmp': 'pal8nonsquare-v.png', - 'pal8os2.bmp': 'pal8.png', - 'pal8os2sp.bmp': 'pal8.png', - 'pal8os2v2.bmp': 'pal8.png', - 'pal8os2v2-16.bmp': 'pal8.png', - 'pal8v4.bmp': 'pal8.png', - 'pal8v5.bmp': 'pal8.png', - 'rgb16-565pal.bmp': 'rgb16-565.png', - 'rgb24pal.bmp': 'rgb24.png', - 'rgb32.bmp': 'rgb24.png', - 'rgb32bf.bmp': 'rgb24.png' - } - - def get_compare(f): - (head, name) = os.path.split(f) - if name in file_map: - return os.path.join(base, 'html', file_map[name]) - (name,ext) = os.path.splitext(name) - return os.path.join(base, 'html', "%s.png"%name) - - for f in get_files('g'): - try: - im = Image.open(f) - im.load() - compare = Image.open(get_compare(f)) - compare.load() - if im.mode == 'P': - # assert image similar doesn't really work - # with paletized image, since the palette might - # be differently ordered for an equivalent image. - im = im.convert('RGBA') - compare = im.convert('RGBA') - assert_image_similar(im, compare,5) - - - except Exception as msg: - # there are three here that are unsupported: - unsupported = (os.path.join(base, 'g', 'rgb32bf.bmp'), - os.path.join(base, 'g', 'pal8rle.bmp'), - os.path.join(base, 'g', 'pal4rle.bmp')) - if f not in unsupported: - assert_true(False, "Unsupported Image %s: %s" %(f,msg)) - diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py deleted file mode 100644 index ef2d40594..000000000 --- a/Tests/test_file_psd.py +++ /dev/null @@ -1,14 +0,0 @@ -from tester import * - -from PIL import Image - -# sample ppm stream -file = "Images/lena.psd" -data = open(file, "rb").read() - -def test_sanity(): - im = Image.open(file) - im.load() - assert_equal(im.mode, "RGB") - assert_equal(im.size, (128, 128)) - assert_equal(im.format, "PSD") diff --git a/Tests/test_image_getcolors.py b/Tests/test_image_getcolors.py deleted file mode 100644 index 2429f9350..000000000 --- a/Tests/test_image_getcolors.py +++ /dev/null @@ -1,64 +0,0 @@ -from tester import * - -from PIL import Image - -def test_getcolors(): - - def getcolors(mode, limit=None): - im = lena(mode) - if limit: - colors = im.getcolors(limit) - else: - colors = im.getcolors() - if colors: - return len(colors) - return None - - assert_equal(getcolors("1"), 2) - assert_equal(getcolors("L"), 193) - assert_equal(getcolors("I"), 193) - assert_equal(getcolors("F"), 193) - assert_equal(getcolors("P"), 54) # fixed palette - assert_equal(getcolors("RGB"), None) - assert_equal(getcolors("RGBA"), None) - assert_equal(getcolors("CMYK"), None) - assert_equal(getcolors("YCbCr"), None) - - assert_equal(getcolors("L", 128), None) - assert_equal(getcolors("L", 1024), 193) - - assert_equal(getcolors("RGB", 8192), None) - assert_equal(getcolors("RGB", 16384), 14836) - assert_equal(getcolors("RGB", 100000), 14836) - - assert_equal(getcolors("RGBA", 16384), 14836) - assert_equal(getcolors("CMYK", 16384), 14836) - assert_equal(getcolors("YCbCr", 16384), 11995) - -# -------------------------------------------------------------------- - -def test_pack(): - # Pack problems for small tables (@PIL209) - - im = lena().quantize(3).convert("RGB") - - expected = [(3236, (227, 183, 147)), (6297, (143, 84, 81)), (6851, (208, 143, 112))] - - A = im.getcolors(maxcolors=2) - assert_equal(A, None) - - A = im.getcolors(maxcolors=3) - A.sort() - assert_equal(A, expected) - - A = im.getcolors(maxcolors=4) - A.sort() - assert_equal(A, expected) - - A = im.getcolors(maxcolors=8) - A.sort() - assert_equal(A, expected) - - A = im.getcolors(maxcolors=16) - A.sort() - assert_equal(A, expected) diff --git a/Tests/test_image_rotate.py b/Tests/test_image_rotate.py deleted file mode 100644 index 5e4782c87..000000000 --- a/Tests/test_image_rotate.py +++ /dev/null @@ -1,15 +0,0 @@ -from tester import * - -from PIL import Image - -def test_rotate(): - def rotate(mode): - im = lena(mode) - out = im.rotate(45) - assert_equal(out.mode, mode) - assert_equal(out.size, im.size) # default rotate clips output - out = im.rotate(45, expand=1) - assert_equal(out.mode, mode) - assert_true(out.size != im.size) - for mode in "1", "P", "L", "RGB", "I", "F": - yield_test(rotate, mode) diff --git a/Tests/test_image_thumbnail.py b/Tests/test_image_thumbnail.py deleted file mode 100644 index 871dd1f54..000000000 --- a/Tests/test_image_thumbnail.py +++ /dev/null @@ -1,36 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - - im = lena() - im.thumbnail((100, 100)) - - assert_image(im, im.mode, (100, 100)) - -def test_aspect(): - - im = lena() - im.thumbnail((100, 100)) - assert_image(im, im.mode, (100, 100)) - - im = lena().resize((128, 256)) - im.thumbnail((100, 100)) - assert_image(im, im.mode, (50, 100)) - - im = lena().resize((128, 256)) - im.thumbnail((50, 100)) - assert_image(im, im.mode, (50, 100)) - - im = lena().resize((256, 128)) - im.thumbnail((100, 100)) - assert_image(im, im.mode, (100, 50)) - - im = lena().resize((256, 128)) - im.thumbnail((100, 50)) - assert_image(im, im.mode, (100, 50)) - - im = lena().resize((128, 128)) - im.thumbnail((100, 100)) - assert_image(im, im.mode, (100, 100)) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py deleted file mode 100644 index 8ed5ccefa..000000000 --- a/Tests/test_imageops.py +++ /dev/null @@ -1,81 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageOps - -class Deformer: - def getmesh(self, im): - x, y = im.size - return [((0, 0, x, y), (0, 0, x, 0, x, y, y, 0))] - -deformer = Deformer() - -def test_sanity(): - - ImageOps.autocontrast(lena("L")) - ImageOps.autocontrast(lena("RGB")) - - ImageOps.autocontrast(lena("L"), cutoff=10) - ImageOps.autocontrast(lena("L"), ignore=[0, 255]) - - ImageOps.colorize(lena("L"), (0, 0, 0), (255, 255, 255)) - ImageOps.colorize(lena("L"), "black", "white") - - ImageOps.crop(lena("L"), 1) - ImageOps.crop(lena("RGB"), 1) - - ImageOps.deform(lena("L"), deformer) - ImageOps.deform(lena("RGB"), deformer) - - ImageOps.equalize(lena("L")) - ImageOps.equalize(lena("RGB")) - - ImageOps.expand(lena("L"), 1) - ImageOps.expand(lena("RGB"), 1) - ImageOps.expand(lena("L"), 2, "blue") - ImageOps.expand(lena("RGB"), 2, "blue") - - ImageOps.fit(lena("L"), (128, 128)) - ImageOps.fit(lena("RGB"), (128, 128)) - - ImageOps.flip(lena("L")) - ImageOps.flip(lena("RGB")) - - ImageOps.grayscale(lena("L")) - ImageOps.grayscale(lena("RGB")) - - ImageOps.invert(lena("L")) - ImageOps.invert(lena("RGB")) - - ImageOps.mirror(lena("L")) - ImageOps.mirror(lena("RGB")) - - ImageOps.posterize(lena("L"), 4) - ImageOps.posterize(lena("RGB"), 4) - - ImageOps.solarize(lena("L")) - ImageOps.solarize(lena("RGB")) - - success() - -def test_1pxfit(): - # Division by zero in equalize if image is 1 pixel high - newimg = ImageOps.fit(lena("RGB").resize((1,1)), (35,35)) - assert_equal(newimg.size,(35,35)) - - newimg = ImageOps.fit(lena("RGB").resize((1,100)), (35,35)) - assert_equal(newimg.size,(35,35)) - - newimg = ImageOps.fit(lena("RGB").resize((100,1)), (35,35)) - assert_equal(newimg.size,(35,35)) - -def test_pil163(): - # Division by zero in equalize if < 255 pixels in image (@PIL163) - - i = lena("RGB").resize((15, 16)) - - ImageOps.equalize(i.convert("L")) - ImageOps.equalize(i.convert("P")) - ImageOps.equalize(i.convert("RGB")) - - success() diff --git a/test/mini.py b/test/mini.py new file mode 100644 index 000000000..c9f1665ab --- /dev/null +++ b/test/mini.py @@ -0,0 +1,5 @@ +from test_image_putpixel import TestImagePutPixel as put +from test_image_getpixel import TestImageGetPixel as get + +dir(get) +get.test_basic() \ No newline at end of file diff --git a/test/test_bmp_reference.py b/test/test_bmp_reference.py new file mode 100644 index 000000000..ed012e7c8 --- /dev/null +++ b/test/test_bmp_reference.py @@ -0,0 +1,94 @@ +from helper import unittest, PillowTestCase + +from PIL import Image +import os + +base = os.path.join('Tests', 'images', 'bmp') + + +class TestImage(PillowTestCase): + + def get_files(self, d, ext='.bmp'): + return [os.path.join(base, d, f) for f + in os.listdir(os.path.join(base, d)) if ext in f] + + def test_bad(self): + """ These shouldn't crash/dos, but they shouldn't return anything + either """ + for f in self.get_files('b'): + try: + im = Image.open(f) + im.load() + except Exception: # as msg: + pass + # print ("Bad Image %s: %s" %(f,msg)) + + def test_questionable(self): + """ These shouldn't crash/dos, but its not well defined that these + are in spec """ + for f in self.get_files('q'): + try: + im = Image.open(f) + im.load() + except Exception: # as msg: + pass + # print ("Bad Image %s: %s" %(f,msg)) + + def test_good(self): + """ These should all work. There's a set of target files in the + html directory that we can compare against. """ + + # Target files, if they're not just replacing the extension + file_map = {'pal1wb.bmp': 'pal1.png', + 'pal4rle.bmp': 'pal4.png', + 'pal8-0.bmp': 'pal8.png', + 'pal8rle.bmp': 'pal8.png', + 'pal8topdown.bmp': 'pal8.png', + 'pal8nonsquare.bmp': 'pal8nonsquare-v.png', + 'pal8os2.bmp': 'pal8.png', + 'pal8os2sp.bmp': 'pal8.png', + 'pal8os2v2.bmp': 'pal8.png', + 'pal8os2v2-16.bmp': 'pal8.png', + 'pal8v4.bmp': 'pal8.png', + 'pal8v5.bmp': 'pal8.png', + 'rgb16-565pal.bmp': 'rgb16-565.png', + 'rgb24pal.bmp': 'rgb24.png', + 'rgb32.bmp': 'rgb24.png', + 'rgb32bf.bmp': 'rgb24.png' + } + + def get_compare(f): + (head, name) = os.path.split(f) + if name in file_map: + return os.path.join(base, 'html', file_map[name]) + (name, ext) = os.path.splitext(name) + return os.path.join(base, 'html', "%s.png" % name) + + for f in self.get_files('g'): + try: + im = Image.open(f) + im.load() + compare = Image.open(get_compare(f)) + compare.load() + if im.mode == 'P': + # assert image similar doesn't really work + # with paletized image, since the palette might + # be differently ordered for an equivalent image. + im = im.convert('RGBA') + compare = im.convert('RGBA') + self.assert_image_similar(im, compare, 5) + + except Exception as msg: + # there are three here that are unsupported: + unsupported = (os.path.join(base, 'g', 'rgb32bf.bmp'), + os.path.join(base, 'g', 'pal8rle.bmp'), + os.path.join(base, 'g', 'pal4rle.bmp')) + if f not in unsupported: + self.assertTrue( + False, "Unsupported Image %s: %s" % (f, msg)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_file_psd.py b/test/test_file_psd.py new file mode 100644 index 000000000..de3d6f33d --- /dev/null +++ b/test/test_file_psd.py @@ -0,0 +1,23 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + +# sample ppm stream +file = "Images/lena.psd" +data = open(file, "rb").read() + + +class TestImagePsd(PillowTestCase): + + def test_sanity(self): + im = Image.open(file) + im.load() + self.assertEqual(im.mode, "RGB") + self.assertEqual(im.size, (128, 128)) + self.assertEqual(im.format, "PSD") + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_getcolors.py b/test/test_image_getcolors.py new file mode 100644 index 000000000..cfc827b28 --- /dev/null +++ b/test/test_image_getcolors.py @@ -0,0 +1,74 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImage(PillowTestCase): + + def test_getcolors(self): + + def getcolors(mode, limit=None): + im = lena(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"), 193) + self.assertEqual(getcolors("I"), 193) + self.assertEqual(getcolors("F"), 193) + self.assertEqual(getcolors("P"), 54) # fixed palette + self.assertEqual(getcolors("RGB"), None) + self.assertEqual(getcolors("RGBA"), None) + self.assertEqual(getcolors("CMYK"), None) + self.assertEqual(getcolors("YCbCr"), None) + + self.assertEqual(getcolors("L", 128), None) + self.assertEqual(getcolors("L", 1024), 193) + + self.assertEqual(getcolors("RGB", 8192), None) + self.assertEqual(getcolors("RGB", 16384), 14836) + self.assertEqual(getcolors("RGB", 100000), 14836) + + self.assertEqual(getcolors("RGBA", 16384), 14836) + self.assertEqual(getcolors("CMYK", 16384), 14836) + self.assertEqual(getcolors("YCbCr", 16384), 11995) + + # -------------------------------------------------------------------- + + def test_pack(self): + # Pack problems for small tables (@PIL209) + + im = lena().quantize(3).convert("RGB") + + expected = [ + (3236, (227, 183, 147)), + (6297, (143, 84, 81)), + (6851, (208, 143, 112))] + + A = im.getcolors(maxcolors=2) + self.assertEqual(A, None) + + A = im.getcolors(maxcolors=3) + A.sort() + self.assertEqual(A, expected) + + A = im.getcolors(maxcolors=4) + A.sort() + self.assertEqual(A, expected) + + A = im.getcolors(maxcolors=8) + A.sort() + self.assertEqual(A, expected) + + A = im.getcolors(maxcolors=16) + A.sort() + self.assertEqual(A, expected) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_rotate.py b/test/test_image_rotate.py new file mode 100644 index 000000000..531fdd63f --- /dev/null +++ b/test/test_image_rotate.py @@ -0,0 +1,22 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImageRotate(PillowTestCase): + + def test_rotate(self): + def rotate(mode): + im = lena(mode) + out = im.rotate(45) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size) # default rotate clips output + out = im.rotate(45, expand=1) + self.assertEqual(out.mode, mode) + self.assertNotEqual(out.size, im.size) + for mode in "1", "P", "L", "RGB", "I", "F": + rotate(mode) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_thumbnail.py b/test/test_image_thumbnail.py new file mode 100644 index 000000000..ee49be43e --- /dev/null +++ b/test/test_image_thumbnail.py @@ -0,0 +1,43 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImageThumbnail(PillowTestCase): + + def test_sanity(self): + + im = lena() + im.thumbnail((100, 100)) + + self.assert_image(im, im.mode, (100, 100)) + + def test_aspect(self): + + im = lena() + im.thumbnail((100, 100)) + self.assert_image(im, im.mode, (100, 100)) + + im = lena().resize((128, 256)) + im.thumbnail((100, 100)) + self.assert_image(im, im.mode, (50, 100)) + + im = lena().resize((128, 256)) + im.thumbnail((50, 100)) + self.assert_image(im, im.mode, (50, 100)) + + im = lena().resize((256, 128)) + im.thumbnail((100, 100)) + self.assert_image(im, im.mode, (100, 50)) + + im = lena().resize((256, 128)) + im.thumbnail((100, 50)) + self.assert_image(im, im.mode, (100, 50)) + + im = lena().resize((128, 128)) + im.thumbnail((100, 100)) + self.assert_image(im, im.mode, (100, 100)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imageops.py b/test/test_imageops.py new file mode 100644 index 000000000..a4a94ca4d --- /dev/null +++ b/test/test_imageops.py @@ -0,0 +1,85 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import ImageOps + + +class TestImageOps(PillowTestCase): + + class Deformer: + def getmesh(self, im): + x, y = im.size + return [((0, 0, x, y), (0, 0, x, 0, x, y, y, 0))] + + deformer = Deformer() + + def test_sanity(self): + + ImageOps.autocontrast(lena("L")) + ImageOps.autocontrast(lena("RGB")) + + ImageOps.autocontrast(lena("L"), cutoff=10) + ImageOps.autocontrast(lena("L"), ignore=[0, 255]) + + ImageOps.colorize(lena("L"), (0, 0, 0), (255, 255, 255)) + ImageOps.colorize(lena("L"), "black", "white") + + ImageOps.crop(lena("L"), 1) + ImageOps.crop(lena("RGB"), 1) + + ImageOps.deform(lena("L"), self.deformer) + ImageOps.deform(lena("RGB"), self.deformer) + + ImageOps.equalize(lena("L")) + ImageOps.equalize(lena("RGB")) + + ImageOps.expand(lena("L"), 1) + ImageOps.expand(lena("RGB"), 1) + ImageOps.expand(lena("L"), 2, "blue") + ImageOps.expand(lena("RGB"), 2, "blue") + + ImageOps.fit(lena("L"), (128, 128)) + ImageOps.fit(lena("RGB"), (128, 128)) + + ImageOps.flip(lena("L")) + ImageOps.flip(lena("RGB")) + + ImageOps.grayscale(lena("L")) + ImageOps.grayscale(lena("RGB")) + + ImageOps.invert(lena("L")) + ImageOps.invert(lena("RGB")) + + ImageOps.mirror(lena("L")) + ImageOps.mirror(lena("RGB")) + + ImageOps.posterize(lena("L"), 4) + ImageOps.posterize(lena("RGB"), 4) + + ImageOps.solarize(lena("L")) + ImageOps.solarize(lena("RGB")) + + def test_1pxfit(self): + # Division by zero in equalize if image is 1 pixel high + newimg = ImageOps.fit(lena("RGB").resize((1, 1)), (35, 35)) + self.assertEqual(newimg.size, (35, 35)) + + newimg = ImageOps.fit(lena("RGB").resize((1, 100)), (35, 35)) + self.assertEqual(newimg.size, (35, 35)) + + newimg = ImageOps.fit(lena("RGB").resize((100, 1)), (35, 35)) + self.assertEqual(newimg.size, (35, 35)) + + def test_pil163(self): + # Division by zero in equalize if < 255 pixels in image (@PIL163) + + i = lena("RGB").resize((15, 16)) + + ImageOps.equalize(i.convert("L")) + ImageOps.equalize(i.convert("P")) + ImageOps.equalize(i.convert("RGB")) + + +if __name__ == '__main__': + unittest.main() + +# End of file From d18c406d394bc87737f9136280e45d36d31988fe Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 17:27:03 +0300 Subject: [PATCH 20/33] Convert test_imagecolor.py --- Tests/test_imagecolor.py | 54 ------------------------------ test/test_imagecolor.py | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 54 deletions(-) delete mode 100644 Tests/test_imagecolor.py create mode 100644 test/test_imagecolor.py diff --git a/Tests/test_imagecolor.py b/Tests/test_imagecolor.py deleted file mode 100644 index c67c20255..000000000 --- a/Tests/test_imagecolor.py +++ /dev/null @@ -1,54 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageColor - -# -------------------------------------------------------------------- -# sanity - -assert_equal((255, 0, 0), ImageColor.getrgb("#f00")) -assert_equal((255, 0, 0), ImageColor.getrgb("#ff0000")) -assert_equal((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)")) -assert_equal((255, 0, 0), ImageColor.getrgb("rgb(255, 0, 0)")) -assert_equal((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)")) -assert_equal((255, 0, 0), ImageColor.getrgb("hsl(0, 100%, 50%)")) -assert_equal((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)")) -assert_equal((255, 0, 0, 0), ImageColor.getrgb("rgba(255, 0, 0, 0)")) -assert_equal((255, 0, 0), ImageColor.getrgb("red")) - -# -------------------------------------------------------------------- -# look for rounding errors (based on code by Tim Hatch) - -for color in list(ImageColor.colormap.keys()): - expected = Image.new("RGB", (1, 1), color).convert("L").getpixel((0, 0)) - actual = Image.new("L", (1, 1), color).getpixel((0, 0)) - assert_equal(expected, actual) - -assert_equal((0, 0, 0), ImageColor.getcolor("black", "RGB")) -assert_equal((255, 255, 255), ImageColor.getcolor("white", "RGB")) -assert_equal((0, 255, 115), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")) -Image.new("RGB", (1, 1), "white") - -assert_equal((0, 0, 0, 255), ImageColor.getcolor("black", "RGBA")) -assert_equal((255, 255, 255, 255), ImageColor.getcolor("white", "RGBA")) -assert_equal((0, 255, 115, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGBA")) -Image.new("RGBA", (1, 1), "white") - -assert_equal(0, ImageColor.getcolor("black", "L")) -assert_equal(255, ImageColor.getcolor("white", "L")) -assert_equal(162, ImageColor.getcolor("rgba(0, 255, 115, 33)", "L")) -Image.new("L", (1, 1), "white") - -assert_equal(0, ImageColor.getcolor("black", "1")) -assert_equal(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_equal(162, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1")) -# Correct behavior -# assert_equal(255, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1")) -Image.new("1", (1, 1), "white") - -assert_equal((0, 255), ImageColor.getcolor("black", "LA")) -assert_equal((255, 255), ImageColor.getcolor("white", "LA")) -assert_equal((162, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA")) -Image.new("LA", (1, 1), "white") diff --git a/test/test_imagecolor.py b/test/test_imagecolor.py new file mode 100644 index 000000000..5d8944852 --- /dev/null +++ b/test/test_imagecolor.py @@ -0,0 +1,71 @@ +from helper import unittest, PillowTestCase + +from PIL import Image +from PIL import ImageColor + + +class TestImageColor(PillowTestCase): + + def test_sanity(self): + self.assertEqual((255, 0, 0), ImageColor.getrgb("#f00")) + self.assertEqual((255, 0, 0), ImageColor.getrgb("#ff0000")) + self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)")) + 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), ImageColor.getrgb("hsl(0, 100%, 50%)")) + self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)")) + self.assertEqual( + (255, 0, 0, 0), ImageColor.getrgb("rgba(255, 0, 0, 0)")) + self.assertEqual((255, 0, 0), ImageColor.getrgb("red")) + + # look for rounding errors (based on code by Tim Hatch) + def test_rounding_errors(self): + + for color in list(ImageColor.colormap.keys()): + expected = Image.new( + "RGB", (1, 1), color).convert("L").getpixel((0, 0)) + actual = Image.new("L", (1, 1), color).getpixel((0, 0)) + self.assertEqual(expected, actual) + + self.assertEqual((0, 0, 0), ImageColor.getcolor("black", "RGB")) + self.assertEqual((255, 255, 255), ImageColor.getcolor("white", "RGB")) + 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( + 162, 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( + 162, 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( + (162, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA")) + Image.new("LA", (1, 1), "white") + + +if __name__ == '__main__': + unittest.main() + +# End of file From e01e3e90d592e6c0865be77b66e4b32f7407f49a Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 5 Jun 2014 18:15:12 +0300 Subject: [PATCH 21/33] Convert more tests --- Tests/test_file_xpm.py | 14 ---- Tests/test_format_lab.py | 41 ----------- Tests/test_image_getbands.py | 15 ---- Tests/test_image_putalpha.py | 43 ------------ Tests/test_image_transform.py | 116 ------------------------------- test/test_file_xpm.py | 23 +++++++ test/test_format_lab.py | 48 +++++++++++++ test/test_image_getbands.py | 26 +++++++ test/test_image_putalpha.py | 52 ++++++++++++++ test/test_image_transform.py | 125 ++++++++++++++++++++++++++++++++++ 10 files changed, 274 insertions(+), 229 deletions(-) delete mode 100644 Tests/test_file_xpm.py delete mode 100644 Tests/test_format_lab.py delete mode 100644 Tests/test_image_getbands.py delete mode 100644 Tests/test_image_putalpha.py delete mode 100644 Tests/test_image_transform.py create mode 100644 test/test_file_xpm.py create mode 100644 test/test_format_lab.py create mode 100644 test/test_image_getbands.py create mode 100644 test/test_image_putalpha.py create mode 100644 test/test_image_transform.py diff --git a/Tests/test_file_xpm.py b/Tests/test_file_xpm.py deleted file mode 100644 index 44135d028..000000000 --- a/Tests/test_file_xpm.py +++ /dev/null @@ -1,14 +0,0 @@ -from tester import * - -from PIL import Image - -# sample ppm stream -file = "Images/lena.xpm" -data = open(file, "rb").read() - -def test_sanity(): - im = Image.open(file) - im.load() - assert_equal(im.mode, "P") - assert_equal(im.size, (128, 128)) - assert_equal(im.format, "XPM") diff --git a/Tests/test_format_lab.py b/Tests/test_format_lab.py deleted file mode 100644 index 371b06a0b..000000000 --- a/Tests/test_format_lab.py +++ /dev/null @@ -1,41 +0,0 @@ -from tester import * - -from PIL import Image - -def test_white(): - i = Image.open('Tests/images/lab.tif') - - bits = i.load() - - assert_equal(i.mode, 'LAB') - - assert_equal(i.getbands(), ('L','A', 'B')) - - k = i.getpixel((0,0)) - assert_equal(k, (255,128,128)) - - L = i.getdata(0) - a = i.getdata(1) - b = i.getdata(2) - - assert_equal(list(L), [255]*100) - assert_equal(list(a), [128]*100) - assert_equal(list(b), [128]*100) - - -def test_green(): - # l= 50 (/100), a = -100 (-128 .. 128) b=0 in PS - # == RGB: 0, 152, 117 - i = Image.open('Tests/images/lab-green.tif') - - k = i.getpixel((0,0)) - assert_equal(k, (128,28,128)) - - -def test_red(): - # l= 50 (/100), a = 100 (-128 .. 128) b=0 in PS - # == RGB: 255, 0, 124 - i = Image.open('Tests/images/lab-red.tif') - - k = i.getpixel((0,0)) - assert_equal(k, (128,228,128)) diff --git a/Tests/test_image_getbands.py b/Tests/test_image_getbands.py deleted file mode 100644 index e7f1ec5a9..000000000 --- a/Tests/test_image_getbands.py +++ /dev/null @@ -1,15 +0,0 @@ -from tester import * - -from PIL import Image - -def test_getbands(): - - assert_equal(Image.new("1", (1, 1)).getbands(), ("1",)) - assert_equal(Image.new("L", (1, 1)).getbands(), ("L",)) - assert_equal(Image.new("I", (1, 1)).getbands(), ("I",)) - assert_equal(Image.new("F", (1, 1)).getbands(), ("F",)) - assert_equal(Image.new("P", (1, 1)).getbands(), ("P",)) - assert_equal(Image.new("RGB", (1, 1)).getbands(), ("R", "G", "B")) - assert_equal(Image.new("RGBA", (1, 1)).getbands(), ("R", "G", "B", "A")) - assert_equal(Image.new("CMYK", (1, 1)).getbands(), ("C", "M", "Y", "K")) - assert_equal(Image.new("YCbCr", (1, 1)).getbands(), ("Y", "Cb", "Cr")) diff --git a/Tests/test_image_putalpha.py b/Tests/test_image_putalpha.py deleted file mode 100644 index b23f69834..000000000 --- a/Tests/test_image_putalpha.py +++ /dev/null @@ -1,43 +0,0 @@ -from tester import * - -from PIL import Image - -def test_interface(): - - im = Image.new("RGBA", (1, 1), (1, 2, 3, 0)) - assert_equal(im.getpixel((0, 0)), (1, 2, 3, 0)) - - im = Image.new("RGBA", (1, 1), (1, 2, 3)) - assert_equal(im.getpixel((0, 0)), (1, 2, 3, 255)) - - im.putalpha(Image.new("L", im.size, 4)) - assert_equal(im.getpixel((0, 0)), (1, 2, 3, 4)) - - im.putalpha(5) - assert_equal(im.getpixel((0, 0)), (1, 2, 3, 5)) - -def test_promote(): - - im = Image.new("L", (1, 1), 1) - assert_equal(im.getpixel((0, 0)), 1) - - im.putalpha(2) - assert_equal(im.mode, 'LA') - assert_equal(im.getpixel((0, 0)), (1, 2)) - - im = Image.new("RGB", (1, 1), (1, 2, 3)) - assert_equal(im.getpixel((0, 0)), (1, 2, 3)) - - im.putalpha(4) - assert_equal(im.mode, 'RGBA') - assert_equal(im.getpixel((0, 0)), (1, 2, 3, 4)) - -def test_readonly(): - - im = Image.new("RGB", (1, 1), (1, 2, 3)) - im.readonly = 1 - - im.putalpha(4) - assert_false(im.readonly) - assert_equal(im.mode, 'RGBA') - assert_equal(im.getpixel((0, 0)), (1, 2, 3, 4)) diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py deleted file mode 100644 index dd9b6fe5c..000000000 --- a/Tests/test_image_transform.py +++ /dev/null @@ -1,116 +0,0 @@ -from tester import * - -from PIL import Image - -def test_extent(): - im = lena('RGB') - (w,h) = im.size - transformed = im.transform(im.size, Image.EXTENT, - (0,0, - w//2,h//2), # ul -> lr - Image.BILINEAR) - - - scaled = im.resize((w*2, h*2), Image.BILINEAR).crop((0,0,w,h)) - - assert_image_similar(transformed, scaled, 10) # undone -- precision? - -def test_quad(): - # one simple quad transform, equivalent to scale & crop upper left quad - im = lena('RGB') - (w,h) = im.size - transformed = im.transform(im.size, Image.QUAD, - (0,0,0,h//2, - w//2,h//2,w//2,0), # ul -> ccw around quad - Image.BILINEAR) - - scaled = im.resize((w*2, h*2), Image.BILINEAR).crop((0,0,w,h)) - - assert_image_equal(transformed, scaled) - -def test_mesh(): - # this should be a checkerboard of halfsized lenas in ul, lr - im = lena('RGBA') - (w,h) = im.size - transformed = im.transform(im.size, Image.MESH, - [((0,0,w//2,h//2), # box - (0,0,0,h, - w,h,w,0)), # ul -> ccw around quad - ((w//2,h//2,w,h), # box - (0,0,0,h, - w,h,w,0))], # ul -> ccw around quad - Image.BILINEAR) - - #transformed.save('transformed.png') - - scaled = im.resize((w//2, h//2), Image.BILINEAR) - - checker = Image.new('RGBA', im.size) - checker.paste(scaled, (0,0)) - checker.paste(scaled, (w//2,h//2)) - - assert_image_equal(transformed, checker) - - # now, check to see that the extra area is (0,0,0,0) - blank = Image.new('RGBA', (w//2,h//2), (0,0,0,0)) - - assert_image_equal(blank, transformed.crop((w//2,0,w,h//2))) - assert_image_equal(blank, transformed.crop((0,h//2,w//2,h))) - -def _test_alpha_premult(op): - # create image with half white, half black, with the black half transparent. - # do op, - # there should be no darkness in the white section. - im = Image.new('RGBA', (10,10), (0,0,0,0)); - im2 = Image.new('RGBA', (5,10), (255,255,255,255)); - im.paste(im2, (0,0)) - - im = op(im, (40,10)) - im_background = Image.new('RGB', (40,10), (255,255,255)) - im_background.paste(im, (0,0), im) - - hist = im_background.histogram() - assert_equal(40*10, hist[-1]) - - -def test_alpha_premult_resize(): - - def op (im, sz): - return im.resize(sz, Image.LINEAR) - - _test_alpha_premult(op) - -def test_alpha_premult_transform(): - - def op(im, sz): - (w,h) = im.size - return im.transform(sz, Image.EXTENT, - (0,0, - w,h), - Image.BILINEAR) - - _test_alpha_premult(op) - - -def test_blank_fill(): - # attempting to hit - # https://github.com/python-pillow/Pillow/issues/254 reported - # - # issue is that transforms with transparent overflow area - # contained junk from previous images, especially on systems with - # constrained memory. So, attempt to fill up memory with a - # pattern, free it, and then run the mesh test again. Using a 1Mp - # image with 4 bands, for 4 megs of data allocated, x 64. OMM (64 - # bit 12.04 VM with 512 megs available, this fails with Pillow < - # a0eaf06cc5f62a6fb6de556989ac1014ff3348ea - # - # Running by default, but I'd totally understand not doing it in - # the future - - foo = [Image.new('RGBA',(1024,1024), (a,a,a,a)) - for a in range(1,65)] - - # Yeah. Watch some JIT optimize this out. - foo = None - - test_mesh() diff --git a/test/test_file_xpm.py b/test/test_file_xpm.py new file mode 100644 index 000000000..ecbb4137a --- /dev/null +++ b/test/test_file_xpm.py @@ -0,0 +1,23 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + +# sample ppm stream +file = "Images/lena.xpm" +data = open(file, "rb").read() + + +class TestFileXpm(PillowTestCase): + + def test_sanity(self): + im = Image.open(file) + im.load() + self.assertEqual(im.mode, "P") + self.assertEqual(im.size, (128, 128)) + self.assertEqual(im.format, "XPM") + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_format_lab.py b/test/test_format_lab.py new file mode 100644 index 000000000..53468db5f --- /dev/null +++ b/test/test_format_lab.py @@ -0,0 +1,48 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + + +class TestFormatLab(PillowTestCase): + + def test_white(self): + i = Image.open('Tests/images/lab.tif') + + i.load() + + self.assertEqual(i.mode, 'LAB') + + self.assertEqual(i.getbands(), ('L', 'A', 'B')) + + k = i.getpixel((0, 0)) + self.assertEqual(k, (255, 128, 128)) + + L = i.getdata(0) + a = i.getdata(1) + b = i.getdata(2) + + self.assertEqual(list(L), [255]*100) + self.assertEqual(list(a), [128]*100) + self.assertEqual(list(b), [128]*100) + + def test_green(self): + # l= 50 (/100), a = -100 (-128 .. 128) b=0 in PS + # == RGB: 0, 152, 117 + i = Image.open('Tests/images/lab-green.tif') + + k = i.getpixel((0, 0)) + self.assertEqual(k, (128, 28, 128)) + + def test_red(self): + # l= 50 (/100), a = 100 (-128 .. 128) b=0 in PS + # == RGB: 255, 0, 124 + i = Image.open('Tests/images/lab-red.tif') + + k = i.getpixel((0, 0)) + self.assertEqual(k, (128, 228, 128)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_getbands.py b/test/test_image_getbands.py new file mode 100644 index 000000000..e803abb02 --- /dev/null +++ b/test/test_image_getbands.py @@ -0,0 +1,26 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + + +class TestImageGetBands(PillowTestCase): + + def test_getbands(self): + self.assertEqual(Image.new("1", (1, 1)).getbands(), ("1",)) + self.assertEqual(Image.new("L", (1, 1)).getbands(), ("L",)) + self.assertEqual(Image.new("I", (1, 1)).getbands(), ("I",)) + self.assertEqual(Image.new("F", (1, 1)).getbands(), ("F",)) + self.assertEqual(Image.new("P", (1, 1)).getbands(), ("P",)) + self.assertEqual(Image.new("RGB", (1, 1)).getbands(), ("R", "G", "B")) + self.assertEqual( + Image.new("RGBA", (1, 1)).getbands(), ("R", "G", "B", "A")) + self.assertEqual( + Image.new("CMYK", (1, 1)).getbands(), ("C", "M", "Y", "K")) + self.assertEqual( + Image.new("YCbCr", (1, 1)).getbands(), ("Y", "Cb", "Cr")) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_putalpha.py b/test/test_image_putalpha.py new file mode 100644 index 000000000..85c7ac262 --- /dev/null +++ b/test/test_image_putalpha.py @@ -0,0 +1,52 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + + +class TestImagePutAlpha(PillowTestCase): + + def test_interface(self): + + im = Image.new("RGBA", (1, 1), (1, 2, 3, 0)) + self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 0)) + + im = Image.new("RGBA", (1, 1), (1, 2, 3)) + self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 255)) + + im.putalpha(Image.new("L", im.size, 4)) + self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 4)) + + im.putalpha(5) + self.assertEqual(im.getpixel((0, 0)), (1, 2, 3, 5)) + + def test_promote(self): + + 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)) + + 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)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_transform.py b/test/test_image_transform.py new file mode 100644 index 000000000..6ab186c12 --- /dev/null +++ b/test/test_image_transform.py @@ -0,0 +1,125 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImageTransform(PillowTestCase): + + def test_extent(self): + im = lena('RGB') + (w, h) = im.size + transformed = im.transform(im.size, Image.EXTENT, + (0, 0, + w//2, h//2), # ul -> lr + Image.BILINEAR) + + scaled = im.resize((w*2, h*2), Image.BILINEAR).crop((0, 0, w, h)) + + # undone -- precision? + self.assert_image_similar(transformed, scaled, 10) + + def test_quad(self): + # one simple quad transform, equivalent to scale & crop upper left quad + im = lena('RGB') + (w, h) = im.size + transformed = im.transform(im.size, Image.QUAD, + (0, 0, 0, h//2, + # ul -> ccw around quad: + w//2, h//2, w//2, 0), + Image.BILINEAR) + + scaled = im.resize((w*2, h*2), Image.BILINEAR).crop((0, 0, w, h)) + + self.assert_image_equal(transformed, scaled) + + def test_mesh(self): + # this should be a checkerboard of halfsized lenas in ul, lr + im = lena('RGBA') + (w, h) = im.size + transformed = im.transform(im.size, Image.MESH, + [((0, 0, w//2, h//2), # box + (0, 0, 0, h, + w, h, w, 0)), # ul -> ccw around quad + ((w//2, h//2, w, h), # box + (0, 0, 0, h, + w, h, w, 0))], # ul -> ccw around quad + Image.BILINEAR) + + # transformed.save('transformed.png') + + scaled = im.resize((w//2, h//2), Image.BILINEAR) + + checker = Image.new('RGBA', im.size) + checker.paste(scaled, (0, 0)) + checker.paste(scaled, (w//2, h//2)) + + self.assert_image_equal(transformed, checker) + + # now, check to see that the extra area is (0, 0, 0, 0) + blank = Image.new('RGBA', (w//2, h//2), (0, 0, 0, 0)) + + self.assert_image_equal(blank, transformed.crop((w//2, 0, w, h//2))) + self.assert_image_equal(blank, transformed.crop((0, h//2, w//2, h))) + + def _test_alpha_premult(self, op): + # create image with half white, half black, + # with the black half transparent. + # do op, + # there should be no darkness in the white section. + im = Image.new('RGBA', (10, 10), (0, 0, 0, 0)) + im2 = Image.new('RGBA', (5, 10), (255, 255, 255, 255)) + im.paste(im2, (0, 0)) + + im = op(im, (40, 10)) + im_background = Image.new('RGB', (40, 10), (255, 255, 255)) + im_background.paste(im, (0, 0), im) + + hist = im_background.histogram() + self.assertEqual(40*10, hist[-1]) + + def test_alpha_premult_resize(self): + + def op(im, sz): + return im.resize(sz, Image.LINEAR) + + self._test_alpha_premult(op) + + def test_alpha_premult_transform(self): + + def op(im, sz): + (w, h) = im.size + return im.transform(sz, Image.EXTENT, + (0, 0, + w, h), + Image.BILINEAR) + + self._test_alpha_premult(op) + + def test_blank_fill(self): + # attempting to hit + # https://github.com/python-pillow/Pillow/issues/254 reported + # + # issue is that transforms with transparent overflow area + # contained junk from previous images, especially on systems with + # constrained memory. So, attempt to fill up memory with a + # pattern, free it, and then run the mesh test again. Using a 1Mp + # image with 4 bands, for 4 megs of data allocated, x 64. OMM (64 + # bit 12.04 VM with 512 megs available, this fails with Pillow < + # a0eaf06cc5f62a6fb6de556989ac1014ff3348ea + # + # Running by default, but I'd totally understand not doing it in + # the future + + foo = [Image.new('RGBA', (1024, 1024), (a, a, a, a)) + for a in range(1, 65)] + + # Yeah. Watch some JIT optimize this out. + foo = None + + self.test_mesh() + + +if __name__ == '__main__': + unittest.main() + +# End of file From e87ae09328cde2da7193f696e10ae4480ff3c7f3 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 5 Jun 2014 21:31:26 +0300 Subject: [PATCH 22/33] Delete test test [CI skip] --- test/mini.py | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 test/mini.py diff --git a/test/mini.py b/test/mini.py deleted file mode 100644 index c9f1665ab..000000000 --- a/test/mini.py +++ /dev/null @@ -1,5 +0,0 @@ -from test_image_putpixel import TestImagePutPixel as put -from test_image_getpixel import TestImageGetPixel as get - -dir(get) -get.test_basic() \ No newline at end of file From 0eb92eccd7d4528093b21f6a9b3f3396da16fede Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 5 Jun 2014 22:10:12 +0300 Subject: [PATCH 23/33] Skip test_floodfill_border() on PyPy Otherwise it sometimes, but not always, causes an error: RPython traceback: File "rpython_jit_metainterp_compile.c", line 20472, in send_loop_to_backend File "rpython_jit_backend_x86_assembler.c", line 1818, in Assembler386_assemble_loop File "rpython_jit_backend_x86_regalloc.c", line 293, in RegAlloc_prepare_loop File "rpython_jit_backend_x86_regalloc.c", line 909, in RegAlloc__prepare File "rpython_jit_backend_llsupport_regalloc.c", line 4706, in compute_vars_longevity Fatal RPython error: AssertionError /home/travis/build.sh: line 236: 7300 Aborted --- test/test_imagedraw.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_imagedraw.py b/test/test_imagedraw.py index 44403e50c..98876296f 100644 --- a/test/test_imagedraw.py +++ b/test/test_imagedraw.py @@ -4,6 +4,8 @@ from PIL import Image from PIL import ImageColor from PIL import ImageDraw +import sys + # Image size w, h = 100, 100 @@ -225,7 +227,11 @@ class TestImageDraw(PillowTestCase): self.assert_image_equal( im, Image.open("Tests/images/imagedraw_floodfill.png")) + @unittest.skipIf(hasattr(sys, 'pypy_version_info'), + "Causes fatal RPython error on PyPy") def test_floodfill_border(self): + # floodfill() is experimental + # Arrange im = Image.new("RGB", (w, h)) draw = ImageDraw.Draw(im) From 4ab5b6c583f5c21ae065cbacb66bc6b25f92005b Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 10:48:28 +0300 Subject: [PATCH 24/33] Comment out lena() caching --- test/helper.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/helper.py b/test/helper.py index e6684b845..455ff9673 100644 --- a/test/helper.py +++ b/test/helper.py @@ -169,9 +169,9 @@ def tostring(im, format, **options): return out.getvalue() -def lena(mode="RGB", cache={}): +def lena(mode="RGB"): # , cache={}): from PIL import Image - im = cache.get(mode) + # im = cache.get(mode) if im is None: if mode == "RGB": im = Image.open("Images/lena.ppm") @@ -181,7 +181,7 @@ def lena(mode="RGB", cache={}): im = lena("I").convert(mode) else: im = lena("RGB").convert(mode) - cache[mode] = im + # cache[mode] = im return im From 40ada60ceceedb67a1e91121cb89f49862f9bf9f Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 10:55:04 +0300 Subject: [PATCH 25/33] Comment out lena() caching --- test/helper.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/helper.py b/test/helper.py index 455ff9673..cfe7b86c7 100644 --- a/test/helper.py +++ b/test/helper.py @@ -172,6 +172,7 @@ def tostring(im, format, **options): def lena(mode="RGB"): # , cache={}): from PIL import Image # im = cache.get(mode) + im = None if im is None: if mode == "RGB": im = Image.open("Images/lena.ppm") From e2e361141c1d4f90542a198e427de2450321b407 Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 14:04:26 +0300 Subject: [PATCH 26/33] Return duplicate in lena() --- .travis.yml | 12 ++++++------ test/helper.py | 11 ++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 48206a64d..ef5094a68 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,14 +29,14 @@ script: - python setup.py build_ext --inplace # Don't cover PyPy: it fails intermittently and is x5.8 slower (#640) - - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python selftest.py; fi - - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then nosetests test/; fi - - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python Tests/run.py; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then time python selftest.py; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then time nosetests test/; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then time python Tests/run.py; fi # Cover the others - - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* selftest.py; fi - - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* -m nose test/; fi - - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then python Tests/run.py --coverage; fi + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then time coverage run --append --include=PIL/* selftest.py; fi + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then time coverage run --append --include=PIL/* -m nose test/; fi + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then time python Tests/run.py --coverage; fi after_success: diff --git a/test/helper.py b/test/helper.py index cfe7b86c7..9fe66a721 100644 --- a/test/helper.py +++ b/test/helper.py @@ -169,10 +169,9 @@ def tostring(im, format, **options): return out.getvalue() -def lena(mode="RGB"): # , cache={}): +def lena(mode="RGB"), cache={}): from PIL import Image - # im = cache.get(mode) - im = None + im = cache.get(mode) if im is None: if mode == "RGB": im = Image.open("Images/lena.ppm") @@ -182,8 +181,10 @@ def lena(mode="RGB"): # , cache={}): im = lena("I").convert(mode) else: im = lena("RGB").convert(mode) - # cache[mode] = im - return im + cache[mode] = im + import copy + duplicate = copy.copy(im) + return duplicate # def assert_image_completely_equal(a, b, msg=None): From c45f20119c2ba40d4802f984e8d54afacd160a2b Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 14:53:33 +0300 Subject: [PATCH 27/33] Fix syntax error --- test/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helper.py b/test/helper.py index 9fe66a721..f97faf4d1 100644 --- a/test/helper.py +++ b/test/helper.py @@ -169,7 +169,7 @@ def tostring(im, format, **options): return out.getvalue() -def lena(mode="RGB"), cache={}): +def lena(mode="RGB", cache={}): from PIL import Image im = cache.get(mode) if im is None: From 3ff2ea4883b32303c6baacbc5df06d3fc76c4a1a Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 15:19:28 +0300 Subject: [PATCH 28/33] Disable lena() caching for now, convert more tests --- Tests/test_file_icns.py | 66 -------------------------------- Tests/test_image_resize.py | 12 ------ Tests/test_imagefileio.py | 24 ------------ Tests/test_imagetransform.py | 18 --------- Tests/test_locale.py | 31 --------------- test/helper.py | 9 ++--- test/test_file_icns.py | 74 ++++++++++++++++++++++++++++++++++++ test/test_image_resize.py | 19 +++++++++ test/test_imagefileio.py | 31 +++++++++++++++ test/test_imagetransform.py | 27 +++++++++++++ test/test_locale.py | 39 +++++++++++++++++++ 11 files changed, 194 insertions(+), 156 deletions(-) delete mode 100644 Tests/test_file_icns.py delete mode 100644 Tests/test_image_resize.py delete mode 100644 Tests/test_imagefileio.py delete mode 100644 Tests/test_imagetransform.py delete mode 100644 Tests/test_locale.py create mode 100644 test/test_file_icns.py create mode 100644 test/test_image_resize.py create mode 100644 test/test_imagefileio.py create mode 100644 test/test_imagetransform.py create mode 100644 test/test_locale.py diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py deleted file mode 100644 index 3e31f8879..000000000 --- a/Tests/test_file_icns.py +++ /dev/null @@ -1,66 +0,0 @@ -from tester import * - -from PIL import Image - -# sample icon file -file = "Images/pillow.icns" -data = open(file, "rb").read() - -enable_jpeg2k = hasattr(Image.core, 'jp2klib_version') - -def test_sanity(): - # Loading this icon by default should result in the largest size - # (512x512@2x) being loaded - im = Image.open(file) - im.load() - assert_equal(im.mode, "RGBA") - assert_equal(im.size, (1024, 1024)) - assert_equal(im.format, "ICNS") - -def test_sizes(): - # Check that we can load all of the sizes, and that the final pixel - # dimensions are as expected - im = Image.open(file) - for w,h,r in im.info['sizes']: - wr = w * r - hr = h * r - im2 = Image.open(file) - im2.size = (w, h, r) - im2.load() - assert_equal(im2.mode, 'RGBA') - assert_equal(im2.size, (wr, hr)) - -def test_older_icon(): - # This icon was made with Icon Composer rather than iconutil; it still - # uses PNG rather than JP2, however (since it was made on 10.9). - im = Image.open('Tests/images/pillow2.icns') - for w,h,r in im.info['sizes']: - wr = w * r - hr = h * r - im2 = Image.open('Tests/images/pillow2.icns') - im2.size = (w, h, r) - im2.load() - assert_equal(im2.mode, 'RGBA') - assert_equal(im2.size, (wr, hr)) - -def test_jp2_icon(): - # This icon was made by using Uli Kusterer's oldiconutil to replace - # the PNG images with JPEG 2000 ones. The advantage of doing this is - # that OS X 10.5 supports JPEG 2000 but not PNG; some commercial - # software therefore does just this. - - # (oldiconutil is here: https://github.com/uliwitness/oldiconutil) - - if not enable_jpeg2k: - return - - im = Image.open('Tests/images/pillow3.icns') - for w,h,r in im.info['sizes']: - wr = w * r - hr = h * r - im2 = Image.open('Tests/images/pillow3.icns') - im2.size = (w, h, r) - im2.load() - assert_equal(im2.mode, 'RGBA') - assert_equal(im2.size, (wr, hr)) - diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py deleted file mode 100644 index 4e228a396..000000000 --- a/Tests/test_image_resize.py +++ /dev/null @@ -1,12 +0,0 @@ -from tester import * - -from PIL import Image - -def test_resize(): - def resize(mode, size): - out = lena(mode).resize(size) - assert_equal(out.mode, mode) - assert_equal(out.size, size) - for mode in "1", "P", "L", "RGB", "I", "F": - yield_test(resize, mode, (100, 100)) - yield_test(resize, mode, (200, 200)) diff --git a/Tests/test_imagefileio.py b/Tests/test_imagefileio.py deleted file mode 100644 index c63f07bb0..000000000 --- a/Tests/test_imagefileio.py +++ /dev/null @@ -1,24 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageFileIO - -def test_fileio(): - - class DumbFile: - def __init__(self, data): - self.data = data - def read(self, bytes=None): - assert_equal(bytes, None) - return self.data - def close(self): - pass - - im1 = lena() - - io = ImageFileIO.ImageFileIO(DumbFile(tostring(im1, "PPM"))) - - im2 = Image.open(io) - assert_image_equal(im1, im2) - - diff --git a/Tests/test_imagetransform.py b/Tests/test_imagetransform.py deleted file mode 100644 index 884e6bb1c..000000000 --- a/Tests/test_imagetransform.py +++ /dev/null @@ -1,18 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageTransform - -im = Image.new("L", (100, 100)) - -seq = tuple(range(10)) - -def test_sanity(): - transform = ImageTransform.AffineTransform(seq[:6]) - assert_no_exception(lambda: im.transform((100, 100), transform)) - transform = ImageTransform.ExtentTransform(seq[:4]) - assert_no_exception(lambda: im.transform((100, 100), transform)) - transform = ImageTransform.QuadTransform(seq[:8]) - assert_no_exception(lambda: im.transform((100, 100), transform)) - transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])]) - assert_no_exception(lambda: im.transform((100, 100), transform)) diff --git a/Tests/test_locale.py b/Tests/test_locale.py deleted file mode 100644 index 6b2b95201..000000000 --- a/Tests/test_locale.py +++ /dev/null @@ -1,31 +0,0 @@ -from tester import * -from PIL import Image - -import locale - -# ref https://github.com/python-pillow/Pillow/issues/272 -## on windows, in polish locale: - -## import locale -## print locale.setlocale(locale.LC_ALL, 'polish') -## import string -## print len(string.whitespace) -## print ord(string.whitespace[6]) - -## Polish_Poland.1250 -## 7 -## 160 - -# one of string.whitespace is not freely convertable into ascii. - -path = "Images/lena.jpg" - -def test_sanity(): - assert_no_exception(lambda: Image.open(path)) - try: - locale.setlocale(locale.LC_ALL, "polish") - except: - skip('polish locale not available') - import string - assert_no_exception(lambda: Image.open(path)) - diff --git a/test/helper.py b/test/helper.py index f97faf4d1..ebe651120 100644 --- a/test/helper.py +++ b/test/helper.py @@ -171,7 +171,8 @@ def tostring(im, format, **options): def lena(mode="RGB", cache={}): from PIL import Image - im = cache.get(mode) + im = None + # im = cache.get(mode) if im is None: if mode == "RGB": im = Image.open("Images/lena.ppm") @@ -181,10 +182,8 @@ def lena(mode="RGB", cache={}): im = lena("I").convert(mode) else: im = lena("RGB").convert(mode) - cache[mode] = im - import copy - duplicate = copy.copy(im) - return duplicate + # cache[mode] = im + return im # def assert_image_completely_equal(a, b, msg=None): diff --git a/test/test_file_icns.py b/test/test_file_icns.py new file mode 100644 index 000000000..9d838620b --- /dev/null +++ b/test/test_file_icns.py @@ -0,0 +1,74 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + +# sample icon file +file = "Images/pillow.icns" +data = open(file, "rb").read() + +enable_jpeg2k = hasattr(Image.core, 'jp2klib_version') + + +class TestFileIcns(PillowTestCase): + + def test_sanity(self): + # Loading this icon by default should result in the largest size + # (512x512@2x) being loaded + im = Image.open(file) + im.load() + self.assertEqual(im.mode, "RGBA") + self.assertEqual(im.size, (1024, 1024)) + self.assertEqual(im.format, "ICNS") + + def test_sizes(self): + # Check that we can load all of the sizes, and that the final pixel + # dimensions are as expected + im = Image.open(file) + for w, h, r in im.info['sizes']: + wr = w * r + hr = h * r + im2 = Image.open(file) + im2.size = (w, h, r) + im2.load() + self.assertEqual(im2.mode, 'RGBA') + self.assertEqual(im2.size, (wr, hr)) + + def test_older_icon(self): + # This icon was made with Icon Composer rather than iconutil; it still + # uses PNG rather than JP2, however (since it was made on 10.9). + im = Image.open('Tests/images/pillow2.icns') + for w, h, r in im.info['sizes']: + wr = w * r + hr = h * r + im2 = Image.open('Tests/images/pillow2.icns') + im2.size = (w, h, r) + im2.load() + self.assertEqual(im2.mode, 'RGBA') + self.assertEqual(im2.size, (wr, hr)) + + def test_jp2_icon(self): + # This icon was made by using Uli Kusterer's oldiconutil to replace + # the PNG images with JPEG 2000 ones. The advantage of doing this is + # that OS X 10.5 supports JPEG 2000 but not PNG; some commercial + # software therefore does just this. + + # (oldiconutil is here: https://github.com/uliwitness/oldiconutil) + + if not enable_jpeg2k: + return + + im = Image.open('Tests/images/pillow3.icns') + for w, h, r in im.info['sizes']: + wr = w * r + hr = h * r + im2 = Image.open('Tests/images/pillow3.icns') + im2.size = (w, h, r) + im2.load() + self.assertEqual(im2.mode, 'RGBA') + self.assertEqual(im2.size, (wr, hr)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_resize.py b/test/test_image_resize.py new file mode 100644 index 000000000..6c9932e45 --- /dev/null +++ b/test/test_image_resize.py @@ -0,0 +1,19 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImageResize(PillowTestCase): + + def test_resize(self): + def resize(mode, size): + out = lena(mode).resize(size) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, size) + for mode in "1", "P", "L", "RGB", "I", "F": + resize(mode, (100, 100)) + resize(mode, (200, 200)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagefileio.py b/test/test_imagefileio.py new file mode 100644 index 000000000..73f660361 --- /dev/null +++ b/test/test_imagefileio.py @@ -0,0 +1,31 @@ +from helper import unittest, PillowTestCase, lena, tostring + +from PIL import Image +from PIL import ImageFileIO + + +class TestImageFileIo(PillowTestCase): + + def test_fileio(self): + + class DumbFile: + def __init__(self, data): + self.data = data + def read(self, bytes=None): + self.assertEqual(bytes, None) + return self.data + def close(self): + pass + + im1 = lena() + + io = ImageFileIO.ImageFileIO(DumbFile(tostring(im1, "PPM"))) + + im2 = Image.open(io) + self.assert_image_equal(im1, im2) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagetransform.py b/test/test_imagetransform.py new file mode 100644 index 000000000..f5741df32 --- /dev/null +++ b/test/test_imagetransform.py @@ -0,0 +1,27 @@ +from helper import unittest, PillowTestCase + +from PIL import Image +from PIL import ImageTransform + + +class TestImageTransform(PillowTestCase): + + def test_sanity(self): + im = Image.new("L", (100, 100)) + + seq = tuple(range(10)) + + transform = ImageTransform.AffineTransform(seq[:6]) + im.transform((100, 100), transform) + transform = ImageTransform.ExtentTransform(seq[:4]) + im.transform((100, 100), transform) + transform = ImageTransform.QuadTransform(seq[:8]) + im.transform((100, 100), transform) + transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])]) + im.transform((100, 100), transform) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_locale.py b/test/test_locale.py new file mode 100644 index 000000000..599e46266 --- /dev/null +++ b/test/test_locale.py @@ -0,0 +1,39 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + +import locale + +# ref https://github.com/python-pillow/Pillow/issues/272 +# on windows, in polish locale: + +# import locale +# print locale.setlocale(locale.LC_ALL, 'polish') +# import string +# print len(string.whitespace) +# print ord(string.whitespace[6]) + +# Polish_Poland.1250 +# 7 +# 160 + +# one of string.whitespace is not freely convertable into ascii. + +path = "Images/lena.jpg" + + +class TestLocale(PillowTestCase): + + def test_sanity(self): + Image.open(path) + try: + locale.setlocale(locale.LC_ALL, "polish") + except: + unittest.skip('Polish locale not available') + Image.open(path) + + +if __name__ == '__main__': + unittest.main() + +# End of file From cbb2f9dce941698b448c579034687c7f57145970 Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 15:26:48 +0300 Subject: [PATCH 29/33] Fix test/test_imagefileio.py --- Tests/test_image_paste.py | 5 ----- Tests/test_image_save.py | 5 ----- Tests/test_image_seek.py | 5 ----- Tests/test_image_show.py | 5 ----- Tests/test_image_tell.py | 5 ----- Tests/test_image_verify.py | 5 ----- test/test_imagefileio.py | 4 +++- 7 files changed, 3 insertions(+), 31 deletions(-) delete mode 100644 Tests/test_image_paste.py delete mode 100644 Tests/test_image_save.py delete mode 100644 Tests/test_image_seek.py delete mode 100644 Tests/test_image_show.py delete mode 100644 Tests/test_image_tell.py delete mode 100644 Tests/test_image_verify.py diff --git a/Tests/test_image_paste.py b/Tests/test_image_paste.py deleted file mode 100644 index 7d4b6d9b3..000000000 --- a/Tests/test_image_paste.py +++ /dev/null @@ -1,5 +0,0 @@ -from tester import * - -from PIL import Image - -success() diff --git a/Tests/test_image_save.py b/Tests/test_image_save.py deleted file mode 100644 index 7d4b6d9b3..000000000 --- a/Tests/test_image_save.py +++ /dev/null @@ -1,5 +0,0 @@ -from tester import * - -from PIL import Image - -success() diff --git a/Tests/test_image_seek.py b/Tests/test_image_seek.py deleted file mode 100644 index 7d4b6d9b3..000000000 --- a/Tests/test_image_seek.py +++ /dev/null @@ -1,5 +0,0 @@ -from tester import * - -from PIL import Image - -success() diff --git a/Tests/test_image_show.py b/Tests/test_image_show.py deleted file mode 100644 index 7d4b6d9b3..000000000 --- a/Tests/test_image_show.py +++ /dev/null @@ -1,5 +0,0 @@ -from tester import * - -from PIL import Image - -success() diff --git a/Tests/test_image_tell.py b/Tests/test_image_tell.py deleted file mode 100644 index 7d4b6d9b3..000000000 --- a/Tests/test_image_tell.py +++ /dev/null @@ -1,5 +0,0 @@ -from tester import * - -from PIL import Image - -success() diff --git a/Tests/test_image_verify.py b/Tests/test_image_verify.py deleted file mode 100644 index 7d4b6d9b3..000000000 --- a/Tests/test_image_verify.py +++ /dev/null @@ -1,5 +0,0 @@ -from tester import * - -from PIL import Image - -success() diff --git a/test/test_imagefileio.py b/test/test_imagefileio.py index 73f660361..32ee0bc5e 100644 --- a/test/test_imagefileio.py +++ b/test/test_imagefileio.py @@ -11,9 +11,11 @@ class TestImageFileIo(PillowTestCase): class DumbFile: def __init__(self, data): self.data = data + def read(self, bytes=None): - self.assertEqual(bytes, None) + assert(bytes is None) return self.data + def close(self): pass From c4d3898006b5d5c5af90312e15ba558e1bcd3a01 Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 16:55:00 +0300 Subject: [PATCH 30/33] Convert more tests --- Tests/test_file_ico.py | 14 --- Tests/test_font_bdf.py | 13 --- Tests/test_image_offset.py | 16 --- Tests/test_imagecms.py | 203 ----------------------------------- Tests/test_imagemode.py | 23 ---- Tests/test_imageshow.py | 6 -- Tests/test_imagetk.py | 9 -- Tests/test_imagewin.py | 6 -- test/helper.py | 5 +- test/test_file_ico.py | 23 ++++ test/test_font_bdf.py | 22 ++++ test/test_image_offset.py | 25 +++++ test/test_imagecms.py | 214 +++++++++++++++++++++++++++++++++++++ test/test_imagemode.py | 32 ++++++ test/test_imageshow.py | 18 ++++ test/test_imagetk.py | 17 +++ test/test_imagewin.py | 18 ++++ 17 files changed, 372 insertions(+), 292 deletions(-) delete mode 100644 Tests/test_file_ico.py delete mode 100644 Tests/test_font_bdf.py delete mode 100644 Tests/test_image_offset.py delete mode 100644 Tests/test_imagecms.py delete mode 100644 Tests/test_imagemode.py delete mode 100644 Tests/test_imageshow.py delete mode 100644 Tests/test_imagetk.py delete mode 100644 Tests/test_imagewin.py create mode 100644 test/test_file_ico.py create mode 100644 test/test_font_bdf.py create mode 100644 test/test_image_offset.py create mode 100644 test/test_imagecms.py create mode 100644 test/test_imagemode.py create mode 100644 test/test_imageshow.py create mode 100644 test/test_imagetk.py create mode 100644 test/test_imagewin.py diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py deleted file mode 100644 index e0db34acc..000000000 --- a/Tests/test_file_ico.py +++ /dev/null @@ -1,14 +0,0 @@ -from tester import * - -from PIL import Image - -# sample ppm stream -file = "Images/lena.ico" -data = open(file, "rb").read() - -def test_sanity(): - im = Image.open(file) - im.load() - assert_equal(im.mode, "RGBA") - assert_equal(im.size, (16, 16)) - assert_equal(im.format, "ICO") diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py deleted file mode 100644 index 366bb4468..000000000 --- a/Tests/test_font_bdf.py +++ /dev/null @@ -1,13 +0,0 @@ -from tester import * - -from PIL import Image, FontFile, BdfFontFile - -filename = "Images/courB08.bdf" - -def test_sanity(): - - file = open(filename, "rb") - font = BdfFontFile.BdfFontFile(file) - - assert_true(isinstance(font, FontFile.FontFile)) - assert_equal(len([_f for _f in font.glyph if _f]), 190) diff --git a/Tests/test_image_offset.py b/Tests/test_image_offset.py deleted file mode 100644 index f6356907a..000000000 --- a/Tests/test_image_offset.py +++ /dev/null @@ -1,16 +0,0 @@ -from tester import * - -from PIL import Image - -def test_offset(): - - im1 = lena() - - im2 = assert_warning(DeprecationWarning, lambda: im1.offset(10)) - assert_equal(im1.getpixel((0, 0)), im2.getpixel((10, 10))) - - im2 = assert_warning(DeprecationWarning, lambda: im1.offset(10, 20)) - assert_equal(im1.getpixel((0, 0)), im2.getpixel((10, 20))) - - im2 = assert_warning(DeprecationWarning, lambda: im1.offset(20, 20)) - assert_equal(im1.getpixel((0, 0)), im2.getpixel((20, 20))) diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py deleted file mode 100644 index f52101eb1..000000000 --- a/Tests/test_imagecms.py +++ /dev/null @@ -1,203 +0,0 @@ -from tester import * - -from PIL import Image -try: - from PIL import ImageCms - ImageCms.core.profile_open -except ImportError: - skip() - -SRGB = "Tests/icc/sRGB.icm" - - -def test_sanity(): - - # basic smoke test. - # this mostly follows the cms_test outline. - - v = ImageCms.versions() # should return four strings - assert_equal(v[0], '1.0.0 pil') - assert_equal(list(map(type, v)), [str, str, str, str]) - - # internal version number - assert_match(ImageCms.core.littlecms_version, "\d+\.\d+$") - - i = ImageCms.profileToProfile(lena(), SRGB, SRGB) - assert_image(i, "RGB", (128, 128)) - - i = lena() - ImageCms.profileToProfile(i, SRGB, SRGB, inPlace=True) - assert_image(i, "RGB", (128, 128)) - - t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") - i = ImageCms.applyTransform(lena(), t) - assert_image(i, "RGB", (128, 128)) - - i = lena() - t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") - ImageCms.applyTransform(lena(), t, inPlace=True) - assert_image(i, "RGB", (128, 128)) - - p = ImageCms.createProfile("sRGB") - o = ImageCms.getOpenProfile(SRGB) - t = ImageCms.buildTransformFromOpenProfiles(p, o, "RGB", "RGB") - i = ImageCms.applyTransform(lena(), t) - assert_image(i, "RGB", (128, 128)) - - t = ImageCms.buildProofTransform(SRGB, SRGB, SRGB, "RGB", "RGB") - assert_equal(t.inputMode, "RGB") - assert_equal(t.outputMode, "RGB") - i = ImageCms.applyTransform(lena(), t) - assert_image(i, "RGB", (128, 128)) - - # test PointTransform convenience API - lena().point(t) - - -def test_name(): - # get profile information for file - assert_equal(ImageCms.getProfileName(SRGB).strip(), - 'IEC 61966-2.1 Default RGB colour space - sRGB') - - -def test_info(): - assert_equal(ImageCms.getProfileInfo(SRGB).splitlines(), - ['sRGB IEC61966-2.1', '', - 'Copyright (c) 1998 Hewlett-Packard Company', '']) - - -def test_copyright(): - assert_equal(ImageCms.getProfileCopyright(SRGB).strip(), - 'Copyright (c) 1998 Hewlett-Packard Company') - - -def test_manufacturer(): - assert_equal(ImageCms.getProfileManufacturer(SRGB).strip(), - 'IEC http://www.iec.ch') - - -def test_model(): - assert_equal(ImageCms.getProfileModel(SRGB).strip(), - 'IEC 61966-2.1 Default RGB colour space - sRGB') - - -def test_description(): - assert_equal(ImageCms.getProfileDescription(SRGB).strip(), - 'sRGB IEC61966-2.1') - - -def test_intent(): - assert_equal(ImageCms.getDefaultIntent(SRGB), 0) - assert_equal(ImageCms.isIntentSupported( - SRGB, ImageCms.INTENT_ABSOLUTE_COLORIMETRIC, - ImageCms.DIRECTION_INPUT), 1) - - -def test_profile_object(): - # same, using profile object - p = ImageCms.createProfile("sRGB") -# assert_equal(ImageCms.getProfileName(p).strip(), -# 'sRGB built-in - (lcms internal)') -# assert_equal(ImageCms.getProfileInfo(p).splitlines(), -# ['sRGB built-in', '', 'WhitePoint : D65 (daylight)', '', '']) - assert_equal(ImageCms.getDefaultIntent(p), 0) - assert_equal(ImageCms.isIntentSupported( - p, ImageCms.INTENT_ABSOLUTE_COLORIMETRIC, - ImageCms.DIRECTION_INPUT), 1) - - -def test_extensions(): - # extensions - i = Image.open("Tests/images/rgb.jpg") - p = ImageCms.getOpenProfile(BytesIO(i.info["icc_profile"])) - assert_equal(ImageCms.getProfileName(p).strip(), - 'IEC 61966-2.1 Default RGB colour space - sRGB') - - -def test_exceptions(): - # the procedural pyCMS API uses PyCMSError for all sorts of errors - assert_exception( - ImageCms.PyCMSError, - lambda: ImageCms.profileToProfile(lena(), "foo", "bar")) - assert_exception( - ImageCms.PyCMSError, - lambda: ImageCms.buildTransform("foo", "bar", "RGB", "RGB")) - assert_exception( - ImageCms.PyCMSError, - lambda: ImageCms.getProfileName(None)) - assert_exception( - ImageCms.PyCMSError, - lambda: ImageCms.isIntentSupported(SRGB, None, None)) - - -def test_display_profile(): - # try fetching the profile for the current display device - assert_no_exception(lambda: ImageCms.get_display_profile()) - - -def test_lab_color_profile(): - ImageCms.createProfile("LAB", 5000) - ImageCms.createProfile("LAB", 6500) - - -def test_simple_lab(): - i = Image.new('RGB', (10, 10), (128, 128, 128)) - - pLab = ImageCms.createProfile("LAB") - t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") - - i_lab = ImageCms.applyTransform(i, t) - - assert_equal(i_lab.mode, 'LAB') - - k = i_lab.getpixel((0, 0)) - assert_equal(k, (137, 128, 128)) # not a linear luminance map. so L != 128 - - L = i_lab.getdata(0) - a = i_lab.getdata(1) - b = i_lab.getdata(2) - - assert_equal(list(L), [137] * 100) - assert_equal(list(a), [128] * 100) - assert_equal(list(b), [128] * 100) - - -def test_lab_color(): - pLab = ImageCms.createProfile("LAB") - t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") - # Need to add a type mapping for some PIL type to TYPE_Lab_8 in - # findLCMSType, and have that mapping work back to a PIL mode (likely RGB). - i = ImageCms.applyTransform(lena(), t) - assert_image(i, "LAB", (128, 128)) - - # i.save('temp.lab.tif') # visually verified vs PS. - - target = Image.open('Tests/images/lena.Lab.tif') - - assert_image_similar(i, target, 30) - - -def test_lab_srgb(): - pLab = ImageCms.createProfile("LAB") - t = ImageCms.buildTransform(pLab, SRGB, "LAB", "RGB") - - img = Image.open('Tests/images/lena.Lab.tif') - - img_srgb = ImageCms.applyTransform(img, t) - - # img_srgb.save('temp.srgb.tif') # visually verified vs ps. - - assert_image_similar(lena(), img_srgb, 30) - - -def test_lab_roundtrip(): - # check to see if we're at least internally consistent. - pLab = ImageCms.createProfile("LAB") - t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") - - t2 = ImageCms.buildTransform(pLab, SRGB, "LAB", "RGB") - - i = ImageCms.applyTransform(lena(), t) - out = ImageCms.applyTransform(i, t2) - - assert_image_similar(lena(), out, 2) diff --git a/Tests/test_imagemode.py b/Tests/test_imagemode.py deleted file mode 100644 index 54b04435f..000000000 --- a/Tests/test_imagemode.py +++ /dev/null @@ -1,23 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageMode - -ImageMode.getmode("1") -ImageMode.getmode("L") -ImageMode.getmode("P") -ImageMode.getmode("RGB") -ImageMode.getmode("I") -ImageMode.getmode("F") - -m = ImageMode.getmode("1") -assert_equal(m.mode, "1") -assert_equal(m.bands, ("1",)) -assert_equal(m.basemode, "L") -assert_equal(m.basetype, "L") - -m = ImageMode.getmode("RGB") -assert_equal(m.mode, "RGB") -assert_equal(m.bands, ("R", "G", "B")) -assert_equal(m.basemode, "RGB") -assert_equal(m.basetype, "L") diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py deleted file mode 100644 index 99ec005c8..000000000 --- a/Tests/test_imageshow.py +++ /dev/null @@ -1,6 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageShow - -success() diff --git a/Tests/test_imagetk.py b/Tests/test_imagetk.py deleted file mode 100644 index b30971e8f..000000000 --- a/Tests/test_imagetk.py +++ /dev/null @@ -1,9 +0,0 @@ -from tester import * - -from PIL import Image -try: - from PIL import ImageTk -except (OSError, ImportError) as v: - skip(v) - -success() diff --git a/Tests/test_imagewin.py b/Tests/test_imagewin.py deleted file mode 100644 index 268a75b6f..000000000 --- a/Tests/test_imagewin.py +++ /dev/null @@ -1,6 +0,0 @@ -from tester import * - -from PIL import Image -from PIL import ImageWin - -success() diff --git a/test/helper.py b/test/helper.py index ebe651120..54739cf7b 100644 --- a/test/helper.py +++ b/test/helper.py @@ -60,18 +60,19 @@ class PillowTestCase(unittest.TestCase): def assert_warning(self, warn_class, func): import warnings + result = None with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Hopefully trigger a warning. - func() + result = func() # Verify some things. self.assertEqual(len(w), 1) assert issubclass(w[-1].category, warn_class) self.assertIn("deprecated", str(w[-1].message)) - + return result # # require that deprecation warnings are triggered # import warnings diff --git a/test/test_file_ico.py b/test/test_file_ico.py new file mode 100644 index 000000000..dc289e1d2 --- /dev/null +++ b/test/test_file_ico.py @@ -0,0 +1,23 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + +# sample ppm stream +file = "Images/lena.ico" +data = open(file, "rb").read() + + +class TestFileIco(PillowTestCase): + + def test_sanity(self): + im = Image.open(file) + im.load() + self.assertEqual(im.mode, "RGBA") + self.assertEqual(im.size, (16, 16)) + self.assertEqual(im.format, "ICO") + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_font_bdf.py b/test/test_font_bdf.py new file mode 100644 index 000000000..b141e6149 --- /dev/null +++ b/test/test_font_bdf.py @@ -0,0 +1,22 @@ +from helper import unittest, PillowTestCase + +from PIL import FontFile, BdfFontFile + +filename = "Images/courB08.bdf" + + +class TestImage(PillowTestCase): + + def test_sanity(self): + + file = open(filename, "rb") + font = BdfFontFile.BdfFontFile(file) + + self.assertIsInstance(font, FontFile.FontFile) + self.assertEqual(len([_f for _f in font.glyph if _f]), 190) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_offset.py b/test/test_image_offset.py new file mode 100644 index 000000000..bb9b5a38c --- /dev/null +++ b/test/test_image_offset.py @@ -0,0 +1,25 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImage(PillowTestCase): + + def test_offset(self): + + im1 = lena() + + im2 = self.assert_warning(DeprecationWarning, lambda: im1.offset(10)) + self.assertEqual(im1.getpixel((0, 0)), im2.getpixel((10, 10))) + + im2 = self.assert_warning( + DeprecationWarning, lambda: im1.offset(10, 20)) + self.assertEqual(im1.getpixel((0, 0)), im2.getpixel((10, 20))) + + im2 = self.assert_warning( + DeprecationWarning, lambda: im1.offset(20, 20)) + self.assertEqual(im1.getpixel((0, 0)), im2.getpixel((20, 20))) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagecms.py b/test/test_imagecms.py new file mode 100644 index 000000000..1a31636e8 --- /dev/null +++ b/test/test_imagecms.py @@ -0,0 +1,214 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + +try: + from PIL import ImageCms + ImageCms.core.profile_open +except ImportError as v: + # Skipped via setUp() + pass + + +SRGB = "Tests/icc/sRGB.icm" + + +class TestImage(PillowTestCase): + + def setUp(self): + try: + from PIL import ImageCms + except ImportError as v: + self.skipTest(v) + + def test_sanity(self): + + # basic smoke test. + # this mostly follows the cms_test outline. + + v = ImageCms.versions() # should return four strings + self.assertEqual(v[0], '1.0.0 pil') + self.assertEqual(list(map(type, v)), [str, str, str, str]) + + # internal version number + self.assertRegexpMatches(ImageCms.core.littlecms_version, "\d+\.\d+$") + + i = ImageCms.profileToProfile(lena(), SRGB, SRGB) + self.assert_image(i, "RGB", (128, 128)) + + i = lena() + ImageCms.profileToProfile(i, SRGB, SRGB, inPlace=True) + self.assert_image(i, "RGB", (128, 128)) + + t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") + i = ImageCms.applyTransform(lena(), t) + self.assert_image(i, "RGB", (128, 128)) + + i = lena() + t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") + ImageCms.applyTransform(lena(), t, inPlace=True) + self.assert_image(i, "RGB", (128, 128)) + + p = ImageCms.createProfile("sRGB") + o = ImageCms.getOpenProfile(SRGB) + t = ImageCms.buildTransformFromOpenProfiles(p, o, "RGB", "RGB") + i = ImageCms.applyTransform(lena(), t) + self.assert_image(i, "RGB", (128, 128)) + + t = ImageCms.buildProofTransform(SRGB, SRGB, SRGB, "RGB", "RGB") + self.assertEqual(t.inputMode, "RGB") + self.assertEqual(t.outputMode, "RGB") + i = ImageCms.applyTransform(lena(), t) + self.assert_image(i, "RGB", (128, 128)) + + # test PointTransform convenience API + lena().point(t) + + def test_name(self): + # get profile information for file + self.assertEqual( + ImageCms.getProfileName(SRGB).strip(), + 'IEC 61966-2.1 Default RGB colour space - sRGB') + + def test_info(self): + self.assertEqual( + ImageCms.getProfileInfo(SRGB).splitlines(), [ + 'sRGB IEC61966-2.1', '', + 'Copyright (c) 1998 Hewlett-Packard Company', '']) + + def test_copyright(self): + self.assertEqual( + ImageCms.getProfileCopyright(SRGB).strip(), + 'Copyright (c) 1998 Hewlett-Packard Company') + + def test_manufacturer(self): + self.assertEqual( + ImageCms.getProfileManufacturer(SRGB).strip(), + 'IEC http://www.iec.ch') + + def test_model(self): + self.assertEqual( + ImageCms.getProfileModel(SRGB).strip(), + 'IEC 61966-2.1 Default RGB colour space - sRGB') + + def test_description(self): + self.assertEqual( + ImageCms.getProfileDescription(SRGB).strip(), + 'sRGB IEC61966-2.1') + + def test_intent(self): + self.assertEqual(ImageCms.getDefaultIntent(SRGB), 0) + self.assertEqual(ImageCms.isIntentSupported( + SRGB, ImageCms.INTENT_ABSOLUTE_COLORIMETRIC, + ImageCms.DIRECTION_INPUT), 1) + + def test_profile_object(self): + # same, using profile object + p = ImageCms.createProfile("sRGB") + # self.assertEqual(ImageCms.getProfileName(p).strip(), + # 'sRGB built-in - (lcms internal)') + # self.assertEqual(ImageCms.getProfileInfo(p).splitlines(), + # ['sRGB built-in', '', 'WhitePoint : D65 (daylight)', '', '']) + self.assertEqual(ImageCms.getDefaultIntent(p), 0) + self.assertEqual(ImageCms.isIntentSupported( + p, ImageCms.INTENT_ABSOLUTE_COLORIMETRIC, + ImageCms.DIRECTION_INPUT), 1) + + def test_extensions(self): + # extensions + from io import BytesIO + i = Image.open("Tests/images/rgb.jpg") + p = ImageCms.getOpenProfile(BytesIO(i.info["icc_profile"])) + self.assertEqual( + ImageCms.getProfileName(p).strip(), + 'IEC 61966-2.1 Default RGB colour space - sRGB') + + def test_exceptions(self): + # the procedural pyCMS API uses PyCMSError for all sorts of errors + self.assertRaises( + ImageCms.PyCMSError, + lambda: ImageCms.profileToProfile(lena(), "foo", "bar")) + self.assertRaises( + ImageCms.PyCMSError, + lambda: ImageCms.buildTransform("foo", "bar", "RGB", "RGB")) + self.assertRaises( + ImageCms.PyCMSError, + lambda: ImageCms.getProfileName(None)) + self.assertRaises( + ImageCms.PyCMSError, + lambda: ImageCms.isIntentSupported(SRGB, None, None)) + + def test_display_profile(self): + # try fetching the profile for the current display device + ImageCms.get_display_profile() + + def test_lab_color_profile(self): + ImageCms.createProfile("LAB", 5000) + ImageCms.createProfile("LAB", 6500) + + def test_simple_lab(self): + i = Image.new('RGB', (10, 10), (128, 128, 128)) + + pLab = ImageCms.createProfile("LAB") + t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") + + i_lab = ImageCms.applyTransform(i, t) + + self.assertEqual(i_lab.mode, 'LAB') + + k = i_lab.getpixel((0, 0)) + # not a linear luminance map. so L != 128: + self.assertEqual(k, (137, 128, 128)) + + L = i_lab.getdata(0) + a = i_lab.getdata(1) + b = i_lab.getdata(2) + + self.assertEqual(list(L), [137] * 100) + self.assertEqual(list(a), [128] * 100) + self.assertEqual(list(b), [128] * 100) + + def test_lab_color(self): + pLab = ImageCms.createProfile("LAB") + t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") + # Need to add a type mapping for some PIL type to TYPE_Lab_8 in + # findLCMSType, and have that mapping work back to a PIL mode + # (likely RGB). + i = ImageCms.applyTransform(lena(), t) + self.assert_image(i, "LAB", (128, 128)) + + # i.save('temp.lab.tif') # visually verified vs PS. + + target = Image.open('Tests/images/lena.Lab.tif') + + self.assert_image_similar(i, target, 30) + + def test_lab_srgb(self): + pLab = ImageCms.createProfile("LAB") + t = ImageCms.buildTransform(pLab, SRGB, "LAB", "RGB") + + img = Image.open('Tests/images/lena.Lab.tif') + + img_srgb = ImageCms.applyTransform(img, t) + + # img_srgb.save('temp.srgb.tif') # visually verified vs ps. + + self.assert_image_similar(lena(), img_srgb, 30) + + def test_lab_roundtrip(self): + # check to see if we're at least internally consistent. + pLab = ImageCms.createProfile("LAB") + t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") + + t2 = ImageCms.buildTransform(pLab, SRGB, "LAB", "RGB") + + i = ImageCms.applyTransform(lena(), t) + out = ImageCms.applyTransform(i, t2) + + self.assert_image_similar(lena(), out, 2) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagemode.py b/test/test_imagemode.py new file mode 100644 index 000000000..7fb596b46 --- /dev/null +++ b/test/test_imagemode.py @@ -0,0 +1,32 @@ +from helper import unittest, PillowTestCase + +from PIL import ImageMode + + +class TestImage(PillowTestCase): + + def test_sanity(self): + 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(m.bands, ("1",)) + self.assertEqual(m.basemode, "L") + self.assertEqual(m.basetype, "L") + + m = ImageMode.getmode("RGB") + self.assertEqual(m.mode, "RGB") + self.assertEqual(m.bands, ("R", "G", "B")) + self.assertEqual(m.basemode, "RGB") + self.assertEqual(m.basetype, "L") + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imageshow.py b/test/test_imageshow.py new file mode 100644 index 000000000..12594c98f --- /dev/null +++ b/test/test_imageshow.py @@ -0,0 +1,18 @@ +from helper import unittest, PillowTestCase + +from PIL import Image +from PIL import ImageShow + + +class TestImage(PillowTestCase): + + def test_sanity(self): + dir(Image) + dir(ImageShow) + pass + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagetk.py b/test/test_imagetk.py new file mode 100644 index 000000000..87a07e288 --- /dev/null +++ b/test/test_imagetk.py @@ -0,0 +1,17 @@ +from helper import unittest, PillowTestCase + + +class TestImageTk(PillowTestCase): + + def test_import(self): + try: + from PIL import ImageTk + dir(ImageTk) + except (OSError, ImportError) as v: + self.skipTest(v) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagewin.py b/test/test_imagewin.py new file mode 100644 index 000000000..f7a6bc0dc --- /dev/null +++ b/test/test_imagewin.py @@ -0,0 +1,18 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image +from PIL import ImageWin + + +class TestImage(PillowTestCase): + + def test_sanity(self): + dir(Image) + dir(ImageShow) + pass + + +if __name__ == '__main__': + unittest.main() + +# End of file From 3c7c89e642518de369c7c6a30f21faebbddf2824 Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 17:05:54 +0300 Subject: [PATCH 31/33] Convert more tests, fix test_imagewin.py --- Tests/test_image_getpalette.py | 19 ----------------- Tests/test_image_mode.py | 27 ------------------------ Tests/test_image_quantize.py | 27 ------------------------ {Tests => test}/test_file_xbm.py | 22 +++++++++++++------ test/test_image_getpalette.py | 26 +++++++++++++++++++++++ test/test_image_mode.py | 36 ++++++++++++++++++++++++++++++++ test/test_image_quantize.py | 35 +++++++++++++++++++++++++++++++ test/test_imagewin.py | 2 +- 8 files changed, 114 insertions(+), 80 deletions(-) delete mode 100644 Tests/test_image_getpalette.py delete mode 100644 Tests/test_image_mode.py delete mode 100644 Tests/test_image_quantize.py rename {Tests => test}/test_file_xbm.py (72%) create mode 100644 test/test_image_getpalette.py create mode 100644 test/test_image_mode.py create mode 100644 test/test_image_quantize.py diff --git a/Tests/test_image_getpalette.py b/Tests/test_image_getpalette.py deleted file mode 100644 index 5dc923b9f..000000000 --- a/Tests/test_image_getpalette.py +++ /dev/null @@ -1,19 +0,0 @@ -from tester import * - -from PIL import Image - -def test_palette(): - def palette(mode): - p = lena(mode).getpalette() - if p: - return p[:10] - return None - assert_equal(palette("1"), None) - assert_equal(palette("L"), None) - assert_equal(palette("I"), None) - assert_equal(palette("F"), None) - assert_equal(palette("P"), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) - assert_equal(palette("RGB"), None) - assert_equal(palette("RGBA"), None) - assert_equal(palette("CMYK"), None) - assert_equal(palette("YCbCr"), None) diff --git a/Tests/test_image_mode.py b/Tests/test_image_mode.py deleted file mode 100644 index cd5bd47f5..000000000 --- a/Tests/test_image_mode.py +++ /dev/null @@ -1,27 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - - im = lena() - assert_no_exception(lambda: im.mode) - -def test_properties(): - def check(mode, *result): - signature = ( - Image.getmodebase(mode), Image.getmodetype(mode), - Image.getmodebands(mode), Image.getmodebandnames(mode), - ) - assert_equal(signature, result) - check("1", "L", "L", 1, ("1",)) - check("L", "L", "L", 1, ("L",)) - check("P", "RGB", "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")) diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py deleted file mode 100644 index dbf68a25e..000000000 --- a/Tests/test_image_quantize.py +++ /dev/null @@ -1,27 +0,0 @@ -from tester import * - -from PIL import Image - -def test_sanity(): - - im = lena() - - im = im.quantize() - assert_image(im, "P", im.size) - - im = lena() - im = im.quantize(palette=lena("P")) - assert_image(im, "P", im.size) - -def test_octree_quantize(): - im = lena() - - im = im.quantize(100, Image.FASTOCTREE) - assert_image(im, "P", im.size) - - assert len(im.getcolors()) == 100 - -def test_rgba_quantize(): - im = lena('RGBA') - assert_no_exception(lambda: im.quantize()) - assert_exception(Exception, lambda: im.quantize(method=0)) diff --git a/Tests/test_file_xbm.py b/test/test_file_xbm.py similarity index 72% rename from Tests/test_file_xbm.py rename to test/test_file_xbm.py index f27a3a349..02aec70b1 100644 --- a/Tests/test_file_xbm.py +++ b/test/test_file_xbm.py @@ -1,4 +1,4 @@ -from tester import * +from helper import unittest, PillowTestCase from PIL import Image @@ -25,10 +25,20 @@ static char basic_bits[] = { }; """ -def test_pil151(): - im = Image.open(BytesIO(PIL151)) +class TestFileXbm(PillowTestCase): - assert_no_exception(lambda: im.load()) - assert_equal(im.mode, '1') - assert_equal(im.size, (32, 32)) + def test_pil151(self): + from io import BytesIO + + im = Image.open(BytesIO(PIL151)) + + im.load() + self.assertEqual(im.mode, '1') + self.assertEqual(im.size, (32, 32)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_getpalette.py b/test/test_image_getpalette.py new file mode 100644 index 000000000..0c399c432 --- /dev/null +++ b/test/test_image_getpalette.py @@ -0,0 +1,26 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImageGetPalette(PillowTestCase): + + def test_palette(self): + def palette(mode): + p = lena(mode).getpalette() + if p: + return p[:10] + return None + self.assertEqual(palette("1"), None) + self.assertEqual(palette("L"), None) + self.assertEqual(palette("I"), None) + self.assertEqual(palette("F"), None) + self.assertEqual(palette("P"), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + self.assertEqual(palette("RGB"), None) + self.assertEqual(palette("RGBA"), None) + self.assertEqual(palette("CMYK"), None) + self.assertEqual(palette("YCbCr"), None) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_mode.py b/test/test_image_mode.py new file mode 100644 index 000000000..d229a27a2 --- /dev/null +++ b/test/test_image_mode.py @@ -0,0 +1,36 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImage(PillowTestCase): + + def test_sanity(self): + + im = lena() + im.mode + + 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", "RGB", "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")) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_image_quantize.py b/test/test_image_quantize.py new file mode 100644 index 000000000..35f876717 --- /dev/null +++ b/test/test_image_quantize.py @@ -0,0 +1,35 @@ +from helper import unittest, PillowTestCase, lena + +from PIL import Image + + +class TestImage(PillowTestCase): + + def test_sanity(self): + im = lena() + + im = im.quantize() + self.assert_image(im, "P", im.size) + + im = lena() + im = im.quantize(palette=lena("P")) + self.assert_image(im, "P", im.size) + + def test_octree_quantize(self): + im = lena() + + im = im.quantize(100, Image.FASTOCTREE) + self.assert_image(im, "P", im.size) + + assert len(im.getcolors()) == 100 + + def test_rgba_quantize(self): + im = lena('RGBA') + im.quantize() + self.assertRaises(Exception, lambda: im.quantize(method=0)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_imagewin.py b/test/test_imagewin.py index f7a6bc0dc..916abc77b 100644 --- a/test/test_imagewin.py +++ b/test/test_imagewin.py @@ -8,7 +8,7 @@ class TestImage(PillowTestCase): def test_sanity(self): dir(Image) - dir(ImageShow) + dir(ImageWin) pass From 5919c87afb1a6c6f1ab1d2787e0c79fee5e1ad5f Mon Sep 17 00:00:00 2001 From: hugovk Date: Fri, 6 Jun 2014 17:13:29 +0300 Subject: [PATCH 32/33] Convert more tests --- Tests/test_image_getextrema.py | 17 ---- Tests/test_lib_pack.py | 138 ------------------------------- test/test_image_getextrema.py | 27 ++++++ test/test_lib_pack.py | 147 +++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 155 deletions(-) delete mode 100644 Tests/test_image_getextrema.py delete mode 100644 Tests/test_lib_pack.py create mode 100644 test/test_image_getextrema.py create mode 100644 test/test_lib_pack.py diff --git a/Tests/test_image_getextrema.py b/Tests/test_image_getextrema.py deleted file mode 100644 index 86106cde0..000000000 --- a/Tests/test_image_getextrema.py +++ /dev/null @@ -1,17 +0,0 @@ -from tester import * - -from PIL import Image - -def test_extrema(): - - def extrema(mode): - return lena(mode).getextrema() - - assert_equal(extrema("1"), (0, 255)) - assert_equal(extrema("L"), (40, 235)) - assert_equal(extrema("I"), (40, 235)) - assert_equal(extrema("F"), (40.0, 235.0)) - assert_equal(extrema("P"), (11, 218)) # fixed palette - assert_equal(extrema("RGB"), ((61, 255), (26, 234), (44, 223))) - assert_equal(extrema("RGBA"), ((61, 255), (26, 234), (44, 223), (255, 255))) - assert_equal(extrema("CMYK"), ((0, 194), (21, 229), (32, 211), (0, 0))) diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py deleted file mode 100644 index 7675348b3..000000000 --- a/Tests/test_lib_pack.py +++ /dev/null @@ -1,138 +0,0 @@ -from tester import * - -from PIL import Image - -def pack(): - pass # not yet - -def test_pack(): - - def pack(mode, rawmode): - if len(mode) == 1: - im = Image.new(mode, (1, 1), 1) - else: - im = Image.new(mode, (1, 1), (1, 2, 3, 4)[:len(mode)]) - - if py3: - return list(im.tobytes("raw", rawmode)) - else: - return [ord(c) for c in im.tobytes("raw", rawmode)] - - order = 1 if Image._ENDIAN == '<' else -1 - - assert_equal(pack("1", "1"), [128]) - assert_equal(pack("1", "1;I"), [0]) - assert_equal(pack("1", "1;R"), [1]) - assert_equal(pack("1", "1;IR"), [0]) - - assert_equal(pack("L", "L"), [1]) - - assert_equal(pack("I", "I"), [1, 0, 0, 0][::order]) - - assert_equal(pack("F", "F"), [0, 0, 128, 63][::order]) - - assert_equal(pack("LA", "LA"), [1, 2]) - - assert_equal(pack("RGB", "RGB"), [1, 2, 3]) - assert_equal(pack("RGB", "RGB;L"), [1, 2, 3]) - assert_equal(pack("RGB", "BGR"), [3, 2, 1]) - assert_equal(pack("RGB", "RGBX"), [1, 2, 3, 255]) # 255? - assert_equal(pack("RGB", "BGRX"), [3, 2, 1, 0]) - assert_equal(pack("RGB", "XRGB"), [0, 1, 2, 3]) - assert_equal(pack("RGB", "XBGR"), [0, 3, 2, 1]) - - assert_equal(pack("RGBX", "RGBX"), [1, 2, 3, 4]) # 4->255? - - assert_equal(pack("RGBA", "RGBA"), [1, 2, 3, 4]) - - assert_equal(pack("CMYK", "CMYK"), [1, 2, 3, 4]) - assert_equal(pack("YCbCr", "YCbCr"), [1, 2, 3]) - -def test_unpack(): - - def unpack(mode, rawmode, bytes_): - im = None - - if py3: - data = bytes(range(1,bytes_+1)) - else: - data = ''.join(chr(i) for i in range(1,bytes_+1)) - - im = Image.frombytes(mode, (1, 1), data, "raw", rawmode, 0, 1) - - return im.getpixel((0, 0)) - - def unpack_1(mode, rawmode, value): - assert mode == "1" - im = None - - if py3: - im = Image.frombytes(mode, (8, 1), bytes([value]), "raw", rawmode, 0, 1) - else: - im = Image.frombytes(mode, (8, 1), chr(value), "raw", rawmode, 0, 1) - - return tuple(im.getdata()) - - X = 255 - - assert_equal(unpack_1("1", "1", 1), (0,0,0,0,0,0,0,X)) - assert_equal(unpack_1("1", "1;I", 1), (X,X,X,X,X,X,X,0)) - assert_equal(unpack_1("1", "1;R", 1), (X,0,0,0,0,0,0,0)) - assert_equal(unpack_1("1", "1;IR", 1), (0,X,X,X,X,X,X,X)) - - assert_equal(unpack_1("1", "1", 170), (X,0,X,0,X,0,X,0)) - assert_equal(unpack_1("1", "1;I", 170), (0,X,0,X,0,X,0,X)) - assert_equal(unpack_1("1", "1;R", 170), (0,X,0,X,0,X,0,X)) - assert_equal(unpack_1("1", "1;IR", 170), (X,0,X,0,X,0,X,0)) - - assert_equal(unpack("L", "L;2", 1), 0) - assert_equal(unpack("L", "L;4", 1), 0) - assert_equal(unpack("L", "L", 1), 1) - assert_equal(unpack("L", "L;I", 1), 254) - assert_equal(unpack("L", "L;R", 1), 128) - assert_equal(unpack("L", "L;16", 2), 2) # little endian - assert_equal(unpack("L", "L;16B", 2), 1) # big endian - - assert_equal(unpack("LA", "LA", 2), (1, 2)) - assert_equal(unpack("LA", "LA;L", 2), (1, 2)) - - assert_equal(unpack("RGB", "RGB", 3), (1, 2, 3)) - assert_equal(unpack("RGB", "RGB;L", 3), (1, 2, 3)) - assert_equal(unpack("RGB", "RGB;R", 3), (128, 64, 192)) - assert_equal(unpack("RGB", "RGB;16B", 6), (1, 3, 5)) # ? - assert_equal(unpack("RGB", "BGR", 3), (3, 2, 1)) - assert_equal(unpack("RGB", "RGB;15", 2), (8, 131, 0)) - assert_equal(unpack("RGB", "BGR;15", 2), (0, 131, 8)) - assert_equal(unpack("RGB", "RGB;16", 2), (8, 64, 0)) - assert_equal(unpack("RGB", "BGR;16", 2), (0, 64, 8)) - assert_equal(unpack("RGB", "RGB;4B", 2), (17, 0, 34)) - - assert_equal(unpack("RGB", "RGBX", 4), (1, 2, 3)) - assert_equal(unpack("RGB", "BGRX", 4), (3, 2, 1)) - assert_equal(unpack("RGB", "XRGB", 4), (2, 3, 4)) - assert_equal(unpack("RGB", "XBGR", 4), (4, 3, 2)) - - assert_equal(unpack("RGBA", "RGBA", 4), (1, 2, 3, 4)) - assert_equal(unpack("RGBA", "BGRA", 4), (3, 2, 1, 4)) - assert_equal(unpack("RGBA", "ARGB", 4), (2, 3, 4, 1)) - assert_equal(unpack("RGBA", "ABGR", 4), (4, 3, 2, 1)) - assert_equal(unpack("RGBA", "RGBA;15", 2), (8, 131, 0, 0)) - assert_equal(unpack("RGBA", "BGRA;15", 2), (0, 131, 8, 0)) - assert_equal(unpack("RGBA", "RGBA;4B", 2), (17, 0, 34, 0)) - - assert_equal(unpack("RGBX", "RGBX", 4), (1, 2, 3, 4)) # 4->255? - assert_equal(unpack("RGBX", "BGRX", 4), (3, 2, 1, 255)) - assert_equal(unpack("RGBX", "XRGB", 4), (2, 3, 4, 255)) - assert_equal(unpack("RGBX", "XBGR", 4), (4, 3, 2, 255)) - assert_equal(unpack("RGBX", "RGB;15", 2), (8, 131, 0, 255)) - assert_equal(unpack("RGBX", "BGR;15", 2), (0, 131, 8, 255)) - assert_equal(unpack("RGBX", "RGB;4B", 2), (17, 0, 34, 255)) - - assert_equal(unpack("CMYK", "CMYK", 4), (1, 2, 3, 4)) - assert_equal(unpack("CMYK", "CMYK;I", 4), (254, 253, 252, 251)) - - assert_exception(ValueError, lambda: unpack("L", "L", 0)) - assert_exception(ValueError, lambda: unpack("RGB", "RGB", 2)) - assert_exception(ValueError, lambda: unpack("CMYK", "CMYK", 2)) - -run() diff --git a/test/test_image_getextrema.py b/test/test_image_getextrema.py new file mode 100644 index 000000000..af7f7698a --- /dev/null +++ b/test/test_image_getextrema.py @@ -0,0 +1,27 @@ +from helper import unittest, PillowTestCase, lena + + +class TestImageGetExtrema(PillowTestCase): + + def test_extrema(self): + + def extrema(mode): + return lena(mode).getextrema() + + self.assertEqual(extrema("1"), (0, 255)) + self.assertEqual(extrema("L"), (40, 235)) + self.assertEqual(extrema("I"), (40, 235)) + self.assertEqual(extrema("F"), (40.0, 235.0)) + self.assertEqual(extrema("P"), (11, 218)) # fixed palette + self.assertEqual( + extrema("RGB"), ((61, 255), (26, 234), (44, 223))) + self.assertEqual( + extrema("RGBA"), ((61, 255), (26, 234), (44, 223), (255, 255))) + self.assertEqual( + extrema("CMYK"), ((0, 194), (21, 229), (32, 211), (0, 0))) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/test/test_lib_pack.py b/test/test_lib_pack.py new file mode 100644 index 000000000..102835b58 --- /dev/null +++ b/test/test_lib_pack.py @@ -0,0 +1,147 @@ +from helper import unittest, PillowTestCase, py3 + +from PIL import Image + + +class TestLibPack(PillowTestCase): + + def pack(self): + pass # not yet + + def test_pack(self): + + def pack(mode, rawmode): + if len(mode) == 1: + im = Image.new(mode, (1, 1), 1) + else: + im = Image.new(mode, (1, 1), (1, 2, 3, 4)[:len(mode)]) + + if py3: + return list(im.tobytes("raw", rawmode)) + else: + return [ord(c) for c in im.tobytes("raw", rawmode)] + + order = 1 if Image._ENDIAN == '<' else -1 + + self.assertEqual(pack("1", "1"), [128]) + self.assertEqual(pack("1", "1;I"), [0]) + self.assertEqual(pack("1", "1;R"), [1]) + self.assertEqual(pack("1", "1;IR"), [0]) + + self.assertEqual(pack("L", "L"), [1]) + + self.assertEqual(pack("I", "I"), [1, 0, 0, 0][::order]) + + self.assertEqual(pack("F", "F"), [0, 0, 128, 63][::order]) + + self.assertEqual(pack("LA", "LA"), [1, 2]) + + self.assertEqual(pack("RGB", "RGB"), [1, 2, 3]) + self.assertEqual(pack("RGB", "RGB;L"), [1, 2, 3]) + self.assertEqual(pack("RGB", "BGR"), [3, 2, 1]) + self.assertEqual(pack("RGB", "RGBX"), [1, 2, 3, 255]) # 255? + self.assertEqual(pack("RGB", "BGRX"), [3, 2, 1, 0]) + self.assertEqual(pack("RGB", "XRGB"), [0, 1, 2, 3]) + self.assertEqual(pack("RGB", "XBGR"), [0, 3, 2, 1]) + + self.assertEqual(pack("RGBX", "RGBX"), [1, 2, 3, 4]) # 4->255? + + self.assertEqual(pack("RGBA", "RGBA"), [1, 2, 3, 4]) + + self.assertEqual(pack("CMYK", "CMYK"), [1, 2, 3, 4]) + self.assertEqual(pack("YCbCr", "YCbCr"), [1, 2, 3]) + + def test_unpack(self): + + def unpack(mode, rawmode, bytes_): + im = None + + if py3: + data = bytes(range(1, bytes_+1)) + else: + data = ''.join(chr(i) for i in range(1, bytes_+1)) + + im = Image.frombytes(mode, (1, 1), data, "raw", rawmode, 0, 1) + + return im.getpixel((0, 0)) + + def unpack_1(mode, rawmode, value): + assert mode == "1" + im = None + + if py3: + im = Image.frombytes( + mode, (8, 1), bytes([value]), "raw", rawmode, 0, 1) + else: + im = Image.frombytes( + mode, (8, 1), chr(value), "raw", rawmode, 0, 1) + + return tuple(im.getdata()) + + X = 255 + + self.assertEqual(unpack_1("1", "1", 1), (0, 0, 0, 0, 0, 0, 0, X)) + self.assertEqual(unpack_1("1", "1;I", 1), (X, X, X, X, X, X, X, 0)) + self.assertEqual(unpack_1("1", "1;R", 1), (X, 0, 0, 0, 0, 0, 0, 0)) + self.assertEqual(unpack_1("1", "1;IR", 1), (0, X, X, X, X, X, X, X)) + + self.assertEqual(unpack_1("1", "1", 170), (X, 0, X, 0, X, 0, X, 0)) + self.assertEqual(unpack_1("1", "1;I", 170), (0, X, 0, X, 0, X, 0, X)) + self.assertEqual(unpack_1("1", "1;R", 170), (0, X, 0, X, 0, X, 0, X)) + self.assertEqual(unpack_1("1", "1;IR", 170), (X, 0, X, 0, X, 0, X, 0)) + + self.assertEqual(unpack("L", "L;2", 1), 0) + self.assertEqual(unpack("L", "L;4", 1), 0) + self.assertEqual(unpack("L", "L", 1), 1) + self.assertEqual(unpack("L", "L;I", 1), 254) + self.assertEqual(unpack("L", "L;R", 1), 128) + self.assertEqual(unpack("L", "L;16", 2), 2) # little endian + self.assertEqual(unpack("L", "L;16B", 2), 1) # big endian + + self.assertEqual(unpack("LA", "LA", 2), (1, 2)) + self.assertEqual(unpack("LA", "LA;L", 2), (1, 2)) + + self.assertEqual(unpack("RGB", "RGB", 3), (1, 2, 3)) + self.assertEqual(unpack("RGB", "RGB;L", 3), (1, 2, 3)) + self.assertEqual(unpack("RGB", "RGB;R", 3), (128, 64, 192)) + self.assertEqual(unpack("RGB", "RGB;16B", 6), (1, 3, 5)) # ? + self.assertEqual(unpack("RGB", "BGR", 3), (3, 2, 1)) + self.assertEqual(unpack("RGB", "RGB;15", 2), (8, 131, 0)) + self.assertEqual(unpack("RGB", "BGR;15", 2), (0, 131, 8)) + self.assertEqual(unpack("RGB", "RGB;16", 2), (8, 64, 0)) + self.assertEqual(unpack("RGB", "BGR;16", 2), (0, 64, 8)) + self.assertEqual(unpack("RGB", "RGB;4B", 2), (17, 0, 34)) + + self.assertEqual(unpack("RGB", "RGBX", 4), (1, 2, 3)) + self.assertEqual(unpack("RGB", "BGRX", 4), (3, 2, 1)) + self.assertEqual(unpack("RGB", "XRGB", 4), (2, 3, 4)) + self.assertEqual(unpack("RGB", "XBGR", 4), (4, 3, 2)) + + self.assertEqual(unpack("RGBA", "RGBA", 4), (1, 2, 3, 4)) + self.assertEqual(unpack("RGBA", "BGRA", 4), (3, 2, 1, 4)) + self.assertEqual(unpack("RGBA", "ARGB", 4), (2, 3, 4, 1)) + self.assertEqual(unpack("RGBA", "ABGR", 4), (4, 3, 2, 1)) + self.assertEqual(unpack("RGBA", "RGBA;15", 2), (8, 131, 0, 0)) + self.assertEqual(unpack("RGBA", "BGRA;15", 2), (0, 131, 8, 0)) + self.assertEqual(unpack("RGBA", "RGBA;4B", 2), (17, 0, 34, 0)) + + self.assertEqual(unpack("RGBX", "RGBX", 4), (1, 2, 3, 4)) # 4->255? + self.assertEqual(unpack("RGBX", "BGRX", 4), (3, 2, 1, 255)) + self.assertEqual(unpack("RGBX", "XRGB", 4), (2, 3, 4, 255)) + self.assertEqual(unpack("RGBX", "XBGR", 4), (4, 3, 2, 255)) + self.assertEqual(unpack("RGBX", "RGB;15", 2), (8, 131, 0, 255)) + self.assertEqual(unpack("RGBX", "BGR;15", 2), (0, 131, 8, 255)) + self.assertEqual(unpack("RGBX", "RGB;4B", 2), (17, 0, 34, 255)) + + self.assertEqual(unpack("CMYK", "CMYK", 4), (1, 2, 3, 4)) + self.assertEqual(unpack("CMYK", "CMYK;I", 4), (254, 253, 252, 251)) + + self.assertRaises(ValueError, lambda: unpack("L", "L", 0)) + self.assertRaises(ValueError, lambda: unpack("RGB", "RGB", 2)) + self.assertRaises(ValueError, lambda: unpack("CMYK", "CMYK", 2)) + + +if __name__ == '__main__': + unittest.main() + +# End of file From 8693c63173928fb18bdd82fb7f3a450ddab95bfd Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 7 Jun 2014 00:23:55 +0300 Subject: [PATCH 33/33] assert_warning() checks in case of multiple warnings --- test/helper.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/helper.py b/test/helper.py index 54739cf7b..567fc3945 100644 --- a/test/helper.py +++ b/test/helper.py @@ -69,9 +69,13 @@ class PillowTestCase(unittest.TestCase): result = func() # Verify some things. - self.assertEqual(len(w), 1) - assert issubclass(w[-1].category, warn_class) - self.assertIn("deprecated", str(w[-1].message)) + self.assertGreaterEqual(len(w), 1) + found = False + for v in w: + if issubclass(v.category, warn_class): + found = True + break + self.assertTrue(found) return result # # require that deprecation warnings are triggered