Python 3 - Upload PIL Binary to FTP server
Question: im new to python and i try to upload a image to FTP in python 3.

I'm taking the pic with the raspberry camera modul and write it to a binary.
After this i do some actions with PIL. This part works already fine. I also
can store it to a JPEG file. I wanna skip the file storage and upload the
image directly from memory. I found the following code
[here](http://stackoverflow.com/questions/15715940/pil-image-save-to-an-ftp-
server):

    
    
    import StringIO
    f = StringIO()
    image.save(f, 'PNG')
    
    f.seek(0) # return the StringIO's file pointer to the beginning of the file
    
    # again this assumes you already connected and logged in
    ftp.storbinary("STOR remote_filename.png", f)
    

Based on this i created my code (only the important lines):

    
    
    stream = io.BytesIO()
    camera.capture(stream, format='jpeg')  # take pic, streaming method
    stream.seek(0) # "Rewind" the stream to the beginning so we can read its content
    img_web = Image.open(stream)
    
    size=[web_width, web_height] # some PIL editing
    img_web=img_web.resize(size) # resize
    
    session = ftplib.FTP(ftp_server, ftp_user, ftp_pwd)  # ftp upload
    img_web.seek(0)
    temp = io.StringIO() # this is a file object
    img_web.save(temp, format='JPEG') # save the content to temp
    temp.seek(0)
    session.storbinary('STOR /home/www/test.jpg' , temp)
    

But this does not work and i get the following error:

    
    
     img_web.save(temp, format='JPEG') # save the content to temp
     File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1685, in save
       save_handler(self, fp, filename)
     File "/usr/lib/python3/dist-packages/PIL/PngImagePlugin.py", line 657, in _save
       fp.write(_MAGIC)
    TypeError: string argument expected, got 'bytes'
    

How can i solve this problem? Thanks.


Answer: I just played a bit with the code and i found the solution. It's so simple.
Instead of

    
    
    temp = io.StringIO()
    

i only had to use:

    
    
    temp = io.BytesIO()
    

