Search code examples
python-3.xurllib

I downloaded image with urllib.request.urlretrieve and the image is downloaded but it does not open


I downloaded an image using python urllib library and the image is downloaded but when I try to open it it says "we dont support this extension" in default image software even though I downloaded image as png. I tried this on multiple url but the result is always the same.

import urllib.request

def download_image(url):

    full_name = "img3.png"
    urllib.request.urlretrieve(url, full_name)


download_image("https://en.wikipedia.org/wiki/File:Google.png")

Solution

  • It is not working because you are not pointing to an image. You are trying to download an html page as a png. Change the url to a png and it will work.

    import urllib.request
    
    def download_image(url):
    
        full_name = "img3.png"
        urllib.request.urlretrieve(url, full_name)
    
    
    download_image("https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Google.png/800px-Google.png")