~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to Tests/test_mode_i16.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from tester import *
 
2
 
 
3
from PIL import Image
 
4
 
 
5
def verify(im1):
 
6
    im2 = lena("I")
 
7
    assert_equal(im1.size, im2.size)
 
8
    pix1 = im1.load()
 
9
    pix2 = im2.load()
 
10
    for y in range(im1.size[1]):
 
11
        for x in range(im1.size[0]):
 
12
            xy = x, y
 
13
            if pix1[xy] != pix2[xy]:
 
14
                failure(
 
15
                    "got %r from mode %s at %s, expected %r" %
 
16
                    (pix1[xy], im1.mode, xy, pix2[xy])
 
17
                    )
 
18
                return
 
19
    success()
 
20
 
 
21
def test_basic():
 
22
    # PIL 1.1 has limited support for 16-bit image data.  Check that
 
23
    # create/copy/transform and save works as expected.
 
24
 
 
25
    def basic(mode):
 
26
 
 
27
        imIn = lena("I").convert(mode)
 
28
        verify(imIn)
 
29
 
 
30
        w, h = imIn.size
 
31
 
 
32
        imOut = imIn.copy()
 
33
        verify(imOut) # copy
 
34
 
 
35
        imOut = imIn.transform((w, h), Image.EXTENT, (0, 0, w, h))
 
36
        verify(imOut) # transform
 
37
 
 
38
        filename = tempfile("temp.im")
 
39
        imIn.save(filename)
 
40
 
 
41
        imOut = Image.open(filename)
 
42
 
 
43
        verify(imIn)
 
44
        verify(imOut)
 
45
 
 
46
        imOut = imIn.crop((0, 0, w, h))
 
47
        verify(imOut)
 
48
 
 
49
        imOut = Image.new(mode, (w, h), None)
 
50
        imOut.paste(imIn.crop((0, 0, w//2, h)), (0, 0))
 
51
        imOut.paste(imIn.crop((w//2, 0, w, h)), (w//2, 0))
 
52
 
 
53
        verify(imIn)
 
54
        verify(imOut)
 
55
 
 
56
        imIn = Image.new(mode, (1, 1), 1)
 
57
        assert_equal(imIn.getpixel((0, 0)), 1)
 
58
 
 
59
        imIn.putpixel((0, 0), 2)
 
60
        assert_equal(imIn.getpixel((0, 0)), 2)
 
61
 
 
62
        if mode == "L":
 
63
            max = 255
 
64
        else:
 
65
            max = 32767
 
66
 
 
67
        imIn = Image.new(mode, (1, 1), 256)
 
68
        assert_equal(imIn.getpixel((0, 0)), min(256, max))
 
69
 
 
70
        imIn.putpixel((0, 0), 512)
 
71
        assert_equal(imIn.getpixel((0, 0)), min(512, max))
 
72
 
 
73
    basic("L")
 
74
 
 
75
    basic("I;16")
 
76
    basic("I;16B")
 
77
    basic("I;16L")
 
78
 
 
79
    basic("I")
 
80
 
 
81
 
 
82
def test_tostring():
 
83
 
 
84
    def tostring(mode):
 
85
        if py3:
 
86
            return Image.new(mode, (1, 1), 1).tobytes()
 
87
        else:
 
88
            return Image.new(mode, (1, 1), 1).tostring()
 
89
 
 
90
    assert_equal(tostring("L"), b"\x01")
 
91
    assert_equal(tostring("I;16"), b"\x01\x00")
 
92
    assert_equal(tostring("I;16B"), b"\x00\x01")
 
93
    assert_equal(tostring("I"), b"\x01\x00\x00\x00")
 
94
 
 
95
 
 
96
def test_convert():
 
97
 
 
98
    im = lena("I")
 
99
 
 
100
    verify(im.convert("I;16"))
 
101
    verify(im.convert("I;16").convert("L"))
 
102
    verify(im.convert("I;16").convert("I"))
 
103
 
 
104
    verify(im.convert("I;16B"))
 
105
    verify(im.convert("I;16B").convert("L"))
 
106
    verify(im.convert("I;16B").convert("I"))