How to get string data from a python PIL image object?
Question: I'm trying to send the data of a gif file of a desktop through a socket to a
remote desktop (for desktop sharing) but i can't get the string for the data
using PIL, i don't know how to convert the Pil objects to string, here is my
code (btw i know i can just write to a file then read the data like that but i
think that is inefficient and i think that there is a better way maybe?).

    
    
        from PIL import ImageGrab
        import cStringIO
        fakie = cStringIO.StringIO()
        ImageGrab.grab().save(fakie, 'GIF')
        data = fakie.getvalue()
        fakie.close()
        # This last bit of code is to see if the var data stored the right info in a str bc i need to send it through a socket
        with open('C:\something\something\Desktop\image.gif', 'w') as f:
            f.write(data)
    

The problem is that after the file is written the gif picture only displays
the top 1/10 of the page (the gif file is messed up), and so i'm wondering if
the problem lies within my computer or my code (i'm using vista on a VERY old
computer, at least 6 years i think and i'm getting a new one soon). Any input
is appreciated.


Answer: As @HYRY puts it, you must open the image file with "wb" mode instead of "w" -
Without the "b" Python defaults to open it in text mode - in windows it means
that whenever a 0x0a byte is written to the file, the O.S. writes a 0x0d 0x0a
sequence instead, because it translates line ending sequences to Windows
native line endings.

In the "wb" mode, there is no translation, and your image file won't be
corrupted.

