The task is this: I have a BMP image with a size of 896x116 . I open it and ask these variables for two variables. Also, I need to divide 896 by 0x3 and 0x80 , which at the end is sent as 0x380 = 896 ; also with 116
path = "logo.bmp" img = Image.open(path, "r") img.load() width = img.size[0] # получаем 896 height = img.size[1] # получаем 116 def bytes(num): # с помощью этого получаем 0x3 и 0x80 return hex(num >> 8), hex(num & 0xFF) Next, I want to do the following:
address = [0x80, 0x03, 0x94] value = bytes(width)[1] # 0x3 value2 = bytes(width)[0] # 0x80 send = (address + value) And I get an error
TypeError: can only concatenate list (not "str“) to list на send = (address + value) How to get rid of this error?
struct.pack('!H', 896)or if the input number is not fixed in size, thenint2bytes(896)- jfsbytes- this is the name of the Python built-in type. To translate an integer into a set of bytes, instead of a bicycle, it is better to use theint.to_bytes()method, and the result is a list. In your case, it will look something like this:values = list(width.to_bytes(2, byteorder='little'))(“little” means the order of bytes from the youngest to the oldest (from 0x380 you get[0x80, 0x03])) need the opposite, then write "big"). Then just take thevalues[0]andvalues[1]and use as you need. - insolorb"\x03"[0]returns a string, not an integer in Python 2. Do you want to send the 2nd low byte ofwidth?b = (width >> 8) &0xFF; spi.xfer2(address + [b])b = (width >> 8) &0xFF; spi.xfer2(address + [b]). It is probably more convenient withbytearray()to work to create data to send ifspi.xfer2(bytearray(b"\x80\x03\x94\x03"))works. - jfsintinto a list of bytes, you can use this option:value = [ord(item) for item in struct.pack('>i', width)]- insolor