python save image from url python save image from url python python

python save image from url


import requestsimg_data = requests.get(image_url).contentwith open('image_name.jpg', 'wb') as handler:    handler.write(img_data)


A sample code that works for me on Windows:

import requestswith 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)


It is the simplest way to download and save the image from internet using urlib.request package.

Here, you can simply pass the image URL(from where you want to download and save the image) and directory(where you want to save the download image locally, and give the image name with .jpg or .png) Here I given "local-filename.jpg" replace with this.

Python 3

import urllib.requestimgURL = "http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg"urllib.request.urlretrieve(imgURL, "D:/abc/image/local-filename.jpg")

You can download multiple images as well if you have all the image URLs from the internet. Just pass those image URLs in for loop, and the code automatically download the images from the internet.