I have a list of RGB data:
cdata=[R1, G1, B1, R2, G2, B2,..., Rn, Gn, Bn]
where each value is comprised between 0 to 255.
I am trying to rebuild this array as an image using Pillow 5.0.0. Under Python 2, I was able to convert the list of values into a byte string this way:
cdata2 = []
gidx = len(cdata)//3
bidx = len(cdata)//3*2
for i in range(len(cdata)//3):
cdata2.append(cdata[i])
cdata2.append(cdata[i+gidx])
cdata2.append(cdata[i+bidx])
data = ""
for c in cdata2:
data += chr(c)
im = Image.frombytes("RGB", (420, 560), data)
and then reincode 'im' in base64 and display it as a PNG in an HTML template.
Unfortunately this does not work in Python 3, I am having errors like:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 42099-42101: character maps to <undefined>
Furthermore, Pillow 5 documentation now suggests using
im = Image.open(StringIO(data))
but cannot make it work with my string built above. Is there any more clever way to do this? Thanks a lot in advance for your help.