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¶
Default scanner class.
- Type
type
- DEFAULT_PARSER¶
Default parser class.
- Type
type
- DEFAULT_STORAGE¶
Default storage class.
- Type
type
- scanner¶
- parser¶
- storage¶
- Type
markovchain.storage.Storage
- DEFAULT_IMGTYPE¶
alias of
markovchain.image.type.Indexed
- DEFAULT_PARSER¶
alias of
markovchain.parser.LevelParser
- DEFAULT_SCANNER¶
- __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
orint
orlist
ofint
, 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
orNone
) – 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
levels (
int
, optional) – Number of levels.imgtype (
None
ordict
ormarkovchain.image.type.ImageType
) –
- 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
- property levels¶
Number of levels.
- Type
int
markovchain.image.scanner module¶
- class markovchain.image.scanner.ImageScanner(resize=None, levels=1, level_scale=4, scale=Resampling.BICUBIC, traversal=None)[source]¶
Bases:
markovchain.scanner.Scanner
Image scanner class.
- resize¶
If not None, resize images before scanning.
- Type
(
int
,int
) orNone
- min_size¶
Minimum image size.
- Type
int
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
isTrue
.- Returns
Token generator.
- Return type
generator
of (str
ormarkovchain.scanner.Scanner.END
or (markovchain.scanner.Scanner.START
,str
))
- __init__(resize=None, levels=1, level_scale=4, scale=Resampling.BICUBIC, traversal=None)[source]¶
Image scanner constructor.
- Parameters
resize ((
int
,int
) orNone
, optional) – If notNone
, resize images before scanning (default:None
).convert_type (
int
, optional) – Image conversion type (default: 1).dither (
bool
) – IfTrue
, enable image dithering (default:False
).palette (
list
ofint
, optional) – Image palette (default:markovchain.image.util.palette(8, 4, 8)
).levels (
int
, optional) – Number of scanner levels (default: 1).level_scale (
int
orlist
ofint
) – Level scale factors.scale (
int
, optional) – Scale quality (default:PIL.Image.BICUBIC
).traversal (
dict
ormarkovchain.image.traversal.Traversal
orlist
of (dict
ormarkovchain.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
- property level_scale¶
Level scale factors.
- Type
list
ofint
- property levels¶
Number of levels.
- Type
int
- property 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¶
Block size.
- Type
(
int
,int
)
- block_sentences¶
Generate a block end after each block.
- Type
bool
- traverse_image¶
Image traversal.
- traverse_block¶
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
) orNone
)
- __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
ordict
, optional) – Image traversal object or JSON data (default: HLines()).traverse_block (
markovchain.image.traversal.Traversal
ordict
, optional) – Block traversal object or JSON data (default: HLines()).
- class markovchain.image.traversal.HLines(reverse=0, line_sentences=False)[source]¶
Bases:
markovchain.image.traversal.Lines
Horizontal line traversal.
- reverse¶
If greater than 0, reverse every nth line.
- Type
int
- line_sentences¶
Generate a block end after each line.
- Type
bool
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)]
- class markovchain.image.traversal.Hilbert[source]¶
Bases:
markovchain.image.traversal.Traversal
Hilbert curve traversal.
- POSITION¶
- Type
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¶
If greater than 0, reverse every nth line.
- Type
int
- line_sentences¶
Generate a block end after each line.
- Type
bool
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
- class markovchain.image.traversal.Spiral(reverse=False)[source]¶
Bases:
markovchain.image.traversal.Traversal
Spiral traversal.
- reverse¶
- Type
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
)
- class markovchain.image.traversal.Traversal[source]¶
Bases:
markovchain.util.SaveLoad
Base image traversal class.
- classes¶
- Type
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
- abstract __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
) orNone
)
- classes = {'Blocks': <class 'markovchain.image.traversal.Blocks'>, 'HLines': <class 'markovchain.image.traversal.HLines'>, 'Hilbert': <class 'markovchain.image.traversal.Hilbert'>, 'Spiral': <class 'markovchain.image.traversal.Spiral'>, '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¶
If greater than 0, reverse every nth line.
- Type
int
- line_sentences¶
Generate a block end after each line.
- Type
bool
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)]
markovchain.image.type module¶
- class markovchain.image.type.Grayscale[source]¶
Bases:
markovchain.image.type.ImageType
Grayscale image type.
- classes¶
Image traversal class group.
- Type
dict
- mode¶
Image mode.
- Type
str
- channel_mode¶
Channel image mode.
- Type
str
or None
- channels¶
Channel dataset keys.
- Type
list
ofstr
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¶
Image traversal class group.
- Type
dict
- mode¶
Image mode.
- Type
str
- channel_mode¶
Channel image mode.
- Type
str
or None
- channels¶
Channel dataset keys.
- Type
list
ofstr
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 = {'Grayscale': <class 'markovchain.image.type.Grayscale'>, 'Indexed': <class 'markovchain.image.type.Indexed'>, 'RGB': <class 'markovchain.image.type.RGB'>}¶
- convert(img)[source]¶
Convert an image to type.
- Parameters
img (
PIL.Image.Image
) –- Return type
tuple
ofPIL.Image.Image
- create(width, height)[source]¶
Create an image of type.
- Parameters
width (
int
) – Image width.height (
int
) – Image height.
- Return type
PIL.Image.Image
- create_channel(width, height)[source]¶
Create a channel.
- Parameters
width (
int
) – Image width.height (
int
) – Image height.
- Return type
PIL.Image.Image
- merge(imgs)[source]¶
Merge image channels.
- Parameters
imgs (
list
ofPIL.Image.Image
) –- 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¶
- Type
list
ofint
- dither¶
- Type
bool
- convert_type¶
- 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
- channel_mode = None¶
- convert(img)[source]¶
Convert an image to type.
- Parameters
img (
PIL.Image.Image
) –- Return type
tuple
ofPIL.Image.Image
- create(width, height)[source]¶
Create an image of type.
- Parameters
width (
int
) – Image width.height (
int
) – Image height.
- Return type
PIL.Image.Image
- mode = 'P'¶
- property palette¶
- class markovchain.image.type.RGB[source]¶
Bases:
markovchain.image.type.ImageType
RGB image type.
- classes¶
Image traversal class group.
- Type
dict
- mode¶
Image mode.
- Type
str
- channel_mode¶
Channel image mode.
- Type
str
or None
- channels¶
Channel dataset keys.
- Type
list
ofstr
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
) –- Return type
tuple
ofPIL.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.get_image_scale(scale)[source]¶
Get image scale mode by name.
- Parameters
scale (
str
orint
) –- Return type
int
- 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
ofint