markovchain.image package

Submodules

markovchain.image.markov module

class markovchain.image.markov.MarkovImage(levels=1, imgtype=None, *args, **kwargs)[source]

Bases: markovchain.base.Markov

Markov image generator.

DEFAULT_SCANNER

type – Default scanner class.

DEFAULT_PARSER

type – Default parser class.

DEFAULT_STORAGE

type – Default storage class.

scanner

markovchain.scanner.Scanner

parser

markovchain.parser.ParserBase

storage

markovchain.storage.Storage

DEFAULT_IMGTYPE

alias of Indexed

DEFAULT_PARSER

alias of LevelParser

DEFAULT_SCANNER

alias of ImageScanner

__call__(width, height, state_size=None, levels=None, start_level=-1, start_image=None, dataset='')[source]

Generate an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • state_size (None or int or list of int, optional) – State size (default: None).
  • levels (int, optional) – Number of levels to generate (default: self.scanner.levels).
  • start_level (int, optional) – Initial level (default: -1).
  • start_image (PIL.Image or None) – Initial level image (default: None).
  • dataset (str, optional) – Dataset key prefix (default: ‘’).
Returns:

Generated image.

Return type:

PIL.Image

Raises:

ValueError

__init__(levels=1, imgtype=None, *args, **kwargs)[source]

Markov image generator constructor.

Parameters:
data(data, part=False, dataset='')[source]

Parse data and update links.

Parameters:
  • data (PIL.Image) – Image to parse.
  • part (bool, optional) – True if data is partial (default: False).
  • dataset (str, optional) – Dataset key prefix (default: ‘’).
get_settings_json()[source]

Convert generator settings to JSON.

Returns:JSON data.
Return type:dict
levels

int – Number of levels.

markovchain.image.scanner module

class markovchain.image.scanner.ImageScanner(resize=None, levels=1, level_scale=4, scale=3, traversal=None)[source]

Bases: markovchain.scanner.Scanner

Image scanner class.

resize

(int, int) or None – If not None, resize images before scanning.

min_size

int – Minimum image size.

Examples

>>> scan = Scanner(lambda data: data.split())
>>> scan('a b c')
['a', 'b', 'c']
__call__(img, part=False)[source]

Scan an image.

Parameters:
  • img (PIL.Image) – Image to scan.
  • part (bool, optional) – True if data is partial.
Raises:

NotImplementedError – If part is True.

Returns:

Token generator.

Return type:

generator of (str or markovchain.scanner.Scanner.END or (markovchain.scanner.Scanner.START, str))

__init__(resize=None, levels=1, level_scale=4, scale=3, traversal=None)[source]

Image scanner constructor.

Parameters:
  • resize ((int, int) or None, optional) – If not None, resize images before scanning (default: None).
  • convert_type (int, optional) – Image conversion type (default: 1).
  • dither (bool) – If True, enable image dithering (default: False).
  • palette (list of int, optional) – Image palette (default: markovchain.image.util.palette(8, 4, 8)).
  • levels (int, optional) – Number of scanner levels (default: 1).
  • level_scale (int or list of int) – Level scale factors.
  • scale (int, optional) – Scale quality (default: PIL.Image.BICUBIC).
  • traversal (dict or markovchain.image.traversal.Traversal or list of (dict or markovchain.image.traversal.Traversal), optional) – Level traversals (default: markovchain.image.traversal.HLines()).
input(img)[source]

Resize input image if necessary.

Parameters:img (PIL.Image) – Input image.
Raises:ValueError – If input image is too small.
Returns:Resized image or input image.
Return type:PIL.Image
level(img, level)[source]

Get image level.

Parameters:
  • img (PIL.Image) – Input image.
  • level (int) – Level number.
Returns:

Converted image.

Return type:

PIL.Image

level_scale

list of int – Level scale factors.

levels

int – Number of levels.

save()[source]

Convert to JSON.

Returns:JSON data.
Return type:dict
traversal

markovchain.image.traversal.Traversal – Image traversal.

markovchain.image.traversal module

class markovchain.image.traversal.Blocks(block_size=(16, 16), block_sentences=False, traverse_image=None, traverse_block=None)[source]

Bases: markovchain.image.traversal.Traversal

Block traversal.

block_size

(int, int) – Block size.

block_sentences

bool – Generate a block end after each block.

traverse_image

markovchain.image.traversal.Traversal – Image traversal.

traverse_block

markovchain.image.traversal.Traversal – Block traversal.

Examples

>>> traverse = Blocks(block_size=(2, 2),
...                   traverse_image=HLines(),
...                   traverse_block=VLines())
>>> list(traverse(4, 4))
[(0, 0), (0, 1), (1, 0), (1, 1),
 (2, 0), (2, 1), (3, 0), (3, 1),
 (0, 2), (0, 3), (1, 2), (1, 3),
 (2, 2), (2, 3), (3, 2), (3, 3)]
__call__(width, height, ends=True)[source]

Traverse an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • ends (bool, optional) – Generate block ends (default: True).
Returns:

Points and block ends.

Return type:

generator of ((int, int) or None)

__init__(block_size=(16, 16), block_sentences=False, traverse_image=None, traverse_block=None)[source]

Block traversal constructor.

Parameters:
  • block_size ((int, int), optional) – Block size (default: (16, 16)).
  • block_sentences (bool, optional) – Generate a block end after each block (default: False).
  • traverse_image (markovchain.image.traversal.Traversal or dict, optional) – Image traversal object or JSON data (default: HLines()).
  • traverse_block (markovchain.image.traversal.Traversal or dict, optional) – Block traversal object or JSON data (default: HLines()).
save()[source]

Convert to JSON.

Returns:JSON data.
Return type:dict
class markovchain.image.traversal.HLines(reverse=0, line_sentences=False)[source]

Bases: markovchain.image.traversal.Lines

Horizontal line traversal.

reverse

int – If greater than 0, reverse every nth line.

line_sentences

bool – Generate a block end after each line.

Examples

>>> traverse = HLines()
>>> list(traverse(2, 2))
[(0, 0), (1, 0), (0, 1), (1, 1)]
>>> traverse = HLines(reverse=1)
>>> list(traverse(2, 2))
[(1, 0), (0, 0), (1, 1), (0, 1)]
>>> traverse = HLines(reverse=2)
>>> list(traverse(2, 2))
[(1, 0), (0, 0), (0, 1), (1, 1)]
>>> traverse = HLines(line_sentences=True)
>>> list(traverse(2, 2))
[(0, 0), (1, 0), None, (0, 1), (1, 1), None]
>>> list(traverse(2, 2, ends=False))
[(0, 0), (1, 0), (0, 1), (1, 1)]
__call__(width, height, ends=True)[source]

Traverse an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • ends (bool, optional) – Generate block ends (default: True).
Returns:

Points and block ends.

Return type:

generator of ((int, int) or None)

class markovchain.image.traversal.Hilbert[source]

Bases: markovchain.image.traversal.Traversal

Hilbert curve traversal.

POSITION

list of (int, int) – Block positions.

Examples

>>> traverse = Hilbert()
>>> list(traverse(2, 2))
[(0, 0), (0, 1), (1, 1), (1, 0)]
>>> list(traverse(3, 5))
[(0, 0), (0, 1), (1, 1), (1, 0), (2, 0), (2, 1),
 (2, 2), (2, 3), (1, 3), (1, 2), (0, 2), (0, 3),
 (0, 4), (1, 4), (2, 4)]
POSITION = [(0, 0), (0, 1), (1, 1), (1, 0)]
__call__(width, height, ends=True)[source]

Traverse an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • ends (bool, optional) – Unused (default: True).
Returns:

Points.

Return type:

generator of (int, int)

classmethod get_point(idx, size)[source]

Get curve point coordinates by index.

Parameters:
  • idx (int) – Point index.
  • size (int) – Curve size.
Returns:

Point coordinates.

Return type:

(int, int)

classmethod get_point_in_block(x, y, block_idx, block_size)[source]

Get point coordinates in next block.

Parameters:
  • x (int) – X coordinate in current block.
  • y (int) – Y coordinate in current block.
  • block_index (int) – Current block index in next block.
  • block_size (int) – Current block size.
Raises:

IndexError – If block index is out of range.

Returns:

Point coordinates.

Return type:

(int, int)

class markovchain.image.traversal.Lines(reverse=0, line_sentences=False)[source]

Bases: markovchain.image.traversal.Traversal

Base line traversal class.

reverse

int – If greater than 0, reverse every nth line.

line_sentences

bool – Generate a block end after each line.

Examples

>>> class SaveLoadGroup(SaveLoad):
...     classes = {}
...
>>> class SaveLoadObject(SaveLoadGroup):
...     def __init__(self, attr=None):
...         self.attr = attr
...     def save(self):
...         data = super().save()
...         data['attr'] = self.attr
...         return data
...
>>> SaveLoadGroup.add_class(SaveLoadObject)
>>> SaveLoadGroup.classes
{'SaveLoadObject': <class '__main__.SaveLoadObject'>}
>>> obj = SaveLoadObject(0)
>>> data = obj.save()
>>> data
{'attr': 0, '__class__': 'SaveLoadObject'}
>>> obj2 = SaveLoadGroup.load(data)
>>> type(obj2)
<class '__main__.SaveLoadObject'>
>>> obj2.attr
0
__init__(reverse=0, line_sentences=False)[source]

Base line traversal constructor.

Parameters:
  • reverse (int, optional) – If greater than 0, reverse every nth line (default: 0).
  • line_sentences (bool, optional) – Generate a block end after each line (default: False).
save()[source]

Convert to JSON.

Returns:JSON data.
Return type:dict
class markovchain.image.traversal.Spiral(reverse=False)[source]

Bases: markovchain.image.traversal.Traversal

Spiral traversal.

reverse

bool – Reverse traversal order.

Examples

>>> traverse = Spiral()
>>> list(traverse(3, 3))
[(1, 1), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0), (1, 0), (0, 0)]
>>> traverse = Spiral(True)
>>> list(traverse(3, 3))
[(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (1, 2), (0, 2), (0, 1), (1, 1)]
__call__(width, height, ends=True)[source]

Traverse an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • ends (bool, optional) – Unused (default: True).
Returns:

Points.

Return type:

generator of (int, int)

__init__(reverse=False)[source]

Spiral traversal constructor.

Parameters:reverse (bool, optional) – Reverse traversal order (default: False).
save()[source]

Convert to JSON.

Returns:JSON data.
Return type:dict
class markovchain.image.traversal.Traversal[source]

Bases: markovchain.util.SaveLoad

Base image traversal class.

classes

dict – Image traversal class group.

Examples

>>> class SaveLoadGroup(SaveLoad):
...     classes = {}
...
>>> class SaveLoadObject(SaveLoadGroup):
...     def __init__(self, attr=None):
...         self.attr = attr
...     def save(self):
...         data = super().save()
...         data['attr'] = self.attr
...         return data
...
>>> SaveLoadGroup.add_class(SaveLoadObject)
>>> SaveLoadGroup.classes
{'SaveLoadObject': <class '__main__.SaveLoadObject'>}
>>> obj = SaveLoadObject(0)
>>> data = obj.save()
>>> data
{'attr': 0, '__class__': 'SaveLoadObject'}
>>> obj2 = SaveLoadGroup.load(data)
>>> type(obj2)
<class '__main__.SaveLoadObject'>
>>> obj2.attr
0
__call__(width, height, ends=True)[source]

Traverse an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • ends (bool, optional) – Generate block ends (default: True).
Raises:

NotImplementedError

Returns:

Points and block ends.

Return type:

generator of ((int, int) or None)

classes = {'Spiral': <class 'markovchain.image.traversal.Spiral'>, 'Hilbert': <class 'markovchain.image.traversal.Hilbert'>, 'Blocks': <class 'markovchain.image.traversal.Blocks'>, 'HLines': <class 'markovchain.image.traversal.HLines'>, 'VLines': <class 'markovchain.image.traversal.VLines'>}
class markovchain.image.traversal.VLines(reverse=0, line_sentences=False)[source]

Bases: markovchain.image.traversal.Lines

Vertical line traversal.

reverse

int – If greater than 0, reverse every nth line.

line_sentences

bool – Generate a block end after each line.

Examples

>>> traverse = VLines()
>>> list(traverse(2, 2))
[(0, 0), (0, 1), (1, 0), (1, 1)]
>>> traverse = VLines(reverse=1)
>>> list(traverse(2, 2))
[(0, 1), (0, 0), (1, 1), (1, 0)]
>>> traverse = VLines(reverse=2)
>>> list(traverse(2, 2))
[(1, 0), (0, 0), (1, 0), (1, 1)]
>>> traverse = VLines(line_sentences=True)
>>> list(traverse(2, 2))
[(0, 0), (0, 1), None, (1, 0), (1, 1), None]
>>> list(traverse(2, 2, ends=False))
[(0, 0), (0, 1), (1, 0), (1, 1)]
__call__(width, height, ends=True)[source]

Traverse an image.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
  • ends (bool, optional) – Generate block ends (default: True).
Returns:

Points and block ends.

Return type:

generator of ((int, int) or None)

markovchain.image.type module

class markovchain.image.type.Grayscale[source]

Bases: markovchain.image.type.ImageType

Grayscale image type.

classes

dict – Image traversal class group.

mode

str – Image mode.

channel_mode

str or None – Channel image mode.

channels

list of str – Channel dataset keys.

Examples

>>> class SaveLoadGroup(SaveLoad):
...     classes = {}
...
>>> class SaveLoadObject(SaveLoadGroup):
...     def __init__(self, attr=None):
...         self.attr = attr
...     def save(self):
...         data = super().save()
...         data['attr'] = self.attr
...         return data
...
>>> SaveLoadGroup.add_class(SaveLoadObject)
>>> SaveLoadGroup.classes
{'SaveLoadObject': <class '__main__.SaveLoadObject'>}
>>> obj = SaveLoadObject(0)
>>> data = obj.save()
>>> data
{'attr': 0, '__class__': 'SaveLoadObject'}
>>> obj2 = SaveLoadGroup.load(data)
>>> type(obj2)
<class '__main__.SaveLoadObject'>
>>> obj2.attr
0
mode = 'L'
class markovchain.image.type.ImageType[source]

Bases: markovchain.util.SaveLoad

Base image type class.

classes

dict – Image traversal class group.

mode

str – Image mode.

channel_mode

str or None – Channel image mode.

channels

list of str – Channel dataset keys.

Examples

>>> class SaveLoadGroup(SaveLoad):
...     classes = {}
...
>>> class SaveLoadObject(SaveLoadGroup):
...     def __init__(self, attr=None):
...         self.attr = attr
...     def save(self):
...         data = super().save()
...         data['attr'] = self.attr
...         return data
...
>>> SaveLoadGroup.add_class(SaveLoadObject)
>>> SaveLoadGroup.classes
{'SaveLoadObject': <class '__main__.SaveLoadObject'>}
>>> obj = SaveLoadObject(0)
>>> data = obj.save()
>>> data
{'attr': 0, '__class__': 'SaveLoadObject'}
>>> obj2 = SaveLoadGroup.load(data)
>>> type(obj2)
<class '__main__.SaveLoadObject'>
>>> obj2.attr
0
channel_mode = None
channels = ['']
classes = {'Indexed': <class 'markovchain.image.type.Indexed'>, 'RGB': <class 'markovchain.image.type.RGB'>, 'Grayscale': <class 'markovchain.image.type.Grayscale'>}
convert(img)[source]

Convert an image to type.

Parameters:img (PIL.Image.Image) –
Returns:
Return type:tuple of PIL.Image.Image
create(width, height)[source]

Create an image of type.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
Returns:

Return type:

PIL.Image.Image

create_channel(width, height)[source]

Create a channel.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
Returns:

Return type:

PIL.Image.Image

merge(imgs)[source]

Merge image channels.

Parameters:imgs (list of PIL.Image.Image) –
Returns:
Return type:PIL.Image.Image
Raises:ValueError – If image channel list is empty.
mode = None
class markovchain.image.type.Indexed(palette=None, dither=True, convert_type=1)[source]

Bases: markovchain.image.type.ImageType

Indexed image type.

palette

list of int

dither

bool

convert_type

int

Examples

>>> class SaveLoadGroup(SaveLoad):
...     classes = {}
...
>>> class SaveLoadObject(SaveLoadGroup):
...     def __init__(self, attr=None):
...         self.attr = attr
...     def save(self):
...         data = super().save()
...         data['attr'] = self.attr
...         return data
...
>>> SaveLoadGroup.add_class(SaveLoadObject)
>>> SaveLoadGroup.classes
{'SaveLoadObject': <class '__main__.SaveLoadObject'>}
>>> obj = SaveLoadObject(0)
>>> data = obj.save()
>>> data
{'attr': 0, '__class__': 'SaveLoadObject'}
>>> obj2 = SaveLoadGroup.load(data)
>>> type(obj2)
<class '__main__.SaveLoadObject'>
>>> obj2.attr
0
__init__(palette=None, dither=True, convert_type=1)[source]
channel_mode = None
convert(img)[source]

Convert an image to type.

Parameters:img (PIL.Image.Image) –
Returns:
Return type:tuple of PIL.Image.Image
create(width, height)[source]

Create an image of type.

Parameters:
  • width (int) – Image width.
  • height (int) – Image height.
Returns:

Return type:

PIL.Image.Image

mode = 'P'
palette
save()[source]

Convert an object to JSON.

Returns:JSON data.
Return type:dict
class markovchain.image.type.RGB[source]

Bases: markovchain.image.type.ImageType

RGB image type.

classes

dict – Image traversal class group.

mode

str – Image mode.

channel_mode

str or None – Channel image mode.

channels

list of str – Channel dataset keys.

Examples

>>> class SaveLoadGroup(SaveLoad):
...     classes = {}
...
>>> class SaveLoadObject(SaveLoadGroup):
...     def __init__(self, attr=None):
...         self.attr = attr
...     def save(self):
...         data = super().save()
...         data['attr'] = self.attr
...         return data
...
>>> SaveLoadGroup.add_class(SaveLoadObject)
>>> SaveLoadGroup.classes
{'SaveLoadObject': <class '__main__.SaveLoadObject'>}
>>> obj = SaveLoadObject(0)
>>> data = obj.save()
>>> data
{'attr': 0, '__class__': 'SaveLoadObject'}
>>> obj2 = SaveLoadGroup.load(data)
>>> type(obj2)
<class '__main__.SaveLoadObject'>
>>> obj2.attr
0
channel_mode = 'L'
channels = ['_R', '_G', '_B']
convert(img)[source]

Convert an image to type.

Parameters:img (PIL.Image.Image) –
Returns:
Return type:tuple of PIL.Image.Image
mode = 'RGB'

markovchain.image.util module

markovchain.image.util.convert(ctype, img, palette_img, dither=False)[source]

Convert an image to palette type.

Parameters:
  • ctype (int) – Conversion type.
  • img (PIL.Image) – Image to convert.
  • palette_img (PIL.Image) – Palette source image.
  • dither (bool, optional) – Enable dithering (default: False).
Raises:

ValueError – If palette_img has no palette.

Returns:

Converted image.

Return type:

PIL.Image

markovchain.image.util.palette(hues, saturations, values)[source]

Generate a palette.

Parameters:
  • hues (int) – Number of hues.
  • saturations (int) – Number of saturations.
  • values (int) – Number of values.
Raises:

ValueError – If hues * saturations * values > 256 or min(hues, saturations, values) < 1.

Returns:

Palette for PIL.Image.putpalette.

Return type:

list of int

markovchain.image.util.pixel_to_state(px)[source]

Convert pixel value to generator state.

Parameters:px (int) –
Returns:
Return type:str
markovchain.image.util.state_to_pixel(state)[source]

Convert generator state to pixel value.

Parameters:state (str) –
Returns:
Return type:int

Module contents