-2

I want to download the picture using the image url using python. How can I download it? I can try all the order which is available on google but no one code is not working on this type of url.

I have tried this code:

import requests # to get image from the web
import shutil # to save it locally

## Set up the image URL and filename
image_url = "https://graph.facebook.com/264520706917918/picture"
filename = image_url.split("/")[-1]

# Open the url image, set stream to True, this will return the stream content.
r = requests.get(image_url, stream = True)

# Check if the image was retrieved successfully
if r.status_code == 200:
    # Set decode_content value to True, otherwise the downloaded image file's size will be zero.
    r.raw.decode_content = True

    # Open a local file with wb ( write binary ) permission.
    with open(filename,'wb') as f:
        shutil.copyfileobj(r.raw, f)

    print('Image sucessfully Downloaded: ',filename)
else:
    print('Image Couldn\'t be retreived')

and also these code:

# import wget

# # Set up the image URL
# image_url = "https://graph.facebook.com/264520706917918/picture"

# # Use wget download method to download specified image url.
# image_filename = wget.download(image_url)

# print('Image Successfully Downloaded: ', image_filename)
martineau
  • 119,623
  • 25
  • 170
  • 301
Noob Gamer
  • 45
  • 1
  • 1
  • 6
  • Please include the code that you have tried. – mbtamuli Nov 16 '20 at 21:01
  • 2
    Might be a strange question, but have you tried googling? I found [this](https://stackoverflow.com/questions/30229231/python-save-image-from-url/30229298), [this](https://stackoverflow.com/questions/3042757/downloading-a-picture-via-urllib-and-python), [this](https://stackoverflow.com/questions/8286352/how-to-save-an-image-locally-using-python-whose-url-address-i-already-know), [this](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests) and I can keep going – ForceBru Nov 16 '20 at 21:02
  • I have tried this but not working its just download the image with unknown format which i am not able to open it – Noob Gamer Nov 16 '20 at 21:33

1 Answers1

0

try change

r = requests.get(image_url, stream = True)

too

r = requests.get(image_url, stream = True).raw
coderoftheday
  • 1,987
  • 4
  • 7
  • 21