Page 1 of 1

How to change the bits to binary?

Posted: Fri Jan 22, 2021 8:55 am
by destoriNAVY
I wanna know How to change the bits to binary, like 'numpy's frombuffer'
import pyb
from JPEGdecoder import jpeg
with open('photo4.jpg', 'rb') as f:
data = f.read()
print(data)
I have to convert each pixel to binary.
frombuffer is also represented in matrix form, I also need such a function.

but the result is
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xff\xdb\x00C\x00\x02\x01\x01\x02\x01\x01
like this

So, how can I convert each pixel to binary?

Re: How to change the bits to binary?

Posted: Fri Jan 22, 2021 9:17 am
by pythoncoder
Converting from JPEG to a bitmap involves a lot of mathematics: the JPEG format is highly compressed. Options are to find a library which can do the conversion on the Pyboard, use a different file format, or - if you just want to display an image - use a display which can handle JPEG streams natively, like the official LCD160CR display.

Re: How to change the bits to binary?

Posted: Mon Jan 25, 2021 12:19 am
by jimmo
I assume you're using https://github.com/remixer-dec/mpy-img-decoder

See the examples on the github page, but the way this works is that it calls a function of your choice with the pixel data.

Code: Select all

from JPEGdecoder import jpeg

def pixel_handler(x, y, color):
  # do something with color (it's an integer in the form 0xRRGGBB)

jpeg('image.jpg', callback=pixel_handler).render(x, y)