Exemplo n.º 1
0
def test_markov_image_generate_levels(args, kwargs, data, res):
    scanner = Scanner(lambda x: x)
    scanner.traversal = [HLines(), VLines()]
    scanner.levels = 2
    scanner.level_scale = [2]

    markov = MarkovImage(levels=2, scanner=scanner)
    markov.imgtype.convert = lambda x: [x]

    if data:
        markov.data([
            ['\x00', '\x01', '\x02', '\x03'],
            [(Scanner.START, '\x00'), '\x01',
             (Scanner.START, '\x01'), '\x02',
             (Scanner.START, '\x02'), '\x03',
             (Scanner.START, '\x03'), '\x00']
        ])

    if 'start_image' in kwargs:
        img = Image.new('P', (1, 1))
        img.putpixel((0, 0), 1)
        kwargs['start_image'] = img

    if isinstance(res, type):
        with pytest.raises(res):
            markov(*args, **kwargs)
    else:
        assert list(markov(*args, **kwargs).getdata()) == res
Exemplo n.º 2
0
def test_markov_image_generate(test, res):
    scanner = Scanner(lambda x: x)
    scanner.traversal = [HLines()]
    scanner.levels = 1
    scanner.level_scale = []
    markov = MarkovImage(
        scanner=scanner,
        parser=Parser()
    )
    markov.imgtype.convert = lambda x: [x]
    markov.data([['\x00', '\x01', '\x02', '\x03']])
    assert list(markov(*test).getdata()) == res
Exemplo n.º 3
0
def test_markov_image_properties():
    markov = MarkovImage()
    assert isinstance(markov.scanner, ImageScanner)
    assert isinstance(markov.parser, LevelParser)
    assert markov.scanner.levels == markov.levels
    assert markov.parser.levels == markov.levels
    markov.levels = 3
    assert markov.scanner.levels == markov.levels
    assert markov.parser.levels == markov.levels
    with pytest.raises(ValueError):
        markov.levels = -1
    assert markov.scanner.levels == markov.levels
    assert markov.parser.levels == markov.levels
    assert markov.levels == 3
Exemplo n.º 4
0
def test_markov_image_get_settings_json(mocker):
    get_settings_json = mocker.patch(
        'markovchain.Markov.get_settings_json',
        return_value={'x': 0}
    )
    markov = MarkovImage(
        levels=2,
        imgtype=Mock(save=lambda: 3)
    )
    data = markov.get_settings_json()
    assert data == {
        'x': 0,
        'levels': 2,
        'imgtype': 3
    }
    get_settings_json.assert_called_once_with()
Exemplo n.º 5
0
from PIL import Image
from markovchain import JsonStorage
from markovchain.image import MarkovImage

markov = MarkovImage()

markov.data(Image.open('twitterbot.png'))

width = 32
height = 16
img = markov(width, height)
with open('output.png', 'wb') as fp:
    img.save(fp)

markov.save('markov.json')

markov = MarkovImage.from_file('markov.json')
Exemplo n.º 6
0
def test_markov_image_generate_error():
    markov = MarkovImage()
    with pytest.raises(KeyError):
        markov(2, 2)
Exemplo n.º 7
0
def test_markov_image_eq(test, test2, res):
    assert (MarkovImage(*test) == MarkovImage(*test2)) == res