How to change the bits to binary?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
destoriNAVY
Posts: 13
Joined: Wed Jan 20, 2021 7:19 am

How to change the bits to binary?

Post by destoriNAVY » Fri Jan 22, 2021 8:55 am

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?

User avatar
pythoncoder
Posts: 4914
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to change the bits to binary?

Post by pythoncoder » Fri Jan 22, 2021 9:17 am

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.
Peter Hinch

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

Re: How to change the bits to binary?

Post by jimmo » Mon Jan 25, 2021 12:19 am

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)

Post Reply