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
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 =
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
like this

So, how can I convert each pixel to binary?

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

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
Posts: 2215
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: How to change the bits to binary?

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

I assume you're using

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