py3k: Import Christoph Gohlke's test suite

This is Christoph Gohlke's test suite from his personal PIL package found
at http://www.lfd.uci.edu/~gohlke/pythonlibs/.

This is just to bring it in as a separate commit. Future commits will align
it with Pillow.
This commit is contained in:
Brian Crowell 2012-10-15 15:26:38 -05:00 committed by Brian Crowell
parent 9631d42b60
commit ad784eb808
109 changed files with 3572 additions and 0 deletions

4
Tests/README.txt Normal file
View File

@ -0,0 +1,4 @@
Minimalistic PIL test framework.
Test scripts are named "test_xxx" and are supposed to output "ok".
That's it.

20
Tests/bench_get.py Normal file
View File

@ -0,0 +1,20 @@
import sys
sys.path.insert(0, ".")
import tester
import timeit
def bench(mode):
im = tester.lena(mode)
get = im.im.getpixel
xy = 50, 50 # position shouldn't really matter
t0 = timeit.default_timer()
for i in range(1000000):
get(xy)
print(mode, timeit.default_timer() - t0, "us")
bench("L")
bench("I")
bench("I;16")
bench("F")
bench("RGB")

222
Tests/cms_test.py Normal file
View File

@ -0,0 +1,222 @@
# PyCMSTests.py
# Examples of how to use pyCMS, as well as tests to verify it works properly
# By Kevin Cazabon (kevin@cazabon.com)
# Imports
import os
from PIL import Image
from PIL import ImageCms
# import PyCMSError separately so we can catch it
PyCMSError = ImageCms.PyCMSError
#######################################################################
# Configuration:
#######################################################################
# set this to the image you want to test with
IMAGE = "c:\\temp\\test.tif"
# set this to where you want to save the output images
OUTPUTDIR = "c:\\temp\\"
# set these to two different ICC profiles, one for input, one for output
# set the corresponding mode to the proper PIL mode for that profile
INPUT_PROFILE = "c:\\temp\\profiles\\sRGB.icm"
INMODE = "RGB"
OUTPUT_PROFILE = "c:\\temp\\profiles\\genericRGB.icm"
OUTMODE = "RGB"
PROOF_PROFILE = "c:\\temp\\profiles\\monitor.icm"
# set to True to show() images, False to save them into OUTPUT_DIRECTORY
SHOW = False
# Tests you can enable/disable
TEST_error_catching = True
TEST_profileToProfile = True
TEST_profileToProfile_inPlace = True
TEST_buildTransform = True
TEST_buildTransformFromOpenProfiles = True
TEST_buildProofTransform = True
TEST_getProfileInfo = True
TEST_misc = False
#######################################################################
# helper functions
#######################################################################
def outputImage(im, funcName = None):
# save or display the image, depending on value of SHOW_IMAGES
if SHOW == True:
im.show()
else:
im.save(os.path.join(OUTPUTDIR, "%s.tif" %funcName))
#######################################################################
# The tests themselves
#######################################################################
if TEST_error_catching == True:
im = Image.open(IMAGE)
try:
#neither of these proifles exists (unless you make them), so we should
# get an error
imOut = ImageCms.profileToProfile(im, "missingProfile.icm", "cmyk.icm")
except PyCMSError as reason:
print("We caught a PyCMSError: %s\n\n" %reason)
print("error catching test completed successfully (if you see the message \
above that we caught the error).")
if TEST_profileToProfile == True:
# open the image file using the standard PIL function Image.open()
im = Image.open(IMAGE)
# send the image, input/output profiles, and rendering intent to
# ImageCms.profileToProfile()
imOut = ImageCms.profileToProfile(im, INPUT_PROFILE, OUTPUT_PROFILE, \
outputMode = OUTMODE)
# now that the image is converted, save or display it
outputImage(imOut, "profileToProfile")
print("profileToProfile test completed successfully.")
if TEST_profileToProfile_inPlace == True:
# we'll do the same test as profileToProfile, but modify im in place
# instead of getting a new image returned to us
im = Image.open(IMAGE)
# send the image to ImageCms.profileToProfile(), specifying inPlace = True
result = ImageCms.profileToProfile(im, INPUT_PROFILE, OUTPUT_PROFILE, \
outputMode = OUTMODE, inPlace = True)
# now that the image is converted, save or display it
if result == None:
# this is the normal result when modifying in-place
outputImage(im, "profileToProfile_inPlace")
else:
# something failed...
print("profileToProfile in-place failed: %s" %result)
print("profileToProfile in-place test completed successfully.")
if TEST_buildTransform == True:
# make a transform using the input and output profile path strings
transform = ImageCms.buildTransform(INPUT_PROFILE, OUTPUT_PROFILE, INMODE, \
OUTMODE)
# now, use the trnsform to convert a couple images
im = Image.open(IMAGE)
# transform im normally
im2 = ImageCms.applyTransform(im, transform)
outputImage(im2, "buildTransform")
# then transform it again using the same transform, this time in-place.
result = ImageCms.applyTransform(im, transform, inPlace = True)
outputImage(im, "buildTransform_inPlace")
print("buildTransform test completed successfully.")
# and, to clean up a bit, delete the transform
# this should call the C destructor for the transform structure.
# Python should also do this automatically when it goes out of scope.
del(transform)
if TEST_buildTransformFromOpenProfiles == True:
# we'll actually test a couple profile open/creation functions here too
# first, get a handle to an input profile, in this case we'll create
# an sRGB profile on the fly:
inputProfile = ImageCms.createProfile("sRGB")
# then, get a handle to the output profile
outputProfile = ImageCms.getOpenProfile(OUTPUT_PROFILE)
# make a transform from these
transform = ImageCms.buildTransformFromOpenProfiles(inputProfile, \
outputProfile, INMODE, OUTMODE)
# now, use the trnsform to convert a couple images
im = Image.open(IMAGE)
# transform im normally
im2 = ImageCms.applyTransform(im, transform)
outputImage(im2, "buildTransformFromOpenProfiles")
# then do it again using the same transform, this time in-place.
result = ImageCms.applyTransform(im, transform, inPlace = True)
outputImage(im, "buildTransformFromOpenProfiles_inPlace")
print("buildTransformFromOpenProfiles test completed successfully.")
# and, to clean up a bit, delete the transform
# this should call the C destructor for the each item.
# Python should also do this automatically when it goes out of scope.
del(inputProfile)
del(outputProfile)
del(transform)
if TEST_buildProofTransform == True:
# make a transform using the input and output and proof profile path
# strings
# images converted with this transform will simulate the appearance
# of the output device while actually being displayed/proofed on the
# proof device. This usually means a monitor, but can also mean
# other proof-printers like dye-sub, etc.
transform = ImageCms.buildProofTransform(INPUT_PROFILE, OUTPUT_PROFILE, \
PROOF_PROFILE, INMODE, OUTMODE)
# now, use the trnsform to convert a couple images
im = Image.open(IMAGE)
# transform im normally
im2 = ImageCms.applyTransform(im, transform)
outputImage(im2, "buildProofTransform")
# then transform it again using the same transform, this time in-place.
result = ImageCms.applyTransform(im, transform, inPlace = True)
outputImage(im, "buildProofTransform_inPlace")
print("buildProofTransform test completed successfully.")
# and, to clean up a bit, delete the transform
# this should call the C destructor for the transform structure.
# Python should also do this automatically when it goes out of scope.
del(transform)
if TEST_getProfileInfo == True:
# get a profile handle
profile = ImageCms.getOpenProfile(INPUT_PROFILE)
# lets print some info about our input profile:
print("Profile name (retrieved from profile string path name): %s" %ImageCms.getProfileName(INPUT_PROFILE))
# or, you could do the same thing using a profile handle as the arg
print("Profile name (retrieved from profile handle): %s" %ImageCms.getProfileName(profile))
# now lets get the embedded "info" tag contents
# once again, you can use a path to a profile, or a profile handle
print("Profile info (retrieved from profile handle): %s" %ImageCms.getProfileInfo(profile))
# and what's the default intent of this profile?
print("The default intent is (this will be an integer): %d" %(ImageCms.getDefaultIntent(profile)))
# Hmmmm... but does this profile support INTENT_ABSOLUTE_COLORIMETRIC?
print("Does it support INTENT_ABSOLUTE_COLORIMETRIC?: (1 is yes, -1 is no): %s" \
%ImageCms.isIntentSupported(profile, ImageCms.INTENT_ABSOLUTE_COLORIMETRIC, \
ImageCms.DIRECTION_INPUT))
print("getProfileInfo test completed successfully.")
if TEST_misc == True:
# test the versions, about, and copyright functions
print("Versions: %s" %str(ImageCms.versions()))
print("About:\n\n%s" %ImageCms.about())
print("Copyright:\n\n%s" %ImageCms.copyright())
print("misc test completed successfully.")

View File

@ -0,0 +1,14 @@
from PIL import Image, ImageFont, ImageDraw
font = "../pil-archive/memory-error-2.ttf"
s = "Test Text"
f = ImageFont.truetype(font, 64, index=0, encoding="unicode")
w, h = f.getsize(s)
i = Image.new("RGB", (500, h), "white")
d = ImageDraw.Draw(i)
# this line causes a MemoryError
d.text((0,0), s, font=f, fill=0)
i.show()

BIN
Tests/fonts/helvO18.pcf Normal file

Binary file not shown.

BIN
Tests/icc/CMY.icm Normal file

Binary file not shown.

BIN
Tests/icc/YCC709.icm Normal file

Binary file not shown.

BIN
Tests/icc/sRGB.icm Normal file

Binary file not shown.

5
Tests/images/broken.png Normal file
View File

@ -0,0 +1,5 @@
‰PNG

/Å}Pô¨<C3B4>áÌ’ï>ƒ’0&ë✉\ÞÖô­ T´LU
¥í÷âxÃÅ/Ð
Z`ײwëÙ;†9 âcˆ

After

Width:  |  Height:  |  Size: 80 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
Tests/images/pil123p.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
Tests/images/pil123rgba.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
Tests/images/pil136.tiff Normal file

Binary file not shown.

BIN
Tests/images/pil168.tif Normal file

Binary file not shown.

BIN
Tests/images/pil184.pcx Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
Tests/images/rgb.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

13
Tests/import_all.py Normal file
View File

@ -0,0 +1,13 @@
import sys
sys.path.insert(0, ".")
import glob, os
import traceback
for file in glob.glob("PIL/*.py"):
module = os.path.basename(file)[:-3]
try:
exec("from PIL import " + module)
except (ImportError, SyntaxError):
print("===", "failed to import", module)
traceback.print_exc()

57
Tests/make_hash.py Normal file
View File

@ -0,0 +1,57 @@
# brute-force search for access descriptor hash table
import random
modes = [
"1",
"L", "LA",
"I", "I;16", "I;16L", "I;16B", "I;32L", "I;32B",
"F",
"P", "PA",
"RGB", "RGBA", "RGBa", "RGBX",
"CMYK",
"YCbCr",
]
def hash(s, i):
# djb2 hash: multiply by 33 and xor character
for c in s:
i = (((i<<5) + i) ^ ord(c)) & 0xffffffff
return i
def check(size, i0):
h = [None] * size
for m in modes:
i = hash(m, i0)
i = i % size
if h[i]:
return 0
h[i] = m
return h
min_start = 0
# 1) find the smallest table size with no collisions
for min_size in range(len(modes), 16384):
if check(min_size, 0):
print(len(modes), "modes fit in", min_size, "slots")
break
# 2) see if we can do better with a different initial value
for i0 in range(65556):
for size in range(1, min_size):
if check(size, i0):
if size < min_size:
print(len(modes), "modes fit in", size, "slots with start", i0)
min_size = size
min_start = i0
print()
# print check(min_size, min_start)
print("#define ACCESS_TABLE_SIZE", min_size)
print("#define ACCESS_TABLE_HASH", min_start)
# for m in modes:
# print m, "=>", hash(m, min_start) % min_size

93
Tests/run.py Normal file
View File

@ -0,0 +1,93 @@
# minimal test runner
import glob, os, sys
try:
root = os.path.dirname(__file__)
except NameError:
root = os.path.dirname(sys.argv[0])
if not os.path.isfile("PIL/Image.py"):
print("***", "please run this script from the PIL development directory as")
print("***", "$ python Tests/run.py")
sys.exit(1)
print("-"*68)
python_options = []
tester_options = []
if "--installed" not in sys.argv:
python_options.append("-S")
os.environ["PYTHONPATH"] = "."
if "--coverage" in sys.argv:
tester_options.append("--coverage")
if "--log" in sys.argv:
tester_options.append("--log")
files = glob.glob(os.path.join(root, "test_*.py"))
files.sort()
success = failure = 0
include = [x for x in sys.argv[1:] if x[:2] != "--"]
skipped = []
python_options = " ".join(python_options)
tester_options = " ".join(tester_options)
for file in files:
test, ext = os.path.splitext(os.path.basename(file))
if include and test not in include:
continue
print("running", test, "...")
# 2>&1 works on unix and on modern windowses. we might care about
# very old Python versions, but not ancient microsoft products :-)
out = os.popen("%s %s -u %s %s 2>&1" % (
sys.executable, python_options, file, tester_options
))
result = out.read().strip()
if result == "ok":
result = None
elif result == "skip":
print("---", "skipped") # FIXME: driver should include a reason
skipped.append(test)
continue
elif not result:
result = "(no output)"
status = out.close()
if status or result:
if status:
print("=== error", status)
if result:
if result[-3:] == "\nok":
# if there's an ok at the end, it's not really ok
result = result[:-3]
print(result)
failure = failure + 1
else:
success = success + 1
print("-"*68)
tempfiles = glob.glob(os.path.join(root, "temp_*"))
if tempfiles:
print("===", "remaining temporary files")
for file in tempfiles:
print(file)
print("-"*68)
def tests(n):
if n == 1:
return "1 test"
else:
return "%d tests" % n
if skipped:
print("---", tests(len(skipped)), "skipped.")
print(skipped)
if failure:
print("***", tests(failure), "of", (success + failure), "failed.")
else:
print(tests(success), "passed.")

28
Tests/show_icc.py Normal file
View File

@ -0,0 +1,28 @@
import sys
sys.path.insert(0, ".")
from PIL import Image
from PIL import ImageCms
try:
filename = sys.argv[1]
except IndexError:
filename = "../pil-archive/cmyk.jpg"
i = Image.open(filename)
print(i.format)
print(i.mode)
print(i.size)
print(i.tile)
p = ImageCms.getMemoryProfile(i.info["icc_profile"])
print(repr(p.product_name))
print(repr(p.product_info))
o = ImageCms.createProfile("sRGB")
t = ImageCms.buildTransformFromOpenProfiles(p, o, i.mode, "RGB")
i = ImageCms.applyTransform(i, t)
i.show()

26
Tests/show_mcidas.py Normal file
View File

@ -0,0 +1,26 @@
import sys
sys.path.insert(0, ".")
from PIL import Image
from PIL import ImageMath
try:
filename = sys.argv[1]
except IndexError:
filename = "../pil-archive/goes12.2005.140.190925.BAND_01.mcidas"
# filename = "../pil-archive/goes12.2005.140.190925.BAND_01.im"
im = Image.open(filename)
print(im.format)
print(im.mode)
print(im.size)
print(im.tile)
lo, hi = im.getextrema()
print("map", lo, hi, "->", end=' ')
im = ImageMath.eval("convert(im*255/hi, 'L')", im=im, hi=hi)
print(im.getextrema())
im.show()

21
Tests/test_000_sanity.py Normal file
View File

@ -0,0 +1,21 @@
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")

23
Tests/test_001_archive.py Normal file
View File

@ -0,0 +1,23 @@
import PIL
import PIL.Image
import glob, os
for file in glob.glob("../pil-archive/*"):
f, e = os.path.splitext(file)
if e in [".txt", ".ttf", ".otf", ".zip"]:
continue
try:
im = PIL.Image.open(file)
im.load()
except IOError as v:
print("-", "failed to open", file, "-", v)
else:
print("+", file, im.mode, im.size, im.format)
if e == ".exif":
try:
info = im._getexif()
except KeyError as v:
print("-", "failed to get exif info from", file, "-", v)
print("ok")

31
Tests/test_contents.py Normal file
View File

@ -0,0 +1,31 @@
from tester import *
import os, glob
contents = {}
for file in open("CONTENTS"):
file = file.strip()
if file:
contents[os.path.normpath(file)] = None
patterns = [
"PIL/*.py",
"*.c",
"libImaging/*.c",
"libImaging/*.h",
]
def test_files():
for pattern in patterns:
for file in glob.glob(pattern):
file = os.path.normpath("Imaging/" + file)
assert_true(file in contents, "%r not in CONTENTS" % file)
def test_contents():
for file in contents:
root, file = file.split(os.sep, 1)
if file == "MANIFEST":
continue # generated by distribution scripts
assert_true(os.path.isfile(file), "%r not found" % file)

27
Tests/test_file_bmp.py Normal file
View File

@ -0,0 +1,27 @@
from tester import *
from PIL import Image
def test_sanity():
file = tempfile("temp.bmp")
lena().save(file)
im = Image.open(file)
im.load()
assert_equal(im.mode, "RGB")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "BMP")
lena("1").save(file)
im = Image.open(file)
lena("L").save(file)
im = Image.open(file)
lena("P").save(file)
im = Image.open(file)
lena("RGB").save(file)
im = Image.open(file)

28
Tests/test_file_gif.py Normal file
View File

@ -0,0 +1,28 @@
from tester import *
from PIL import Image
codecs = dir(Image.core)
if "gif_encoder" not in codecs or "gif_decoder" not in codecs:
skip("gif support not available") # can this happen?
# sample gif stream
file = "Images/lena.gif"
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, "GIF")
def test_optimize():
def test(optimize):
im = Image.new("L", (1, 1), 0)
file = BytesIO()
im.save(file, "GIF", optimize=optimize)
return len(file.getvalue())
assert_equal(test(0), 800)
assert_equal(test(1), 32)

179
Tests/test_file_jpeg.py Normal file
View File

@ -0,0 +1,179 @@
from tester import *
from PIL import Image
from PIL import ImageFile
codecs = dir(Image.core)
if "jpeg_encoder" not in codecs or "jpeg_decoder" not in codecs:
skip("jpeg support not available")
# sample jpeg stream
file = "Images/lena.jpg"
data = open(file, "rb").read()
def roundtrip(im, **options):
out = BytesIO()
im.save(out, "JPEG", **options)
bytes = out.tell()
out.seek(0)
im = Image.open(out)
im.bytes = bytes # for testing only
return im
# --------------------------------------------------------------------
def test_sanity():
# internal version number
assert_match(Image.core.jpeglib_version, "\d+\.\d+$")
im = Image.open(file)
im.load()
assert_equal(im.mode, "RGB")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "JPEG")
# --------------------------------------------------------------------
def test_app():
# Test APP/COM reader (@PIL135)
im = Image.open(file)
assert_equal(im.applist[0],
("APP0", b"JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"))
assert_equal(im.applist[1], ("COM", b"Python Imaging Library"))
assert_equal(len(im.applist), 2)
def test_cmyk():
# Test CMYK handling. Thanks to Tim and Charlie for test data,
# Michael for getting me to look one more time.
file = "Tests/images/pil_sample_cmyk.jpg"
im = Image.open(file)
# the source image has red pixels in the upper left corner.
c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))]
assert_true(c == 0.0 and m > 0.8 and y > 0.8 and k == 0.0)
# the opposite corner is black
c, m, y, k = [x / 255.0 for x in im.getpixel((im.size[0]-1, im.size[1]-1))]
assert_true(k > 0.9)
# roundtrip, and check again
im = roundtrip(im)
c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))]
assert_true(c == 0.0 and m > 0.8 and y > 0.8 and k == 0.0)
c, m, y, k = [x / 255.0 for x in im.getpixel((im.size[0]-1, im.size[1]-1))]
assert_true(k > 0.9)
def test_dpi():
def test(xdpi, ydpi=None):
im = Image.open(file)
im = roundtrip(im, dpi=(xdpi, ydpi or xdpi))
return im.info.get("dpi")
assert_equal(test(72), (72, 72))
assert_equal(test(300), (300, 300))
assert_equal(test(100, 200), (100, 200))
assert_equal(test(0), None) # square pixels
def test_icc():
# Test ICC support
im1 = Image.open("Tests/images/rgb.jpg")
icc_profile = im1.info["icc_profile"]
assert_equal(len(icc_profile), 3144)
# Roundtrip via physical file.
file = tempfile("temp.jpg")
im1.save(file, icc_profile=icc_profile)
im2 = Image.open(file)
assert_equal(im2.info.get("icc_profile"), icc_profile)
# Roundtrip via memory buffer.
im1 = roundtrip(lena())
im2 = roundtrip(lena(), icc_profile=icc_profile)
assert_image_equal(im1, im2)
assert_false(im1.info.get("icc_profile"))
assert_true(im2.info.get("icc_profile"))
def test_icc_big():
# Make sure that the "extra" support handles large blocks
def test(n):
# The ICC APP marker can store 65519 bytes per marker, so
# using a 4-byte test code should allow us to detect out of
# order issues.
icc_profile = (b"Test"*int(n/4+1))[:n]
assert len(icc_profile) == n # sanity
im1 = roundtrip(lena(), icc_profile=icc_profile)
assert_equal(im1.info.get("icc_profile"), icc_profile or None)
test(0); test(1)
test(3); test(4); test(5)
test(65533-14) # full JPEG marker block
test(65533-14+1) # full block plus one byte
test(ImageFile.MAXBLOCK) # full buffer block
test(ImageFile.MAXBLOCK+1) # full buffer block plus one byte
test(ImageFile.MAXBLOCK*4+3) # large block
def test_optimize():
im1 = roundtrip(lena())
im2 = roundtrip(lena(), optimize=1)
assert_image_equal(im1, im2)
assert_true(im1.bytes >= im2.bytes)
def test_progressive():
im1 = roundtrip(lena())
im2 = roundtrip(lena(), progressive=1)
im3 = roundtrip(lena(), progression=1) # compatibility
assert_image_equal(im1, im2)
assert_image_equal(im1, im3)
assert_false(im1.info.get("progressive"))
assert_false(im1.info.get("progression"))
assert_true(im2.info.get("progressive"))
assert_true(im2.info.get("progression"))
assert_true(im3.info.get("progressive"))
assert_true(im3.info.get("progression"))
def test_quality():
im1 = roundtrip(lena())
im2 = roundtrip(lena(), quality=50)
assert_image(im1, im2.mode, im2.size)
assert_true(im1.bytes >= im2.bytes)
def test_smooth():
im1 = roundtrip(lena())
im2 = roundtrip(lena(), smooth=100)
assert_image(im1, im2.mode, im2.size)
def test_subsampling():
def getsampling(im):
layer = im.layer
return layer[0][1:3] + layer[1][1:3] + layer[2][1:3]
# experimental API
im = roundtrip(lena(), subsampling=-1) # default
assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling=0) # 4:4:4
assert_equal(getsampling(im), (1, 1, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling=1) # 4:2:2
assert_equal(getsampling(im), (2, 1, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling=2) # 4:1:1
assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling=3) # default (undefined)
assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling="4:4:4")
assert_equal(getsampling(im), (1, 1, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling="4:2:2")
assert_equal(getsampling(im), (2, 1, 1, 1, 1, 1))
im = roundtrip(lena(), subsampling="4:1:1")
assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
assert_exception(TypeError, lambda: roundtrip(lena(), subsampling="1:1:1"))
def test_truncated_jpeg():
def test(junk):
if junk:
# replace "junk" bytes at the end with junk
file = BytesIO(data[:-junk] + bytes(junk*[0]))
else:
file = BytesIO(data)
im = Image.open(file)
im.load()
assert_no_exception(lambda: test(0))
assert_exception(IOError, lambda: test(1))
assert_no_exception(lambda: test(2))
assert_no_exception(lambda: test(4))
assert_no_exception(lambda: test(8))
assert_exception(IOError, lambda: test(10))

15
Tests/test_file_msp.py Normal file
View File

@ -0,0 +1,15 @@
from tester import *
from PIL import Image
def test_sanity():
file = tempfile("temp.msp")
lena("1").save(file)
im = Image.open(file)
im.load()
assert_equal(im.mode, "1")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "MSP")

39
Tests/test_file_pcx.py Normal file
View File

@ -0,0 +1,39 @@
from tester import *
from PIL import Image
def test_sanity():
file = tempfile("temp.pcx")
lena("1").save(file)
im = Image.open(file)
im.load()
assert_equal(im.mode, "1")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "PCX")
lena("1").save(file)
im = Image.open(file)
lena("L").save(file)
im = Image.open(file)
lena("P").save(file)
im = Image.open(file)
lena("RGB").save(file)
im = Image.open(file)
def test_pil184():
# Check reading of files where xmin/xmax is not zero.
file = "Tests/images/pil184.pcx"
im = Image.open(file)
assert_equal(im.size, (447, 144))
assert_equal(im.tile[0][1], (0, 0, 447, 144))
# Make sure all pixels are either 0 or 255.
assert_equal(im.histogram()[0] + im.histogram()[255], 447*144)

170
Tests/test_file_png.py Normal file
View File

@ -0,0 +1,170 @@
from tester import *
from PIL import Image
from PIL import PngImagePlugin
codecs = dir(Image.core)
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
skip("zip/deflate support not available")
# sample png stream
file = "Images/lena.png"
data = open(file, "rb").read()
# stuff to create inline PNG images
MAGIC = PngImagePlugin._MAGIC
def chunk(cid, *data):
file = BytesIO()
PngImagePlugin.putchunk(*(file, cid) + data)
return file.getvalue()
o32 = PngImagePlugin.o32
IHDR = chunk(b"IHDR", o32(1), o32(1), bytes((8,2)), bytes((0,0,0)))
IDAT = chunk(b"IDAT")
IEND = chunk(b"IEND")
HEAD = MAGIC + IHDR
TAIL = IDAT + IEND
def load(data):
return Image.open(BytesIO(data))
def roundtrip(im, **options):
out = BytesIO()
im.save(out, "PNG", **options)
out.seek(0)
return Image.open(out)
# --------------------------------------------------------------------
def test_sanity():
# internal version number
assert_match(Image.core.zlib_version, "\d+\.\d+\.\d+(\.\d+)?$")
file = tempfile("temp.png")
lena("RGB").save(file)
im = Image.open(file)
im.load()
assert_equal(im.mode, "RGB")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "PNG")
lena("1").save(file)
im = Image.open(file)
lena("L").save(file)
im = Image.open(file)
lena("P").save(file)
im = Image.open(file)
lena("RGB").save(file)
im = Image.open(file)
lena("I").save(file)
im = Image.open(file)
# --------------------------------------------------------------------
def test_broken():
# Check reading of totally broken files. In this case, the test
# file was checked into Subversion as a text file.
file = "Tests/images/broken.png"
assert_exception(IOError, lambda: Image.open(file))
def test_bad_text():
# Make sure PIL can read malformed tEXt chunks (@PIL152)
im = load(HEAD + chunk(b'tEXt') + TAIL)
assert_equal(im.info, {})
im = load(HEAD + chunk(b'tEXt', b'spam') + TAIL)
assert_equal(im.info, {b'spam': b''})
im = load(HEAD + chunk(b'tEXt', b'spam\0') + TAIL)
assert_equal(im.info, {b'spam': b''})
im = load(HEAD + chunk(b'tEXt', b'spam\0egg') + TAIL)
assert_equal(im.info, {b'spam': b'egg'})
im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
assert_equal(im.info, {b'spam': b'egg\x00'})
def test_interlace():
file = "Tests/images/pil123p.png"
im = Image.open(file)
assert_image(im, "P", (162, 150))
assert_true(im.info.get("interlace"))
assert_no_exception(lambda: im.load())
file = "Tests/images/pil123rgba.png"
im = Image.open(file)
assert_image(im, "RGBA", (162, 150))
assert_true(im.info.get("interlace"))
assert_no_exception(lambda: im.load())
def test_load_verify():
# Check open/load/verify exception (@PIL150)
im = Image.open("Images/lena.png")
assert_no_exception(lambda: im.verify())
im = Image.open("Images/lena.png")
im.load()
assert_exception(RuntimeError, lambda: im.verify())
def test_roundtrip_dpi():
# Check dpi roundtripping
im = Image.open(file)
im = roundtrip(im, dpi=(100, 100))
assert_equal(im.info["dpi"], (100, 100))
def test_roundtrip_text():
# Check text roundtripping
im = Image.open(file)
info = PngImagePlugin.PngInfo()
info.add_text(b"TXT", b"VALUE")
info.add_text(b"ZIP", b"VALUE", 1)
im = roundtrip(im, pnginfo=info)
assert_equal(im.info, {b'TXT': b'VALUE', b'ZIP': b'VALUE'})
assert_equal(im.text, {b'TXT': b'VALUE', b'ZIP': b'VALUE'})
def test_scary():
# Check reading of evil PNG file. For information, see:
# http://scary.beasts.org/security/CESA-2004-001.txt
import base64
file = "Tests/images/pngtest_bad.png.base64"
data = base64.decodebytes(open(file, 'rb').read())
file = BytesIO(data)
assert_exception(IOError, lambda: Image.open(file))
def test_trns_rgb():
# Check writing and reading of tRNS chunks for RGB images.
# Independent file sample provided by Sebastian Spaeth.
file = "Tests/images/caption_6_33_22.png"
im = Image.open(file)
assert_equal(im.info["transparency"], (248, 248, 248))
im = roundtrip(im, transparency=(0, 1, 2))
assert_equal(im.info["transparency"], (0, 1, 2))

14
Tests/test_file_ppm.py Normal file
View File

@ -0,0 +1,14 @@
from tester import *
from PIL import Image
# sample ppm stream
file = "Images/lena.ppm"
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, "PPM")

57
Tests/test_file_tiff.py Normal file
View File

@ -0,0 +1,57 @@
from tester import *
from PIL import Image
def test_sanity():
file = tempfile("temp.tif")
lena("RGB").save(file)
im = Image.open(file)
im.load()
assert_equal(im.mode, "RGB")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "TIFF")
lena("1").save(file)
im = Image.open(file)
lena("L").save(file)
im = Image.open(file)
lena("P").save(file)
im = Image.open(file)
lena("RGB").save(file)
im = Image.open(file)
lena("I").save(file)
im = Image.open(file)
def test_mac_tiff():
# Read RGBa images from Mac OS X [@PIL136]
file = "Tests/images/pil136.tiff"
im = Image.open(file)
assert_equal(im.mode, "RGBA")
assert_equal(im.size, (55, 43))
assert_equal(im.tile, [('raw', (0, 0, 55, 43), 8, ('RGBa', 0, 1))])
assert_no_exception(lambda: im.load())
def test_gimp_tiff():
# Read TIFF JPEG images from GIMP [@PIL168]
file = "Tests/images/pil168.tif"
im = Image.open(file)
assert_equal(im.mode, "RGB")
assert_equal(im.size, (256, 256))
assert_equal(im.tile, [
('jpeg', (0, 0, 256, 64), 8, ('RGB', '')),
('jpeg', (0, 64, 256, 128), 1215, ('RGB', '')),
('jpeg', (0, 128, 256, 192), 2550, ('RGB', '')),
('jpeg', (0, 192, 256, 256), 3890, ('RGB', '')),
])
assert_no_exception(lambda: im.load())

34
Tests/test_file_xbm.py Normal file
View File

@ -0,0 +1,34 @@
from tester import *
from PIL import Image
PIL151 = b"""
#define basic_width 32
#define basic_height 32
static char basic_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x80, 0xff, 0xff, 0x01, 0x40, 0x00, 0x00, 0x02,
0x20, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x08,
0x10, 0x00, 0x00, 0x08,
0x10, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x08,
0x10, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x08,
0x20, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x02,
0x80, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
"""
def test_pil151():
im = Image.open(BytesIO(PIL151))
assert_no_exception(lambda: im.load())
assert_equal(im.mode, '1')
assert_equal(im.size, (32, 32))

13
Tests/test_font_bdf.py Normal file
View File

@ -0,0 +1,13 @@
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)

26
Tests/test_font_pcf.py Normal file
View File

@ -0,0 +1,26 @@
from tester import *
from PIL import Image, FontFile, PcfFontFile
from PIL import ImageFont, ImageDraw
fontname = "Tests/fonts/helvO18.pcf"
tempname = tempfile("temp.pil", "temp.pbm")
message = "hello, world"
def test_sanity():
file = open(fontname, "rb")
font = PcfFontFile.PcfFontFile(file)
assert_true(isinstance(font, FontFile.FontFile))
assert_equal(len([_f for _f in font.glyph if _f]), 192)
font.save(tempname)
def test_draw():
font = ImageFont.load(tempname)
image = Image.new("L", font.getsize(message), "white")
draw = ImageDraw.Draw(image)
draw.text((0, 0), message, font=font)
# assert_signature(image, "7216c60f988dea43a46bb68321e3c1b03ec62aee")

39
Tests/test_image.py Normal file
View File

@ -0,0 +1,39 @@
from tester import *
from PIL import Image
def test_sanity():
im = Image.new("L", (100, 100))
assert_equal(repr(im)[:45], "<PIL.Image.Image image mode=L size=100x100 at")
assert_equal(im.mode, "L")
assert_equal(im.size, (100, 100))
im = Image.new("RGB", (100, 100))
assert_equal(repr(im)[:45], "<PIL.Image.Image image mode=RGB size=100x100 ")
assert_equal(im.mode, "RGB")
assert_equal(im.size, (100, 100))
im1 = Image.new("L", (100, 100), None)
im2 = Image.new("L", (100, 100), 0)
im3 = Image.new("L", (100, 100), "black")
assert_equal(im2.getcolors(), [(10000, 0)])
assert_equal(im3.getcolors(), [(10000, 0)])
assert_exception(ValueError, lambda: Image.new("X", (100, 100)))
# assert_exception(MemoryError, lambda: Image.new("L", (1000000, 1000000)))
def test_internals():
im = Image.new("L", (100, 100))
im.readonly = 1
im._copy()
assert_false(im.readonly)
im.readonly = 1
im.paste(0, (0, 0, 100, 100))
assert_false(im.readonly)
file = tempfile("temp.ppm")
im._dump(file)

33
Tests/test_image_array.py Normal file
View File

@ -0,0 +1,33 @@
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), '<i4', 51200)) # FIXME: wrong?
assert_equal(test("F"), ((100, 128), '<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))

View File

@ -0,0 +1,26 @@
from tester import *
from PIL import Image
def test_sanity():
def convert(im, mode):
out = im.convert(mode)
assert_equal(out.mode, mode)
assert_equal(out.size, im.size)
modes = "1", "L", "I", "F", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"
for mode in modes:
im = lena(mode)
for mode in modes:
yield_test(convert, im, mode)
def test_default():
im = lena("P")
assert_image(im, "P", im.size)
im = im.convert()
assert_image(im, "RGB", im.size)
im = im.convert()
assert_image(im, "RGB", im.size)

12
Tests/test_image_copy.py Normal file
View File

@ -0,0 +1,12 @@
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)

52
Tests/test_image_crop.py Normal file
View File

@ -0,0 +1,52 @@
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])

25
Tests/test_image_draft.py Normal file
View File

@ -0,0 +1,25 @@
from tester import *
from PIL import Image
filename = "Images/lena.jpg"
data = tostring(Image.open(filename).resize((512, 512)), "JPEG")
def draft(mode, size):
im = fromstring(data)
im.draft(mode, size)
return im
def test_size():
assert_equal(draft("RGB", (512, 512)).size, (512, 512))
assert_equal(draft("RGB", (256, 256)).size, (256, 256))
assert_equal(draft("RGB", (128, 128)).size, (128, 128))
assert_equal(draft("RGB", (64, 64)).size, (64, 64))
assert_equal(draft("RGB", (32, 32)).size, (64, 64))
def test_mode():
assert_equal(draft("1", (512, 512)).mode, "RGB")
assert_equal(draft("L", (512, 512)).mode, "L")
assert_equal(draft("RGB", (512, 512)).mode, "RGB")
assert_equal(draft("YCbCr", (512, 512)).mode, "YCbCr")

View File

@ -0,0 +1,82 @@
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))

View File

@ -0,0 +1,11 @@
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)

View File

@ -0,0 +1,15 @@
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"))

View File

@ -0,0 +1,36 @@
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

@ -0,0 +1,64 @@
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)

View File

@ -0,0 +1,28 @@
from tester import *
from PIL import Image
def test_sanity():
data = lena().getdata()
assert_no_exception(lambda: len(data))
assert_no_exception(lambda: list(data))
assert_equal(data[0], (223, 162, 133))
def test_roundtrip():
def getdata(mode):
im = lena(mode).resize((32, 30))
data = im.getdata()
return data[0], len(data), len(list(data))
assert_equal(getdata("1"), (255, 960, 960))
assert_equal(getdata("L"), (176, 960, 960))
assert_equal(getdata("I"), (176, 960, 960))
assert_equal(getdata("F"), (176.0, 960, 960))
assert_equal(getdata("RGB"), ((223, 162, 133), 960, 960))
assert_equal(getdata("RGBA"), ((223, 162, 133, 255), 960, 960))
assert_equal(getdata("CMYK"), ((32, 93, 122, 0), 960, 960))
assert_equal(getdata("YCbCr"), ((176, 103, 160), 960, 960))

View File

@ -0,0 +1,17 @@
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)))

13
Tests/test_image_getim.py Normal file
View File

@ -0,0 +1,13 @@
from tester import *
from PIL import Image
def test_sanity():
im = lena()
type_repr = repr(type(im.getim()))
assert_true("PyCapsule" in type_repr)
assert_true(isinstance(im.im.id, int))

View File

@ -0,0 +1,19 @@
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)

View File

@ -0,0 +1,57 @@
from tester import *
from PIL import Image
def color(mode):
bands = Image.getmodebands(mode)
if bands == 1:
return 1
else:
return tuple(range(1, bands+1))
def test_pixel():
def pixel(mode):
c = color(mode)
im = Image.new(mode, (1, 1), None)
im.putpixel((0, 0), c)
return im.getpixel((0, 0))
assert_equal(pixel("1"), 1)
assert_equal(pixel("L"), 1)
assert_equal(pixel("LA"), (1, 2))
assert_equal(pixel("I"), 1)
assert_equal(pixel("I;16"), 1)
assert_equal(pixel("I;16B"), 1)
assert_equal(pixel("F"), 1.0)
assert_equal(pixel("P"), 1)
assert_equal(pixel("PA"), (1, 2))
assert_equal(pixel("RGB"), (1, 2, 3))
assert_equal(pixel("RGBA"), (1, 2, 3, 4))
assert_equal(pixel("RGBX"), (1, 2, 3, 4))
assert_equal(pixel("CMYK"), (1, 2, 3, 4))
assert_equal(pixel("YCbCr"), (1, 2, 3))
def test_image():
def pixel(mode):
im = Image.new(mode, (1, 1), color(mode))
return im.getpixel((0, 0))
assert_equal(pixel("1"), 1)
assert_equal(pixel("L"), 1)
assert_equal(pixel("LA"), (1, 2))
assert_equal(pixel("I"), 1)
assert_equal(pixel("I;16"), 1)
assert_equal(pixel("I;16B"), 1)
assert_equal(pixel("F"), 1.0)
assert_equal(pixel("P"), 1)
assert_equal(pixel("PA"), (1, 2))
assert_equal(pixel("RGB"), (1, 2, 3))
assert_equal(pixel("RGBA"), (1, 2, 3, 4))
assert_equal(pixel("RGBX"), (1, 2, 3, 4))
assert_equal(pixel("CMYK"), (1, 2, 3, 4))
assert_equal(pixel("YCbCr"), (1, 2, 3))

View File

@ -0,0 +1,30 @@
from tester import *
from PIL import Image
def test_sanity():
im = lena()
projection = im.getprojection()
assert_equal(len(projection), 2)
assert_equal(len(projection[0]), im.size[0])
assert_equal(len(projection[1]), im.size[1])
# 8-bit image
im = Image.new("L", (10, 10))
assert_equal(im.getprojection()[0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
assert_equal(im.getprojection()[1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
im.paste(255, (2, 4, 8, 6))
assert_equal(im.getprojection()[0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0])
assert_equal(im.getprojection()[1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0])
# 32-bit image
im = Image.new("RGB", (10, 10))
assert_equal(im.getprojection()[0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
assert_equal(im.getprojection()[1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
im.paste(255, (2, 4, 8, 6))
assert_equal(im.getprojection()[0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0])
assert_equal(im.getprojection()[1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0])

View File

@ -0,0 +1,19 @@
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))

11
Tests/test_image_load.py Normal file
View File

@ -0,0 +1,11 @@
from tester import *
from PIL import Image
def test_sanity():
im = lena()
pix = im.load()
assert_equal(pix[0, 0], (223, 162, 133))

27
Tests/test_image_mode.py Normal file
View File

@ -0,0 +1,27 @@
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"))

View File

@ -0,0 +1,16 @@
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)))

View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

19
Tests/test_image_point.py Normal file
View File

@ -0,0 +1,19 @@
from tester import *
from PIL import Image
def test_sanity():
im = lena()
assert_exception(ValueError, lambda: im.point(list(range(256))))
assert_no_exception(lambda: im.point(list(range(256))*3))
assert_no_exception(lambda: im.point(lambda x: x))
im = im.convert("I")
assert_exception(ValueError, lambda: im.point(list(range(256))))
assert_no_exception(lambda: im.point(lambda x: x*1))
assert_no_exception(lambda: im.point(lambda x: x+1))
assert_no_exception(lambda: im.point(lambda x: x*1+1))
assert_exception(TypeError, lambda: im.point(lambda x: x-1))
assert_exception(TypeError, lambda: im.point(lambda x: x/1))

View File

@ -0,0 +1,43 @@
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))

View File

@ -0,0 +1,40 @@
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))

View File

@ -0,0 +1,28 @@
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()))

View File

@ -0,0 +1,43 @@
from tester import *
from PIL import Image
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

View File

@ -0,0 +1,15 @@
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)

View File

@ -0,0 +1,12 @@
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))

View File

@ -0,0 +1,15 @@
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)

5
Tests/test_image_save.py Normal file
View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

5
Tests/test_image_seek.py Normal file
View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

5
Tests/test_image_show.py Normal file
View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

42
Tests/test_image_split.py Normal file
View File

@ -0,0 +1,42 @@
from tester import *
from PIL import Image
def test_split():
def split(mode):
layers = lena(mode).split()
return [(i.mode, i.size[0], i.size[1]) for i in layers]
assert_equal(split("1"), [('1', 128, 128)])
assert_equal(split("L"), [('L', 128, 128)])
assert_equal(split("I"), [('I', 128, 128)])
assert_equal(split("F"), [('F', 128, 128)])
assert_equal(split("P"), [('P', 128, 128)])
assert_equal(split("RGB"), [('L', 128, 128), ('L', 128, 128), ('L', 128, 128)])
assert_equal(split("RGBA"), [('L', 128, 128), ('L', 128, 128), ('L', 128, 128), ('L', 128, 128)])
assert_equal(split("CMYK"), [('L', 128, 128), ('L', 128, 128), ('L', 128, 128), ('L', 128, 128)])
assert_equal(split("YCbCr"), [('L', 128, 128), ('L', 128, 128), ('L', 128, 128)])
def test_split_merge():
def split_merge(mode):
return Image.merge(mode, lena(mode).split())
assert_image_equal(lena("1"), split_merge("1"))
assert_image_equal(lena("L"), split_merge("L"))
assert_image_equal(lena("I"), split_merge("I"))
assert_image_equal(lena("F"), split_merge("F"))
assert_image_equal(lena("P"), split_merge("P"))
assert_image_equal(lena("RGB"), split_merge("RGB"))
assert_image_equal(lena("RGBA"), split_merge("RGBA"))
assert_image_equal(lena("CMYK"), split_merge("CMYK"))
assert_image_equal(lena("YCbCr"), split_merge("YCbCr"))
def test_split_open():
file = tempfile("temp.png")
def split_open(mode):
lena(mode).save(file)
im = Image.open(file)
return len(im.split())
assert_equal(split_open("1"), 1)
assert_equal(split_open("L"), 1)
assert_equal(split_open("P"), 1)
assert_equal(split_open("RGB"), 3)
assert_equal(split_open("RGBA"), 4)

5
Tests/test_image_tell.py Normal file
View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

View File

@ -0,0 +1,36 @@
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))

View File

@ -0,0 +1,15 @@
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))

View File

@ -0,0 +1,8 @@
from tester import *
from PIL import Image
def test_sanity():
data = lena().tobytes()
assert_true(isinstance(data, bytes))

View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

View File

@ -0,0 +1,34 @@
from tester import *
from PIL import Image
FLIP_LEFT_RIGHT = Image.FLIP_LEFT_RIGHT
FLIP_TOP_BOTTOM = Image.FLIP_TOP_BOTTOM
ROTATE_90 = Image.ROTATE_90
ROTATE_180 = Image.ROTATE_180
ROTATE_270 = Image.ROTATE_270
def test_sanity():
im = lena()
assert_no_exception(lambda: im.transpose(FLIP_LEFT_RIGHT))
assert_no_exception(lambda: im.transpose(FLIP_TOP_BOTTOM))
assert_no_exception(lambda: im.transpose(ROTATE_90))
assert_no_exception(lambda: im.transpose(ROTATE_180))
assert_no_exception(lambda: im.transpose(ROTATE_270))
def test_roundtrip():
im = lena()
def transpose(first, second):
return im.transpose(first).transpose(second)
assert_image_equal(im, transpose(FLIP_LEFT_RIGHT, FLIP_LEFT_RIGHT))
assert_image_equal(im, transpose(FLIP_TOP_BOTTOM, FLIP_TOP_BOTTOM))
assert_image_equal(im, transpose(ROTATE_90, ROTATE_270))
assert_image_equal(im, transpose(ROTATE_180, ROTATE_180))

View File

@ -0,0 +1,5 @@
from tester import *
from PIL import Image
success()

56
Tests/test_imagechops.py Normal file
View File

@ -0,0 +1,56 @@
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))

82
Tests/test_imagecms.py Normal file
View File

@ -0,0 +1,82 @@
from tester import *
from PIL import Image
try:
from PIL import ImageCms
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], '0.1.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))
t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB")
i = ImageCms.applyTransform(lena(), t)
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))
# get profile information for file
assert_equal(ImageCms.getProfileName(SRGB).strip(),
'IEC 61966-2.1 Default RGB colour space - sRGB')
assert_equal(ImageCms.getProfileInfo(SRGB).splitlines(),
['sRGB IEC61966-2.1', '',
'Copyright (c) 1998 Hewlett-Packard Company', '',
'WhitePoint : D65 (daylight)', '',
'Tests/icc/sRGB.icm'])
assert_equal(ImageCms.getDefaultIntent(SRGB), 0)
assert_equal(ImageCms.isIntentSupported(
SRGB, ImageCms.INTENT_ABSOLUTE_COLORIMETRIC,
ImageCms.DIRECTION_INPUT), 1)
# 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)
# 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')
# 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))
# test PointTransform convenience API
im = lena().point(t)
# try fetching the profile for the current display device
assert_no_exception(lambda: ImageCms.get_display_profile())

31
Tests/test_imagecolor.py Normal file
View File

@ -0,0 +1,31 @@
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(100%,0%,0%)"))
assert_equal((255, 0, 0), ImageColor.getrgb("hsl(0, 100%, 50%)"))
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, ImageColor.getcolor("black", "L"))
assert_equal(255, ImageColor.getcolor("white", "L"))
assert_equal(0, ImageColor.getcolor("black", "1"))
assert_equal(255, ImageColor.getcolor("white", "1"))

28
Tests/test_imagedraw.py Normal file
View File

@ -0,0 +1,28 @@
from tester import *
from PIL import Image
from PIL import ImageDraw
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))

View File

@ -0,0 +1,19 @@
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))

61
Tests/test_imagefile.py Normal file
View File

@ -0,0 +1,61 @@
from tester import *
from PIL import Image
from PIL import ImageFile
# save original block sizes
MAXBLOCK = ImageFile.MAXBLOCK
SAFEBLOCK = ImageFile.SAFEBLOCK
def test_parser():
def roundtrip(format):
im = lena("L").resize((1000, 1000))
if format in ("MSP", "XBM"):
im = im.convert("1")
file = BytesIO()
im.save(file, format)
data = file.getvalue()
parser = ImageFile.Parser()
parser.feed(data)
imOut = parser.close()
return im, imOut
assert_image_equal(*roundtrip("BMP"))
assert_image_equal(*roundtrip("GIF"))
assert_image_equal(*roundtrip("IM"))
assert_image_equal(*roundtrip("MSP"))
try:
# force multiple blocks in PNG driver
ImageFile.MAXBLOCK = 8192
assert_image_equal(*roundtrip("PNG"))
finally:
ImageFile.MAXBLOCK = MAXBLOCK
assert_image_equal(*roundtrip("PPM"))
assert_image_equal(*roundtrip("TIFF"))
assert_image_equal(*roundtrip("XBM"))
im1, im2 = roundtrip("JPEG") # lossy compression
assert_image(im1, im2.mode, im2.size)
assert_exception(IOError, lambda: roundtrip("PCX"))
assert_exception(IOError, lambda: roundtrip("PDF"))
def test_safeblock():
im1 = lena()
try:
ImageFile.SAFEBLOCK = 1
im2 = fromstring(tostring(im1, "PNG"))
finally:
ImageFile.SAFEBLOCK = SAFEBLOCK
assert_image_equal(im1, im2)

24
Tests/test_imagefileio.py Normal file
View File

@ -0,0 +1,24 @@
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)

31
Tests/test_imagefilter.py Normal file
View File

@ -0,0 +1,31 @@
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)

12
Tests/test_imagefont.py Normal file
View File

@ -0,0 +1,12 @@
from tester import *
from PIL import Image
try:
from PIL import ImageFont
ImageFont.core.getfont # check if freetype is available
except ImportError:
skip()
def test_sanity():
assert_match(ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$")

9
Tests/test_imagegl.py Normal file
View File

@ -0,0 +1,9 @@
from tester import *
from PIL import Image
try:
from PIL import ImageGL
except ImportError as v:
skip(v)
success()

13
Tests/test_imagegrab.py Normal file
View File

@ -0,0 +1,13 @@
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)

62
Tests/test_imagemath.py Normal file
View File

@ -0,0 +1,62 @@
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")

23
Tests/test_imagemode.py Normal file
View File

@ -0,0 +1,23 @@
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")

70
Tests/test_imageops.py Normal file
View File

@ -0,0 +1,70 @@
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_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()

View File

@ -0,0 +1,55 @@
from tester import *
from PIL import Image
from PIL import ImageOps
from PIL import ImageFilter
im = Image.open("Images/lena.ppm")
def test_ops_api():
i = ImageOps.gaussian_blur(im, 2.0)
assert_equal(i.mode, "RGB")
assert_equal(i.size, (128, 128))
# i.save("blur.bmp")
i = ImageOps.usm(im, 2.0, 125, 8)
assert_equal(i.mode, "RGB")
assert_equal(i.size, (128, 128))
# i.save("usm.bmp")
def test_filter_api():
filter = ImageFilter.GaussianBlur(2.0)
i = im.filter(filter)
assert_equal(i.mode, "RGB")
assert_equal(i.size, (128, 128))
filter = ImageFilter.UnsharpMask(2.0, 125, 8)
i = im.filter(filter)
assert_equal(i.mode, "RGB")
assert_equal(i.size, (128, 128))
def test_usm():
usm = ImageOps.unsharp_mask
assert_exception(ValueError, lambda: usm(im.convert("1")))
assert_no_exception(lambda: usm(im.convert("L")))
assert_exception(ValueError, lambda: usm(im.convert("I")))
assert_exception(ValueError, lambda: usm(im.convert("F")))
assert_no_exception(lambda: usm(im.convert("RGB")))
assert_no_exception(lambda: usm(im.convert("RGBA")))
assert_no_exception(lambda: usm(im.convert("CMYK")))
assert_exception(ValueError, lambda: usm(im.convert("YCbCr")))
def test_blur():
blur = ImageOps.gaussian_blur
assert_exception(ValueError, lambda: blur(im.convert("1")))
assert_no_exception(lambda: blur(im.convert("L")))
assert_exception(ValueError, lambda: blur(im.convert("I")))
assert_exception(ValueError, lambda: blur(im.convert("F")))
assert_no_exception(lambda: blur(im.convert("RGB")))
assert_no_exception(lambda: blur(im.convert("RGBA")))
assert_no_exception(lambda: blur(im.convert("CMYK")))
assert_exception(ValueError, lambda: blur(im.convert("YCbCr")))

View File

@ -0,0 +1,44 @@
from tester import *
from PIL import Image
from PIL import ImagePalette
ImagePalette = ImagePalette.ImagePalette
def test_sanity():
assert_no_exception(lambda: ImagePalette("RGB", list(range(256))*3))
assert_exception(ValueError, lambda: ImagePalette("RGB", list(range(256))*2))
def test_getcolor():
palette = ImagePalette()
map = {}
for i in range(256):
map[palette.getcolor((i, i, i))] = i
assert_equal(len(map), 256)
assert_exception(ValueError, lambda: palette.getcolor((1, 2, 3)))
def test_file():
palette = ImagePalette()
file = tempfile("temp.lut")
palette.save(file)
from PIL.ImagePalette import load, raw
p = load(file)
# load returns raw palette information
assert_equal(len(p[0]), 768)
assert_equal(p[1], "RGB")
p = raw(p[1], p[0])
assert_true(isinstance(p, ImagePalette))

49
Tests/test_imagepath.py Normal file
View File

@ -0,0 +1,49 @@
from tester import *
from PIL import Image
from PIL import ImagePath
import array
def test_path():
p = ImagePath.Path(list(range(10)))
# sequence interface
assert_equal(len(p), 5)
assert_equal(p[0], (0.0, 1.0))
assert_equal(p[-1], (8.0, 9.0))
assert_equal(list(p[:1]), [(0.0, 1.0)])
assert_equal(list(p), [(0.0, 1.0), (2.0, 3.0), (4.0, 5.0), (6.0, 7.0), (8.0, 9.0)])
# method sanity check
assert_equal(p.tolist(), [(0.0, 1.0), (2.0, 3.0), (4.0, 5.0), (6.0, 7.0), (8.0, 9.0)])
assert_equal(p.tolist(1), [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
assert_equal(p.getbbox(), (0.0, 1.0, 8.0, 9.0))
assert_equal(p.compact(5), 2)
assert_equal(list(p), [(0.0, 1.0), (4.0, 5.0), (8.0, 9.0)])
p.transform((1,0,1,0,1,1))
assert_equal(list(p), [(1.0, 2.0), (5.0, 6.0), (9.0, 10.0)])
# alternative constructors
p = ImagePath.Path([0, 1])
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path([0.0, 1.0])
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path([0, 1])
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path([(0, 1)])
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path(p)
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path(p.tolist(0))
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path(p.tolist(1))
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path(array.array("f", [0, 1]))
assert_equal(list(p), [(0.0, 1.0)])
p = ImagePath.Path(array.array("f", [0, 1]).tostring())
assert_equal(list(p), [(0.0, 1.0)])

9
Tests/test_imageqt.py Normal file
View File

@ -0,0 +1,9 @@
from tester import *
from PIL import Image
try:
from PIL import ImageQt
except ImportError as v:
skip(v)
success()

View File

@ -0,0 +1,22 @@
from tester import *
from PIL import Image
from PIL import ImageSequence
def test_sanity():
file = tempfile("temp.im")
im = lena("RGB")
im.save(file)
seq = ImageSequence.Iterator(im)
index = 0
for frame in seq:
assert_image_equal(im, frame)
assert_equal(im.tell(), index)
index = index + 1
assert_equal(index, 1)

6
Tests/test_imageshow.py Normal file
View File

@ -0,0 +1,6 @@
from tester import *
from PIL import Image
from PIL import ImageShow
success()

52
Tests/test_imagestat.py Normal file
View File

@ -0,0 +1,52 @@
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)

9
Tests/test_imagetk.py Normal file
View File

@ -0,0 +1,9 @@
from tester import *
from PIL import Image
try:
from PIL import ImageTk
except ImportError as v:
skip(v)
success()

Some files were not shown because too many files have changed in this diff Show More