I used shutil to download an image from an URL and it works fine but it downloads the images on the project directory. Is it possible to specify a directory so that the program saves it there?
I searched for a solution on StackOverflow but only found a solution using urllib urlretrieve which does not really relate to what I am trying to do. Thank you
import requests
import shutil
import os
import urllib.request
url = "https://www.lamborghini.com/sites/it-en/files/DAM/lamborghini/facelift_2019/homepage/families-gallery/2022/04_12/family_chooser_tecnica_m.png"
file_name = input("save image as :")
file_name += ".jpg"
res = requests.get(url, stream = True)
if res.ok:
with open(file_name,"wb") as f:
shutil.copyfileobj(res.raw,f)
print("image succesfully downloaded ", file_name)
else:
print("Download failed: status code {}\n{}".format(res.status_code, res.text))
or using urllib
f = open("sadasd.png", "wb")
f.write(urllib.request.urlopen(r"https://www.lamborghini.com/sites/it-en/files/DAM/lamborghini/facelift_2019/homepage/families-gallery/2022/04_12/family_chooser_tecnica_m.png").read())
f.close()