I tried part of yoru code around and nailed the problem down:
Although the "tostring" parameter accepts a "mode" argument, as it is
documented, this mode cannot be different from teh actual current
image mode. (Or at least, it can' t be RGBA if the current image is in
RGB mode - I haven't actually tried with other combinations).
The solution for your code is therefore to convert the image to RGBA
in PIL before exporting it to a string: