pillowcases module

Library that makes it possible to work in a concise, algebraic way with Python Imaging Library image objects.

class pillowcases.pillowcases.Image[source]

Bases: PIL.Image.Image

Derived class for PIL.Image.Image that is hashable. When the pillowcases.pillowcases module is imported, the PIL.Image.Image class is redefined to refer to the pillowcases.pillowcases.Image class.

>>> import pillowcases
>>> i = PIL.Image.frombytes('RGBA', (2, 2), bytes([0]*16))
>>> isinstance(i, pillowcases.Image)
True

Because instances of this derived class are hashable, they can be added as elements to set objects and can be used as keys in dict objects.

>>> j = PIL.Image.frombytes('RGBA', (2, 2), bytes([0]*16))
>>> k = PIL.Image.frombytes('RGBA', (2, 2), bytes([255]*16))
>>> len({i, j, k})
2
>>> d = {j: 1, k: 2}
>>> d[k]
2

Compare the above to the default behavior of the PIL.Image.Image class, demonstrated below.

>>> from importlib import reload
>>> PIL.Image = reload(PIL.Image)
>>> i = PIL.Image.frombytes('RGBA', (2, 2), bytes([0]*16))
>>> j = PIL.Image.frombytes('RGBA', (2, 2), bytes([0]*16))
>>> j = PIL.Image.frombytes('RGBA', (2, 2), bytes([255]*16))
>>> len({i, j, k})
Traceback (most recent call last):
    ...
TypeError: unhashable type: 'Image'