temporarily retrieve an image using the requests library
Asked Answered
N

2

7

I'm writing a web scraper than needs to scrape only the thumbnail of an image from the url.

This is my function using, the urlib library.

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

for various reasons I need to change this to use the requests library, what would be the equivalent for temp saving an image?

Nebulous answered 29/9, 2015 at 21:21 Comment(0)
I
10

You could skip saving to a temporary file part and use the corresponding response object directly to create the image:

#!/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)

Here's requests analog:

#!/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)

I've tested it with Pillow 2.9.0 and requests 2.7.0. It should work since Pillow 2.8.

Irony answered 30/9, 2015 at 6:31 Comment(1)
This also works on requests~=2.26.0 and Pillow~=9.0.0Transgression
B
5

You can write to a 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")
Babcock answered 29/9, 2015 at 21:35 Comment(5)
I'm getting an error: File "/home/david/.virtualenvs/stocksearch/lib/python3.4/posixpath.py", line 148, in dirname i = p.rfind(sep) + 1 AttributeError: '_io.BytesIO' object has no attribute 'rfind'Nebulous
What is Imagelib.open?Babcock
I have an Image class so I imported the PILLOW Image class as ImagelibNebulous
I added a full working example using PIL to save a thumbnailBabcock
you could use r.raw instead of BytesIO(r.content) hereIrony

© 2022 - 2024 — McMap. All rights reserved.