Scale Up/Down Black and White .pbm Picture in MicroPython

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
marioard
Posts: 2
Joined: Fri May 15, 2020 1:22 am

Scale Up/Down Black and White .pbm Picture in MicroPython

Post by marioard » Tue Sep 08, 2020 2:03 pm

Dear friends,
This is my first time sending a question on this forum. I'm new in MicroPython and really appreciate all the work the community put on this!

I want to make a function that takes as an input a black and white icon represented as a bytearray as scale it up/down a number of pixels.

My intention is to display a single .pbm icon file in different sizes in a OLED display. Therefore, instead of saving every size used as different .pbm files, I want to use the same .pbm file image and scale it as needed.

Code: Select all

def scale_image(data, p)
	#TODO: Scale the image contain on the bytearray called data by p pixels 

def display_image(self, image_path="/default.pbm", x=0, y=0, scale=1):
    with open(image_path, 'rb') as photo:
        photo.readline()
        photo.readline()
        width, height = [int(v) for v in photo.readline().split()]
        data = bytearray(photo.read())
	if scale > 1:
		data = scale_image(data, scale)
    buf = framebuf.FrameBuffer(data, width, height, framebuf.MONO_HLSB)
    
    # Send to the oled display driver
    self.driver.framebuf.blit(buf, x, y)
    self.driver.show()
My .pbm file looks like this:

Code: Select all

P4
# Created by GIMP version 2.10.14 PNM plug-in
30 30
\xff\xff\xf0|\xff\xff\xc7<\xff\xff\x9f\xbc\xff\xff?<\x8f\xfe~|'\xfc\xfc\xfcs\xfd\xf9\xfcy\xf9\xf3\xfc|\xfb\xe7\xfc>{\xcf\xfc\xbf;\x9f\xfc\x9f\x99?\xfc\xcf\xcc\x7f\xfc\xe7\xe7\xc1\xfc\xf3\xf7\x9c|\xf8\xe7\xbf<\xfe\x0f\x9f\x9c\xff\xf9\xcf\xcc\xff\xf0\xe7\xe4\xff\xe0s\xf4\xff\xc0y\xf0\xff\x80|\xf8\xff\x00~x\xfe\x00\xff8\xfc\x00\xff\x90\xf8\x01\xff\xc4\xf0\x03\xff\xfc\xf0\x07\xff\xfc\xf0\x1f\xff\xfc\xf8\x7f\xff\xfc

Post Reply