python从url保存图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30229231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python save image from url
提问by Shaoxiang Su
I got a problem when I am using python to save an image from url either by urllib2 request or urllib.urlretrieve. That is the url of the image is valid. I could download it manually using the explorer. However, when I use python to download the image, the file cannot be opened. I use Mac OS preview to view the image. Thank you!
当我使用 python 通过 urllib2 请求或 urllib.urlretrieve 从 url 保存图像时遇到问题。那就是图片的网址是有效的。我可以使用资源管理器手动下载它。但是,当我使用python下载图像时,文件无法打开。我使用 Mac OS 预览来查看图像。谢谢!
UPDATE:
更新:
The code is as follow
代码如下
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())
The image URL that I want to download is http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg
我要下载的图片网址是 http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg
This URL is valid and I can save it through the browser but the python code would download a file that cannot be opened. The Preview says "It may be damaged or use a file format that Preview doesn't recognize." I compare the image that I download by Python and the one that I download manually through the browser. The size of the former one is several byte smaller. So it seems that the file is uncompleted, but I don't know why python cannot completely download it.
这个 URL 是有效的,我可以通过浏览器保存它,但是 python 代码会下载一个无法打开的文件。预览显示“它可能已损坏或使用了预览无法识别的文件格式。” 我比较了我通过 Python 下载的图像和我通过浏览器手动下载的图像。前一个的大小要小几个字节。所以看起来文件未完成,但不知道为什么python无法完全下载它。
采纳答案by DeepSpace
A sample code that works for me on Windows:
在 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)
回答by Vlad Bezden
import requests
img_data = requests.get(image_url).content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)
回答by Basil Jose
Python code snippet to download a file from an url and save with its name
用于从 url 下载文件并使用其名称保存的 Python 代码片段
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)
回答by learner
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")
回答by Vicrobot
For linux in case; you can use wget command
对于 linux,以防万一;你可以使用 wget 命令
import os
url1 = 'YOUR_URL_WHATEVER'
os.system('wget {}'.format(url1))
回答by Charoun crz
import urllib.request
import os
img_url = "https://betanews.com/wp-content/uploads/2017/09/firefox-logo.jpg"
img_name = os.path.basename(img_url)
urllib.request.urlretrieve(img_url,img_name)
回答by Ssubrat Rrudra
Anyone who is wondering how to get the image extension then you can try split method of string on image url:
任何想知道如何获取图像扩展名的人都可以尝试在图像 url 上使用字符串的 split 方法:
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)
回答by Ankit Lad
It is the simplest way to download and save the image from internet using urlib.requestpackage.
这是使用urlib.request包从 Internet 下载和保存图像的最简单方法。
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.
在这里,您可以简单地传递图像 URL(从您要下载和保存图像的位置)和目录(您要在本地保存下载图像的位置,并使用 .jpg 或 .png 指定图像名称)这里我给出了“本地文件名.jpg”替换为这个。
Python 3
蟒蛇 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.
如果您拥有来自 Internet 的所有图像 URL,您也可以下载多个图像。只需在 for 循环中传递这些图像 URL,代码就会自动从 Internet 下载图像。