~bronikowski/onebit

4d4bf7cde13cf31eb664f51bcde9d2328183e89c — Emil Oppeln-Bronikowski 2 years ago 5fd46ca
Produce animation frames by separator
2 files changed, 57 insertions(+), 13 deletions(-)

M bitpixel.py
A few
M bitpixel.py => bitpixel.py +33 -13
@@ 21,21 21,36 @@ def detect_size_from_file(filename: str):
                raise ValueError("Lines are not equal")
        return (x,y)

def set_pixels(image, filename, lightup="*"):
def set_pixels(image, frame, lightup="*"):
    x = 0
    y = 0
    with open(filename, 'r') as s:
        lines = s.readlines()
        for line in lines:
            for bit in line:
                print(x, y)
                if bit == lightup:
                    image.putpixel((x, y), 255)
                x = x + 1
            y = y + 1
            x = 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)



@@ 43,5 58,10 @@ if len(argv) == 2:
    xy = detect_size_from_file(argv[1])
    print(xy)
    image = create_image(xy)
    image = set_pixels(image, argv[1])
    image.save('test.png', "PNG")
    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")

A few => few +24 -0
@@ 0,0 1,24 @@
...**...
..*..*..
...**...
.******.
...**...
..*..*..
..*..*..
********

*.*.*.*.
.*.*.*.*
*.*.*.*.
.*.*.*.*
*.*.*.*.
.*.*.*.*
********

...**...
..*..*..
...**...
.******.
...**...
..*..*..
..*..*..