Python 从 url 保存图像

当我使用 python 通过 urllib2请求或 urllib.url 检索从 url 保存图像时,我遇到了一个问题。即图像的 URL 是有效的。我可以用浏览器手动下载。但是,当我使用 python 下载图像时,无法打开该文件。我使用 Mac OS 预览来查看图像。谢谢!

更新:

密码如下

def downloadImage(self):
request = urllib2.Request(self.url)
pic = urllib2.urlopen(request)
print "downloading: " + self.url
print self.fileName
filePath = localSaveRoot + self.catalog  + self.fileName + Picture.postfix
# urllib.urlretrieve(self.url, filePath)
with open(filePath, 'wb') as localFile:
localFile.write(pic.read())

我要下载的图像 URL 是 Http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg

这个 URL 是有效的,我可以通过浏览器保存它,但是 python 代码会下载一个无法打开的文件。Preview 说“它可能被损坏或者使用了 Preview 不能识别的文件格式。” 我比较了 Python 下载的图像和通过浏览器手动下载的图像。前者的大小比后者小几个字节。因此,这个文件似乎是未完成的,但我不知道为什么 python 不能完全下载它。

234260 次浏览

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


img_data = requests.get(image_url).content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)

Python code snippet to download a file from an url and save with its name

import requests


url = 'http://google.com/favicon.ico'
filename = url.split('/')[-1]
r = requests.get(url, allow_redirects=True)
open(filename, 'wb').write(r.content)
import random
import urllib.request


def download_image(url):
name = random.randrange(1,100)
fullname = str(name)+".jpg"
urllib.request.urlretrieve(url,fullname)
download_image("http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg")

For linux in case; you can use wget command

import os
url1 = 'YOUR_URL_WHATEVER'
os.system('wget {}'.format(url1))

Anyone who is wondering how to get the image extension then you can try split method of string on image url:

str_arr = str(img_url).split('.')
img_ext = '.' + str_arr[3] #www.bigbasket.com/patanjali-atta.jpg (jpg is after 3rd dot so)
img_data = requests.get(img_url).content
with open(img_name + img_ext, 'wb') as handler:
handler.write(img_data)

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.request
imgURL = "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.

download and save image to directory

import requests


headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9"
}


img_data = requests.get(url=image_url, headers=headers).content
with open(create_dir() + "/" + 'image_name' + '.png', 'wb') as handler:
handler.write(img_data)

for creating directory

def create_dir():
# Directory
dir_ = "CountryFlags"
# Parent Directory path
parent_dir = os.path.dirname(os.path.realpath(__file__))
# Path
path = os.path.join(parent_dir, dir_)
os.mkdir(path)
return path

You can pick any arbitrary image from Google Images, copy the url, and use the following approach to download the image. Note that the extension isn't always included in the url, as some of the other answers seem to assume. You can automatically detect the correct extension using imghdr, which is included with Python 3.9.

import requests, imghdr


gif_url = 'https://media.tenor.com/images/eff22afc2220e9df92a7aa2f53948f9f/tenor.gif'
img_url = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQwXRq7zbWry0MyqWq1Rbq12g_oL-uOoxo4Yw&usqp=CAU'
for url, save_basename in [
(gif_url, 'gif_download_test'),
(img_url, 'img_download_test')
]:
response = requests.get(url)
if response.status_code != 200:
raise URLError
extension = imghdr.what(file=None, h=response.content)
save_path = f"{save_basename}.{extension}"
with open(save_path, 'wb') as f:
f.write(response.content)

if you want to stick to 2 lines? :

with open(os.path.join(dir_path, url[0]), 'wb') as f:
f.write(requests.get(new_url).content)