I have an image like this loaded into a PIL.Image:
And now I want to turn it into a python string, and it should not be binary, how do I do this? Because when I tried to encode I get the following error:
My Code:
from PIL import Image
img = Image.open("testImage.jpeg")
string = img.tobytes()
string = string.decode("ascii")
Output:
Traceback (most recent call last):
File "/Users/tomschimansky/Desktop/SenderMAIN.py", line 5, in <module>
string = string.decode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
When this works I also want to turn the string back into an image.
Other methods that also don't worked for me:
- Read the image directly from file with
open("file","rb")and then encode it. - encode it with the
codecslibrary. (string = codecs.encode(string, "base64")) - encode it with the
base64library (was able to convert it to string but the string looked like this:///////. (only slashes))
Thanks for your Answers!
