Page 4 of 4

Re: converting png file to bytearray

Posted: Mon Jul 13, 2020 11:45 pm
by BigStupidBeast
Hello everyone.
Playing with pbm in P4 is simple.
BUT. If i have TRUE pbm in P1 i have some fun.

I can write pbm file

P1
# cool image
7 7
0001000
0011100
0111110
1111111
0011100
0011100
0011100

I can convert it into 1 string

Code: Select all

with open('arrow.pbm') as f:
    data_list = f.readlines()
    # for framebuffer
    [width, hight] = (data_list[2].split())

    body = ''
    body += body.join(data_list[3:]).replace('\n', '')
But i can't display it correctly.
i mean i must encode all binary characters into bytes. but how?
where is instruction about bytes? (crying)

Re: converting png file to bytearray

Posted: Thu Jul 16, 2020 3:55 am
by jimmo
BigStupidBeast wrote:
Mon Jul 13, 2020 11:45 pm
i mean i must encode all binary characters into bytes. but how?
I think what you're asking is how to convert the string "0001000" to the corresponding integer value (i.e. 8).

In Python, you can use `int(str, base)`, i.e.

Code: Select all

>>> int("0001000", 2)
8