converting png file to bytearray

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
BigStupidBeast
Posts: 9
Joined: Thu May 28, 2020 5:40 pm
Location: sorry for my English)

Re: converting png file to bytearray

Post by BigStupidBeast » Mon Jul 13, 2020 11:45 pm

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)

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: converting png file to bytearray

Post by jimmo » Thu Jul 16, 2020 3:55 am

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

Post Reply