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?
requests~=2.26.0
andPillow~=9.0.0
– Transgression