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
How to Switch to the Active Tab in Selenium
Can Python Pickle Lambda Functions
What Does a for Loop Within a List Do in Python
How to Redirect Stdout and Stderr to Logger in Python
Python: Simple List Merging Based on Intersections
Python - Is a Dictionary Slow to Find Frequency of Each Character
Coalesce Values from 2 Columns into a Single Column in a Pandas Dataframe
Cleanest Way to Get Last Item from Python Iterator
How to Create a Custom Activation Function with Keras
How to Escape Special Characters of a String with Single Backslashes
How to Draw Intersecting Planes
How Did Python Implement the Built-In Function Pow()
Run Command and Get Its Stdout, Stderr Separately in Near Real Time Like in a Terminal
Keyerror: 'Tcl_Library' When I Use Cx_Freeze
Find the Recaptcha Element and Click on It -- Python + Selenium