PiicoDev Library .pbm files error

There are free online tools available to convert your own images to .pbm - it’s best if your image size is already 128x64 pixels.

NOP. Every one I have tried causes the code to crash at line 249.

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "PiicoDev_SSD1306.py", line 249, in load_pbm
IndexError: bytearray index out of range

The PiicoDev Logo is 1080 bytes, all the converted files are 1034 bytes.

Please advise which converter will work.
Cheers
Jim

Edit: The image is 128 x 64 pixels, my image editor can save as .pbm and the size is 1034.
To me it looks like an error with the library.
Has anyone else had problems, if not can you advise what you did.

3 Likes

I found the problem. There are 3 readlines for the first few bytes of the tile.
The PiicoDev logo was created with GIMP which has itself identified in one of the lines.
Commenting out one readline it now works for files not created by GIMP.
It is the difference between a file size of 1080 and 1034 bytes.

Funny.

    def load_pbm(self, filename, c):
        with open(filename, 'rb') as f:
#            f.readline()
            f.readline()
            f.readline()
            data_piicodev = bytearray(f.read())
        for byte in range(WIDTH // 8 * HEIGHT):
            for bit in range(8):
                if data_piicodev[byte] & 1 << bit != 0:
                    x_coordinate = ((8-bit) + (byte * 8)) % WIDTH
                    y_coordinate = byte * 8 // WIDTH
                    if x_coordinate < WIDTH and y_coordinate < HEIGHT:
                        self.pixel(x_coordinate, y_coordinate, c)

Regards
Jim

PiicoDev Logo

P4
# Created by GIMP version 2.10.28 PNM plug-in
128 64

Non GIMP editor

P4
128 64
4 Likes

I changed the PiicoDev OLED SSD1306 Display library to check the length of the second line and read a third line if it is greater than 15. (see below) Seemed to be the easiest way, could have checked for characters in the line, but …

This will allow for bitmaps with larger displays than 128x64, but it is hardly an issue in this case.

Both GIMP files and other .pbm files now display correctly.
Regards
Jim

    def load_pbm(self, filename, c):
        with open(filename, 'rb') as f:
            f.readline()
            line = f.readline()
            if len(line) > 15:
                f.readline()
            data_piicodev = bytearray(f.read())
        for byte in range(WIDTH // 8 * HEIGHT):
            for bit in range(8):
                if data_piicodev[byte] & 1 << bit != 0:
                    x_coordinate = ((8-bit) + (byte * 8)) % WIDTH
                    y_coordinate = byte * 8 // WIDTH
                    if x_coordinate < WIDTH and y_coordinate < HEIGHT:
                        self.pixel(x_coordinate, y_coordinate, c)
5 Likes

Hi Jim,

Thanks for posting and congratulations on finding the cause and developing a work around.

We will update the main repository to properly handle commenting in the file header.

Cheers,

Peter

3 Likes

Hi Jim,

The issue with reading PBM files has been patched and is now available on GitHub.
GitHub - CoreElectronics/CE-PiicoDev-SSD1306-MicroPython-Module: CE-PiicoDev-SSD1306-MicroPython-Module

[Update] The Python package is now updated and can be updated through Thonny (PiicoDev v1.0.1)

Cheers,

Peter

4 Likes