Fredrik Lundh is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

effbot / pil-2009-raclette http://effbot.org/zone/pil-index.htm

Work repository for PIL 1.1.7 and beyond. The "default" branch should be fairly solid, the other branches less so. For production use, see the pil-117 repository.

Clone this repository (size: 1.6 MB): HTTPS / SSH
hg clone https://bitbucket.org/effbot/pil-2009-raclette
hg clone ssh://hg@bitbucket.org/effbot/pil-2009-raclette

pil-2009-raclette / Tests / test_image_array.py

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