[SOLVED] PBM file showing /r

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Redostrike
Posts: 6
Joined: Mon Feb 04, 2019 9:37 pm

[SOLVED] PBM file showing /r

Post by Redostrike » Tue Feb 12, 2019 7:25 am

Hey,

I've been using PBM files to load them on an oled (ssd1306). (reading into bytearray and framebuffer it to the screen). All my files i've been using i havent gotten any problems. Now i have a new file made (the same way i always made my pbm files) and (after some testing) i figured out that the /n (newlines) are actually /r (carriage returns). The part i use for loading the files is in an class like this:

Code: Select all

    with open(self.filename + '.pbm', 'rb') as f:
      f.readline()
      self.sizex = int(f.readline().decode('utf8'))
      self.sizey = int(f.readline().decode('utf8'))
      data = bytearray(f.read())
      self.image = framebuf.FrameBuffer(data, int(self.sizex), int(self.sizey), framebuf.MONO_HLSB)
I'm getting the x and y size of the file out of the file itself which again has been working. Now for testing purposes i made this:

Code: Select all

with open('left.pbm', 'rb') as f:
      var1 = f.readline()
      var2 = f.readline()
      var3 = f.readline()
      var4 = f.read()
      
      
print(var1)
print(var2)
print(var3)
print(var4)
Which results int this shown on console:

Code: Select all

b'P4\r7\r7\r\x000($(0\x00'
b''
b''
b''
When i try this on Cpython (same code & same file) i'm getting the desired results:

Code: Select all

b'P4\n'
b'7\n'
b'7\n'
b'\x000($(0\x00'
Only this file is having problems on Micropython other pbm files have been working fine. Anyone know how to resolve this?
Last edited by Redostrike on Tue Feb 12, 2019 6:36 pm, edited 1 time in total.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: PBM file showing /r

Post by Christian Walther » Tue Feb 12, 2019 11:58 am

https://docs.python.org/3/library/io.ht ... e.readline specifies that
The line terminator is always b'\n' for binary files
so I would say the MicroPython behavior is correct, and your CPython probably isn’t Python 3.

The easiest way to resolve this is to replace the CRs in your PBM file by LFs, but if you need to be able to read arbitrary PBMs, which can have arbitrary whitespace, not just CR and LF, then you can’t work with readline() and need to scan the bytes yourself, maybe with regular expressions or manually.

Redostrike
Posts: 6
Joined: Mon Feb 04, 2019 9:37 pm

Re: PBM file showing /r

Post by Redostrike » Tue Feb 12, 2019 12:12 pm

Seems odd to me that al the other files don't have this issue except for the one. cpython is version 3 so I would expect the same behavior in microphyton.

If I check the file out on notepad++ (and show all characters) they show up as an LF and not as CR which even more adds to this mystery. I've reworked the same file (filled it all black) an guess what. It worked. Then reworked it to what it was before and same deal not working.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: PBM file showing /r

Post by Christian Walther » Tue Feb 12, 2019 6:09 pm

How are you transferring the file to the board? Could it be an FTP connection in text mode or something else that mangles line endings when it thinks a file is text?

Redostrike
Posts: 6
Joined: Mon Feb 04, 2019 9:37 pm

Re: PBM file showing /r

Post by Redostrike » Tue Feb 12, 2019 6:21 pm

Using upycraft same as all the other files. Could try and install ampy and try with that.

EDIT: Quickly setup webrepl and sended the file true the webrepl interface. And it works.

Thank you.

Post Reply