Python Forum
save image from link. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: save image from link. (/thread-7721.html)



save image from link. - mr_byte31 - Jan-22-2018

Hi All,

I am trying to make a python script to download image from a link.
I always used this code:

import urllib.request
myImgLink = "http://www.digimouth.com/news/media/2011/09/google-logo.jpg"
urllib.request.urlretrieve(myImgLink , "local-filename.jpg")
now I try to download an image with a link this : "https://drive.google.com/viewerng/img?id=ACFrOgBC9Til2_hbTKxft4aetjJViSBVtjug1ilDi5seFcDl4sQCpuPas7RmqxFc3iDu8m0tgOYw0Lw5cmxaoKaIbIWQPrp5xKISVMSfSD_t8mMh0yvnJFkhMggeOEs%3D&u=0&authuser=0&page=5&w=800"

as you can see the link doesn't have extension of an image i.e. it doesn't end with .png , .jpg , ...

my code now doesn't work for these kind of links.

any suggestions on how to fix that?


RE: save image from link. - Larz60+ - Jan-22-2018

I got a not found from the url you supplied.
I would use requests module to fetch image, something like:
(using different image)

import requests
from io import open as iopen

def fetch_image(img_ur, save_filename):
    img = requests.get(img_ur)
    if img.status_code == 200:
        with iopen(save_filename, 'wb') as f:
            f.write(img.content)
    else:
        print('Received error: {}'.format(img.status_code))

testlink = 'https://vignette.wikia.nocookie.net/pdsh/images/9/95/Prettygoldilocks.jpg'

filename = 'Goldilocks.jpg'
fetch_image(testlink, filename)



RE: save image from link. - mr_byte31 - Jan-22-2018

perfect, that works very well.
many thanks.