Convert more tests

This commit is contained in:
hugovk 2014-06-05 13:39:45 +03:00
parent 34db40ddce
commit 3fda42d280
9 changed files with 290 additions and 257 deletions

View File

@ -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")

View File

@ -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))

View File

@ -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)

View File

@ -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)

View File

@ -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

23
test/test_file_fli.py Normal file
View File

@ -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

View File

@ -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

View File

@ -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

145
test/test_imagefont.py Normal file
View File

@ -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