2014-06-10 13:12:53 +04:00
|
|
|
"""
|
|
|
|
Helper functions.
|
|
|
|
"""
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
2014-06-26 04:08:24 +04:00
|
|
|
import tempfile
|
|
|
|
import os
|
2014-06-10 13:12:53 +04:00
|
|
|
|
|
|
|
if sys.version_info[:2] <= (2, 6):
|
|
|
|
import unittest2 as unittest
|
|
|
|
else:
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class PillowTestCase(unittest.TestCase):
|
|
|
|
|
2014-06-26 04:08:24 +04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
2014-07-07 21:03:50 +04:00
|
|
|
# holds last result object passed to run method:
|
|
|
|
self.currentResult = None
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2014-07-19 19:46:12 +04:00
|
|
|
# Nicer output for --verbose
|
|
|
|
def __str__(self):
|
|
|
|
return self.__class__.__name__ + "." + self._testMethodName
|
|
|
|
|
2014-06-10 13:12:53 +04:00
|
|
|
def run(self, result=None):
|
|
|
|
self.currentResult = result # remember result for use later
|
|
|
|
unittest.TestCase.run(self, result) # call superclass run method
|
|
|
|
|
2014-06-26 04:08:24 +04:00
|
|
|
def delete_tempfile(self, path):
|
2014-06-10 13:12:53 +04:00
|
|
|
try:
|
|
|
|
ok = self.currentResult.wasSuccessful()
|
|
|
|
except AttributeError: # for nosetests
|
|
|
|
proxy = self.currentResult
|
|
|
|
ok = (len(proxy.errors) + len(proxy.failures) == 0)
|
|
|
|
|
|
|
|
if ok:
|
|
|
|
# only clean out tempfiles if test passed
|
|
|
|
try:
|
2014-06-26 04:08:24 +04:00
|
|
|
os.remove(path)
|
2014-06-10 13:12:53 +04:00
|
|
|
except OSError:
|
2014-06-26 04:08:24 +04:00
|
|
|
pass # report?
|
|
|
|
else:
|
2014-07-07 21:03:50 +04:00
|
|
|
print("=== orphaned temp file: %s" % path)
|
2014-06-10 13:12:53 +04:00
|
|
|
|
|
|
|
def assert_almost_equal(self, a, b, msg=None, eps=1e-6):
|
|
|
|
self.assertLess(
|
|
|
|
abs(a-b), eps,
|
|
|
|
msg or "got %r, expected %r" % (a, b))
|
|
|
|
|
|
|
|
def assert_deep_equal(self, a, b, msg=None):
|
|
|
|
try:
|
|
|
|
self.assertEqual(
|
|
|
|
len(a), len(b),
|
|
|
|
msg or "got length %s, expected %s" % (len(a), len(b)))
|
|
|
|
self.assertTrue(
|
|
|
|
all([x == y for x, y in zip(a, b)]),
|
|
|
|
msg or "got %s, expected %s" % (a, b))
|
|
|
|
except:
|
|
|
|
self.assertEqual(a, b, msg)
|
|
|
|
|
|
|
|
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,
|
|
|
|
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))
|
2014-06-30 01:39:32 +04:00
|
|
|
if a.tobytes() != b.tobytes():
|
|
|
|
self.fail(msg or "got different content")
|
2014-06-10 13:12:53 +04:00
|
|
|
|
|
|
|
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,
|
2014-08-28 15:44:19 +04:00
|
|
|
(msg or '') +
|
|
|
|
" average pixel value difference %.4f > epsilon %.4f" % (
|
2014-06-10 13:12:53 +04:00
|
|
|
ave_diff, epsilon))
|
|
|
|
|
|
|
|
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.
|
|
|
|
result = func()
|
|
|
|
|
|
|
|
# Verify some things.
|
|
|
|
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
|
|
|
|
|
2014-07-24 01:31:49 +04:00
|
|
|
def skipKnownBadTest(self, msg=None, platform=None,
|
|
|
|
travis=None, interpreter=None):
|
2014-07-05 21:56:40 +04:00
|
|
|
# Skip if platform/travis matches, and
|
|
|
|
# PILLOW_RUN_KNOWN_BAD is not true in the environment.
|
|
|
|
if bool(os.environ.get('PILLOW_RUN_KNOWN_BAD', False)):
|
|
|
|
print (os.environ.get('PILLOW_RUN_KNOWN_BAD', False))
|
|
|
|
return
|
|
|
|
|
|
|
|
skip = True
|
|
|
|
if platform is not None:
|
|
|
|
skip = sys.platform.startswith(platform)
|
|
|
|
if travis is not None:
|
2014-07-07 21:03:50 +04:00
|
|
|
skip = skip and (travis == bool(os.environ.get('TRAVIS', False)))
|
2014-07-24 01:31:49 +04:00
|
|
|
if interpreter is not None:
|
2014-08-28 15:44:19 +04:00
|
|
|
skip = skip and (interpreter == 'pypy' and
|
|
|
|
hasattr(sys, 'pypy_version_info'))
|
2014-07-05 21:56:40 +04:00
|
|
|
if skip:
|
|
|
|
self.skipTest(msg or "Known Bad Test")
|
|
|
|
|
2014-06-26 04:08:24 +04:00
|
|
|
def tempfile(self, template):
|
|
|
|
assert template[:5] in ("temp.", "temp_")
|
|
|
|
(fd, path) = tempfile.mkstemp(template[4:], template[:4])
|
|
|
|
os.close(fd)
|
2014-07-07 21:03:50 +04:00
|
|
|
|
|
|
|
self.addCleanup(self.delete_tempfile, path)
|
2014-06-26 04:08:24 +04:00
|
|
|
return path
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2014-07-05 21:25:16 +04:00
|
|
|
def open_withImagemagick(self, f):
|
|
|
|
if not imagemagick_available():
|
|
|
|
raise IOError()
|
|
|
|
|
|
|
|
outfile = self.tempfile("temp.png")
|
2014-09-22 04:50:07 +04:00
|
|
|
if command_succeeds([IMCONVERT, f, outfile]):
|
2014-07-05 21:25:16 +04:00
|
|
|
from PIL import Image
|
|
|
|
return Image.open(outfile)
|
|
|
|
raise IOError()
|
2014-07-07 21:03:50 +04:00
|
|
|
|
|
|
|
|
2014-06-24 15:03:10 +04:00
|
|
|
# helpers
|
2014-06-10 13:12:53 +04:00
|
|
|
|
|
|
|
import sys
|
|
|
|
py3 = (sys.version_info >= (3, 0))
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2014-09-04 09:03:55 +04:00
|
|
|
def hopper(mode="RGB", cache={}):
|
|
|
|
from PIL import Image
|
|
|
|
im = None
|
|
|
|
# FIXME: Implement caching to reduce reading from disk but so an original
|
|
|
|
# copy is returned each time and the cached image isn't modified by tests
|
|
|
|
# (for fast, isolated, repeatable tests).
|
|
|
|
# im = cache.get(mode)
|
|
|
|
if im is None:
|
|
|
|
if mode == "RGB":
|
|
|
|
im = Image.open("Tests/images/hopper.ppm")
|
|
|
|
elif mode == "F":
|
2014-09-05 13:36:24 +04:00
|
|
|
im = hopper("L").convert(mode)
|
2014-09-04 09:03:55 +04:00
|
|
|
elif mode[:4] == "I;16":
|
2014-09-05 13:36:24 +04:00
|
|
|
im = hopper("I").convert(mode)
|
2014-09-04 09:03:55 +04:00
|
|
|
else:
|
2014-09-05 13:36:24 +04:00
|
|
|
im = hopper("RGB").convert(mode)
|
2014-09-04 09:03:55 +04:00
|
|
|
# cache[mode] = im
|
|
|
|
return im
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
|
|
|
|
def command_succeeds(cmd):
|
|
|
|
"""
|
|
|
|
Runs the command, which must be a list of strings. Returns True if the
|
|
|
|
command succeeds, or False if an OSError was raised by subprocess.Popen.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
with open(os.devnull, 'w') as f:
|
|
|
|
try:
|
|
|
|
subprocess.Popen(cmd, stdout=f, stderr=subprocess.STDOUT).wait()
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
def djpeg_available():
|
|
|
|
return command_succeeds(['djpeg', '--help'])
|
|
|
|
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
def cjpeg_available():
|
|
|
|
return command_succeeds(['cjpeg', '--help'])
|
|
|
|
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
def netpbm_available():
|
2014-07-07 21:03:50 +04:00
|
|
|
return (command_succeeds(["ppmquant", "--help"]) and
|
|
|
|
command_succeeds(["ppmtogif", "--help"]))
|
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
|
2014-07-05 21:25:16 +04:00
|
|
|
def imagemagick_available():
|
2014-09-22 04:50:07 +04:00
|
|
|
return IMCONVERT and command_succeeds([IMCONVERT, '-version'])
|
|
|
|
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
IMCONVERT = os.environ.get('MAGICK_HOME', '')
|
|
|
|
if IMCONVERT:
|
|
|
|
IMCONVERT = os.path.join(IMCONVERT, 'convert.exe')
|
|
|
|
else:
|
|
|
|
IMCONVERT = 'convert'
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2014-06-24 15:03:10 +04:00
|
|
|
# End of file
|