Python urllib2 Images Distorted
Question: I'm making a program using the website <http://placekitten.com>, but I've run
into a bit of a problem. Using this:

    
    
    im = urllib2.urlopen(url).read()
    f = open('kitten.jpeg', 'w')
    f.write(im)
    f.close()
    

The image turns out distorted with mismatched colors, like this:

<http://imgur.com/zVg64Kn.jpeg>

I was wondering if there was an alternative to extracting images with urllib2.
If anyone could help, that would be great!


Answer: You need to open the file in binary mode:

    
    
    f = open('kitten.jpeg', 'wb')
    

Python will otherwise translate line endings to the native platform form, a
transformation that breaks binary data, as documented for the [`open()`
function](http://docs.python.org/2/library/functions.html#open):

> The default is to use text mode, which may convert `'\n'` characters to a
> platform-specific representation on writing and back on reading. Thus, when
> opening a binary file, you should append `'b'` to the mode value to open the
> file in binary mode, which will improve portability.

When copying data from a URL to a file, you could use
[`shutil.copyfileob()`](http://docs.python.org/2/library/shutil.html#shutil.copyfileobj)
to handle streaming efficiently:

    
    
    from shutil import copyfileobj
    
    im = urllib2.urlopen(url)
    with open('kitten.jpeg', 'wb') as out:
        copyfileobj(im, out)
    

This will read data in chunks, avoiding filling memory with large blobs of
binary data. The `with` statement handles closing the file object for you.

