使用请求库临时检索图像

时间:2015-09-29 21:21:50

标签: python web-scraping python-requests urllib

我正在编写一个网络刮刀,而不是仅从网址中抓取图像的缩略图。

这是我使用urlib库的函数。

def create_thumb(self):
    if self.url and not self.thumbnail:
        image = urllib.request.urlretrieve(self.url)

        # Create the thumbnail of dimension size
        size = 350, 350
        t_img = Imagelib.open(image[0])
        t_img.thumbnail(size)

        # Get the directory name where the temp image was stored
        # by urlretrieve
        dir_name = os.path.dirname(image[0])

        # Get the image name from the url
        img_name = os.path.basename(self.url)

        # Save the thumbnail in the same temp directory
        # where urlretrieve got the full-sized image,
        # using the same file extention in os.path.basename()
        file_path = os.path.join(dir_name, "thumb" + img_name)
        t_img.save(file_path)

        # Save the thumbnail in the media directory, prepend thumb
        self.thumbnail.save(
            os.path.basename(self.url),
            File(open(file_path, 'rb')))

出于各种原因我需要更改它以使用请求库,临时保存图像的等价物是什么?

2 个答案:

答案 0 :(得分:6)

您可以跳过保存到临时文件部分并直接使用相应的响应对象来创建图像:

{true, true,false}
{true,false,false}
{true,false, false}

此处#!/usr/bin/env python3 import urllib.request from PIL import Image # $ pip install pillow im = Image.open(urllib.request.urlopen(url)) print(im.format, im.mode, im.size) 类似物:

requests

我已使用#!/usr/bin/env python import requests # $ pip install requests from PIL import Image # $ pip install pillow r = requests.get(url, stream=True) r.raw.decode_content = True # handle spurious Content-Encoding im = Image.open(r.raw) print(im.format, im.mode, im.size) 2.9.0和Pillow 2.7.0对其进行了测试。它应该在Pillow 2.8之后起作用。

答案 1 :(得分:3)

您可以写信给io.BytesIO:

import requests

from PIL import Image
from io import BytesIO

r = requests.get(self.url)
b = BytesIO(r.content)
size = 350, 350
img = Image.open(b)
img.thumbnail(size)
img.save("foo.thumbnail", "JPEG")