How can I locally save an image using its URL address?

robin_12 commented: Files to be saved cannot contain :, /, or, You will have to use re to sub these characters to all " . " Dot. +0
billie01579 commented: great this is helpful. +0

No sir mr. rproffitt I am not promoting the site. I am just sharing what I have read, and understood from. I am also a learner, so there is no intention of promoting, just giving the information related to the topic.

commented: No, you are part of bait and reply spam operation -4
#File names cannot be saved 
# with :, ", ', /, \, , or //.
# use regular expressions to filter them out 
#using re.sub() methods.

urlf = re.sub('http.*?//', 'http.', url)
urlf = re.sub('\/\/', '.', urlf)# \escapes//
urlf = re.sub('\/', '.', urlf)# \escapes/
urlf = re.sub('\s', '', urlf)# clean up whitespace.
mas = 'img.'+urlf
--save mas img here--
#output:What you want it to be.
img.http.www.sitename.com.path.to.imagename.jpeg

Here is another example I wrote a while ago:

''' urlretrieve_image1.py
retrieve and save an image from a web page
using ...
urlretrieve(url[, filename[, reporthook[, data]]]) 
tested with Python27 and Python36  by  vegaseat
'''

try:
    # Python2
    from urllib import urlretrieve
except ImportError:
    # Python3
    from urllib.request import urlretrieve

# find yourself an image on an internet web page you like
# (MSW: right click on the image, look under properties and copy the address)
image_url = "http://www.google.com/intl/en/images/logo.gif"

# extract the filename from the url
url_list = image_url.split('/')
print(url_list)  # test
image_filename = url_list[-1]

# retrieve the image from the url and save it to a file in the working directory
urlretrieve(image_url, image_filename)

print("image saved as %s"  % image_filename)

# try to show the saved image file ...
import webbrowser
webbrowser.open(image_filename)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.