Vishal Reddy
Vishal Reddy

Reputation: 114

I have a file with n URLS, I what to download these to certain location and also have same dimensions to all images using python

I have a .txt file with n urls like below:

url1
url2
url3
url4
url5
  1. I want to download these images to C:\Users\username\Pictures location
  2. Each image must be downscaled or upscaled to 256x256 dimension.

Upvotes: 0

Views: 23

Answers (1)

Dan Constantinescu
Dan Constantinescu

Reputation: 1526

Here is a rough code with the things needed to achieve your goal:

import requests

from PIL import Image

with open('D:/urls.txt') as f:
    urls = f.read().split('\n')

for url in urls:
    content = requests.get(url).content

    with open('D:/image_big_' + f'{urls.index(url)}' + '.jpg', 'wb') as f:
        f.write(content)

    with Image.open('D:/image_big_' + f'{urls.index(url)}' + '.jpg') as img:
        img.thumbnail(size=(256, 256))
        img.save('D:/image_small_' + f'{urls.index(url)}' + '.jpg', bitmap_format="png")

Upvotes: 1

Related Questions