peterhinch/micropython_data_to_py

TypeError: can't convert 'memoryview' object to str implicitly

mytechnotalent opened this issue · 14 comments

When trying to convert a .jpg image to .py I am getting a TypeError: can't convert 'memoryview' object to str implicitly.

import machine
import gc9a01
import vga2_bold_16x32 as font
import dc540_logo

spi = machine.SPI(0, baudrate=40000000, polarity=0, sck=machine.Pin(6), mosi=machine.Pin(7))
display = gc9a01.GC9A01(spi, 240, 240, backlight=machine.Pin(2, machine.Pin.OUT), reset=machine.Pin(14, machine.Pin.OUT), dc=machine.Pin(15, machine.Pin.OUT))
display.init()

dc540_logo_data = dc540_logo.data()
display.jpg(dc540_logo_data, 0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert 'memoryview' object to str implicitly

dc540_logo_data is a memoryview. I would expect display.jpg to expect a bytes object as its first arg. So I would try:

display.jpg(bytes(dc540_logo_data), 0, 0)

Thanks @peterhinch appreciate all your contributions and being awesome! Just worked!

@peterhinch for whatever reason when I tried again

>>> dc540_logo_data = dc540_logo.data()
>>> display.jpg(bytes(dc540_logo_data), 0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeError: 

dc540_logo.py (truncated)

# Code generated by data_to_py.py.
version = '0.1'

_data =\
b'\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x00\x00\x48'\
b'\x00\x48\x00\x00\xff\xe1\x00\x98\x45\x78\x69\x66\x00\x00\x4d\x4d'\
...
_mvdata = memoryview(_data)

def data():
    return _mvdata

main.py

import machine
import gc9a01
import vga2_bold_16x32 as font
import dc540_logo

spi = machine.SPI(0, baudrate=40000000, polarity=0, sck=machine.Pin(6), mosi=machine.Pin(7))
display = gc9a01.GC9A01(spi, 240, 240, backlight=machine.Pin(2, machine.Pin.OUT), reset=machine.Pin(14, machine.Pin.OUT), dc=machine.Pin(15, machine.Pin.OUT))
display.init()

dc540_logo_data = dc540_logo.data()
display.jpg(bytes(dc540_logo_data), 0, 0)
>>> display.jpg(bytes(dc540_logo_data), 0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeError: ```

dc540_logo.jpg image
https://ibb.co/DVJy0Fw

The image you linked is not visible here.

What happens if you just issue

bytes(dc540_logo_data)

@peterhinch top part is truncated but here is the result

b\xb2\x9d\xd2H\x9ek;\x89m\xa5h\xa5\x05e\x89\xa4\x81\xe3s\x1c\x8aq$d\xecq\xc1S\xfc%\xff\x00\xad?O\xeb\xb8\x1d\x82|L\xf1\xbac~\xb5\xf6\xa3\xe4\xad\xbe\xfdCL\xd259\x0c)\x9d\xa8\xd2\xea\x16\x17267\x1f\x99\x9d\x9b\x18\x05\xc8Q\xb9r\xae\xdf\xd6\xff\x00\xd7\xdc\x06\r\xef\x89\xb5\xdb\xf9\xfe\xd36\xa0\xf1L\x10G\x9b\x08\xad\xf4\xb4\xd8\x06\x001i\x90\xd9\xc4x\xeeP\x93\xdf4r\xaf\xf8\x1d>\xed\x80l~&\xf1\x1c;DZ\xfe\xb5\x18_\xba\x13T\xbdP?\x0f8\x8e;q\xc7\xb7\x14r\xc7\xb2\xfb\xbf\xccV\x8fe\xf7\x0e\x7f\x14\xf8\x9aB\x0b\xf8\x87Zb\xa3\x0b\x9dN\xf7\xe5\x1d0?|0\x08\xec\x0e>\xbdhq\x8b\xb6\x8bO%\xfd?\x9e\xdf4\x16]\x97\xdd\xfd\\\xa5\xfd\xaf\xa8\x96\r%\xdc\x93\xb0b\xe0\xdd\x04\xbb\xc3\x11\x82\xc3\xed+(\x0cFy\xce}\xf9\x14(\xc5l\x92\xeb\xa2\xff\x00-\x83\x95v]\xfd/\xe9\xaa\xbf\xe7\xab:\xab\x7f\x89~3\xb4\xb7\x16\xd6\xba\xa5\xbd\xb4J\xa8\x83\xec\xfa&\x83\x04\xa0F\xc1\x93\xf7\xf1\xe9b\xe3p*2\xc6]\xc4p\xce\xc1\x98Qe\xd9}\xc3i4\x93I\xa5\xa2\xba\xbf\xcb\xfe\x1f\xfc\xcef\xff\x00]\xd6\xb5VF\xd4\xf5]CP1\xb34B\xf2\xee{\x94\x88\xb7\xde0\xa4\xac\xe9\x0e}#U\xf6\xc0;X\xb2[%\xf7\x15\x19J\t\xa8\xb6\x93\xdf\xaf\xcb_\xd2\xdf\x82R\xcd2;rX\x92z\x9e\xe7>\xa7\xbf\xe3\x9f\xc3\x18\xa6H\xca\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x03\xff\xd4\xff\x00?\xfa\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x03\xff\xd9'
>>> bytes(dc540_logo_data)

This result shows that the bytes(dc540_logo_data) call is returning a bytes object without error. This is what I would expect: it is producing the JPEG image s a stream of bytes.

To prove the point you could start with the original image:

with open("my_image.jpg", "rb") as f:
    b = f.read()  # Image as an array of bytes
    display.jpg(b, 0, 0)

If that fails the problem is with that C library. Either you are calling it incorrectly or it has a bug which you will need to take up with its author.

@peterhinch

Same behavior.

with open("dc540_logo.jpg", "rb") as f:
    b = f.read()  # Image as an array of bytes
    display.jpg(b, 0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "main.py", line 109, in <module>
UnicodeError: 
>>> 

I will continue to work with it on my end and will close out as this is not in scope for this repo.