How to Save an Image Locally Using Python Whose Url Address I Already Know

How to save an image locally using Python whose URL address I already know?

Python 2

Here is a more straightforward way if all you want to do is save it as a file:

import urllib

urllib.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

The second argument is the local path where the file should be saved.

Python 3

As SergO suggested the code below should work with Python 3.

import urllib.request

urllib.request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

python save image from url

A sample code that works for me on Windows:

import requests

with open('pic1.jpg', 'wb') as handle:
response = requests.get(pic_url, stream=True)

if not response.ok:
print(response)

for block in response.iter_content(1024):
if not block:
break

handle.write(block)

How do I read image data from a URL?

The following works for Python 3:

from PIL import Image
import requests

im = Image.open(requests.get(url, stream=True).raw)

References:

  • https://github.com/python-pillow/Pillow/pull/1151
  • https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#280-2015-04-01

How to save or download an image that I get in a request -- Python

Try this:

import requests

Picture_request = requests.get(Photo_URL)
if Picture_request.status_code == 200:
with open("/path/to/image.jpg", 'wb') as f:
f.write(Picture_request.content)


Related Topics



Leave a reply



Submit