UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

12,450

Solution 1

The file should be opened in binary mode and then you can copy the stream byte for byte. Since shutil already has a handy helper utility, you can

import shutil
import os
from urllib import request

img = request.urlopen('http://www.py4inf.com/cover.jpg')
with open('cover.jpg', 'wb') as fhand:
    shutil.copyfileobj(img, fhand)
print(os.stat('cover.jpg').st_size, 'characters copied')

Solution 2

Don't use Unicode transformations for JPG images.

Unicode is for text. What you are downloading is not text, it is something else.

Try this:

from urllib import request
img = request.urlopen('http://www.py4inf.com/cover.jpg')
fhand = open('cover.jpg', 'wb')
size = 0
while True:
    info = img.read(100000)
    if len(info) < 1 : break
    size = size + len(info)
    fhand.write(info)

print (size,'characters copied.')

Or, more simply:

from urllib import request
request.urlretrieve('http://www.py4inf.com/cover.jpg', 'cover.jpg')
Share:
12,450
Sharath Manchala
Author by

Sharath Manchala

Updated on June 07, 2022

Comments

  • Sharath Manchala
    Sharath Manchala almost 2 years

    I am trying to scrap a picture from the link and put it into a image file. The request response is returning a byte stream. So I am using decode('utf-8') to convert to unicode stream however, I am facing the following error:

    print (info.decode(('utf-8')))

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

        from urllib import request
        img = request.urlopen('http://www.py4inf.com/cover.jpg')
        fhand = open('cover.jpg', 'w')
        size = 0
        while True:
            info = img.read(100000)
            if len(info) < 1 : break
            size = size + len(info)
            print (info.decode(('utf-8')))
            fhand.write(info.decode(('utf-8')))
    
        print (size,'characters copied.')
        fhand.close()
    

    Please let me know how can I proceed. Thanks.