0

url="https://images.data.gov.sg/api/traffic-images/2016/02/96128cfd-ab9a-4959-972e-a5e74bb149a9.jpg"

I am trying this: import urllib
url="https://images.data.gov.sg/api/traffic-images/2016/02/96128cfd-ab9a-4959-972e-a5e74bb149a9.jpg"
IMAGE=url.rsplit("/")[-1]
urllib.urlretrieve(url,IMAGE)

Image is downloaded in the destination folder after the execution, but it is corrupt. "Could not load image"; error pops up.

  • This is probably, because the url is _not_ an image, it causes (in a browser) a new request which finally gets the image. I'm not sure if `urllib` automatically follows redirects. Have a look at http://python-requests.org - I've found this library better documented and easier to use. – jcoppens Jul 16 '16 at 14:26
  • Thanks! The requests module worked – sumit singh Jul 18 '16 at 06:24

2 Answers2

0

It might be because the domain that you are trying to reach has restrictions over download policy. Check this one out, hope it helps! https://stackoverflow.com/a/8389368/2539771

Community
  • 1
  • 1
Serbay
  • 11
  • 4
0

import urllib URL = "https://images-na.ssl-images-amazon.com/images/I/714tx9QbaKL.SL1500.jpg" urllib.urlretrieve(URL, "sample.png")

from PIL import Image
img = Image.open('/home/sks/sample.png') img.show()

Dhawaleswar
  • 99
  • 1
  • 5