简体   繁体   English

python从url保存图像

[英]python save image from url

I got a problem when I am using python to save an image from url either by urllib2 request or urllib.urlretrieve.当我使用 python 通过 urllib2 请求或 urllib.urlretrieve 从 url 保存图像时遇到问题。 That is the url of the image is valid.那就是图片的url是有效的。 I could download it manually using the explorer.我可以使用资源管理器手动下载它。 However, when I use python to download the image, the file cannot be opened.但是,当我使用python下载图像时,无法打开文件。 I use Mac OS preview to view the image.我使用 Mac OS 预览来查看图像。 Thank you!谢谢!

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.这个 URL 是有效的,我可以通过浏览器保存它,但是 python 代码会下载一个无法打开的文件。 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.我比较了通过 Python 下载的图像和通过浏览器手动下载的图像。 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.所以看起来文件是未完成的,但我不知道为什么python不能完全下载它。

import requests

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

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)

It is the simplest way to download and save the image from internet using urlib.request package.这是使用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 给出图像名称)这里我给出了“ local-filename.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.如果您拥有来自 Internet 的所有图像 URL,您也可以下载多个图像。 Just pass those image URLs in for loop, and the code automatically download the images from the internet.只需在 for 循环中传递这些图像 URL,代码就会自动从 Internet 下载图像。

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)
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")

I got a problem when I am using python to save an image from url either by urllib2 request or urllib.urlretrieve.我在使用python通过urllib2请求或urllib.urlretrieve从url保存图像时遇到问题。 That is the url of the image is valid.那就是图像的URL是有效的。 I could download it manually using the explorer.我可以使用资源管理器手动下载它。 However, when I use python to download the image, the file cannot be opened.但是,当我使用python下载图像时,无法打开该文件。 I use Mac OS preview to view the image.我使用Mac OS预览来查看图像。 Thank you!谢谢!

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我要下载的图像URL是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.这个网址是有效的,我可以通过浏览器保存它,但是python代码会下载无法打开的文件。 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.我比较了我通过Python下载的图像和通过浏览器手动下载的图像。 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.因此,似乎文件未完成,但是我不知道为什么python无法完全下载它。

I got a problem when I am using python to save an image from url either by urllib2 request or urllib.urlretrieve.我在使用python通过urllib2请求或urllib.urlretrieve从url保存图像时遇到问题。 That is the url of the image is valid.那就是图像的URL是有效的。 I could download it manually using the explorer.我可以使用资源管理器手动下载它。 However, when I use python to download the image, the file cannot be opened.但是,当我使用python下载图像时,无法打开该文件。 I use Mac OS preview to view the image.我使用Mac OS预览来查看图像。 Thank you!谢谢!

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我要下载的图像URL是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.这个网址是有效的,我可以通过浏览器保存它,但是python代码会下载无法打开的文件。 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.我比较了我通过Python下载的图像和通过浏览器手动下载的图像。 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.因此,似乎文件未完成,但是我不知道为什么python无法完全下载它。

Anyone who is wondering how to get the image extension then you can try split method of string on image url:任何想知道如何获取图像扩展名的人都可以尝试在图像 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)

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

For linux in case;对于 linux 以防万一; you can use wget command你可以使用 wget 命令

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

You can pick any arbitrary image from Google Images, copy the url, and use the following approach to download the image.您可以从 Google 图片中选择任意图片,复制 url,然后使用以下方法下载图片。 Note that the extension isn't always included in the url, as some of the other answers seem to assume.请注意,扩展名并不总是包含在 url 中,正如其他一些答案似乎假设的那样。 You can automatically detect the correct extension using imghdr, which is included with Python 3.9.您可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM