onebit/bitpixel.py -rw-r--r-- 1.6 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from PIL import Image
from sys import argv

def detect_size_from_file(filename: str):

    x = None
    y = 0
    with open(filename, 'r') as s:
        lines = s.readlines()

        for line in lines:
            line = line.strip()

            if line == '':
                return (x, y)
            y = y + 1

            if not x:
                x = len(line)
            if x != len(line):
                raise ValueError("Lines are not equal")
        return (x,y)

def set_pixels(image, frame, lightup="*"):
    x = 0
    y = 0
    for line in frame:
        for bit in line:
            print(x, y)
            if bit == lightup:
                image.putpixel((x, y), 255)
            x = x + 1
        y = y + 1
        x = 0
    return image

def split_frames(filename, frame_separator = ''):
    with open(filename, 'r') as fh:
        lines = fh.readlines()
        if not any([line for line in lines if line.strip() == frame_separator]):
            raise TypeError("Not an animation")

        frame = []
        for line in lines:
            if line.strip() != frame_separator:
                frame.append(line)
            else:
                print(frame)
                yield frame
                frame = []
        if frame:
            yield frame

def create_image(size: list):
    return Image.new("1", size)

if len(argv) == 2:
    xy = detect_size_from_file(argv[1])
    print(xy)
    image = create_image(xy)
    try:
        for idx, frame in enumerate(split_frames(argv[1])):
            print(frame)
            image = set_pixels(image, frame)
            image.save('test_{}.png'.format(idx), "PNG")
    except TypeError:
        print("nope")