How to download image through python using image link


Python provides various method to download image but it works as per your python version. but before downloading image it is best to download image in specific folder. this will be more organized way to allocate resource.

 

 

for example

For python v3

  import urllib.request 
  import os
  prodimg="http://someimg.com/somg-img.png"
   
   sku="img1"
   directory="smartphone\\"+sku
   
   if not os.path.exists(directory):
            os.makedirs(directory)               
            urllib.request.urlretrieve(prodimg, directory+'\\'+sku+'.jpg')
      

 

 

For python v2

import urllib
import os

directory="smartphone"
prodimg="img1"
if not os.path.exists(directory):
            os.makedirs(directory)               
        
f = open(directory+'\\'+prodimg+'.jpg','wb')
f.write(urllib.urlopen('http://www.gunnerkrigg.com//comics/00000001.jpg').read())
f.close()