~bronikowski/onebit

ref: 5fd46ca669875a3742f0fe2c2709930c369823ac onebit/bitpixel.py -rw-r--r-- 1.1 KiB
5fd46ca6Emil Oppeln-Bronikowski Initial release 3 years ago
                                                                                
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
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, filename, 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
    return image

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)
    image = set_pixels(image, argv[1])
    image.save('test.png', "PNG")