[英]python save image from url
当我使用 python 通过 urllib2 请求或 urllib.urlretrieve 从 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())
我要下载的图片网址是http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg
这个 URL 是有效的,我可以通过浏览器保存它,但是 python 代码会下载一个无法打开的文件。 预览显示“它可能已损坏或使用了预览无法识别的文件格式。” 我比较了通过 Python 下载的图像和通过浏览器手动下载的图像。 前者的大小要小几个字节。 所以看起来文件是未完成的,但我不知道为什么python不能完全下载它。
import requests
img_data = requests.get(image_url).content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)
在 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)
这是使用urlib.request包从 Internet 下载和保存图像的最简单方法。
在这里,您可以简单地传递图像 URL(从您要下载并保存图像的位置)和目录(您要在本地保存下载图像的位置,并以 .jpg 或 .png 给出图像名称)这里我给出了“ local-filename.jpg" 替换为这个。
蟒蛇 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")
如果您拥有来自 Internet 的所有图像 URL,您也可以下载多个图像。 只需在 for 循环中传递这些图像 URL,代码就会自动从 Internet 下载图像。
用于从 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)
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")
我在使用python通过urllib2请求或urllib.urlretrieve从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
这个网址是有效的,我可以通过浏览器保存它,但是python代码会下载无法打开的文件。 预览显示“它可能已损坏或使用了预览无法识别的文件格式。” 我比较了我通过Python下载的图像和通过浏览器手动下载的图像。 前者的大小要小几个字节。 因此,似乎文件未完成,但是我不知道为什么python无法完全下载它。
我在使用python通过urllib2请求或urllib.urlretrieve从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
这个网址是有效的,我可以通过浏览器保存它,但是python代码会下载无法打开的文件。 预览显示“它可能已损坏或使用了预览无法识别的文件格式。” 我比较了我通过Python下载的图像和通过浏览器手动下载的图像。 前者的大小要小几个字节。 因此,似乎文件未完成,但是我不知道为什么python无法完全下载它。
任何想知道如何获取图像扩展名的人都可以尝试在图像 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)
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)
用于创建目录
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
对于 linux 以防万一; 你可以使用 wget 命令
import os
url1 = 'YOUR_URL_WHATEVER'
os.system('wget {}'.format(url1))
您可以从 Google 图片中选择任意图片,复制 url,然后使用以下方法下载图片。 请注意,扩展名并不总是包含在 url 中,正如其他一些答案似乎假设的那样。 您可以使用 Python 3.9 中包含的 imghdr 自动检测正确的扩展名。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.