Python Forum

Full Version: Download multiple images and rename them
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Morning guys, I'm trying to download pictures and rename them

I've got 1 product, that has 5 pictures, so 5 pictures to download and rename.

It would look something like :
(full url to download - http://img.tennis-warehouse.com/new_big/AMDBNBW-1.jpg)

url = ['http://img.tennis-warehouse.com/new_big/'] # url is always the same, the name of the of the picture chances always (it's a product name)

old filename = "AMDBNBW-1.jpg" (there's 5 pictures always, so AMDBNBW-2.jpg, AMDBNBW-3.jpg, AMDBNBW-4.jpg, AMDBNBW-5.jpg )
new filename = "FRIDAY-1.jpg" also renamed sequantially from 1 to 5.jpg

I need to rename the AMDBNBW-1.jpg, to something like "FRIDAY-1.jpg", "FRIDAY-2.jpg", "FRIDAY-3.jpg", "FRIDAY-4.jpg", "FRIDAY-5.jpg"

Please help me to solve this. Thank you in advance
You should always show results of your effort! It is not very nice to ask help without spending your own time in trying to solve your problem.

Desired result can be achieved:

import urllib.request

for x in range(1, 6):
    website = f'http://img.tennis-warehouse.com/new_big/AMDBNBW-{x}.jpg'
    filename = f'FRIDAY-{x}.jpg'
    urllib.request.urlretrieve(website, filename)
Thank you @perfringo ! My initial code using wget is located here :

How do deal when u have multiple product codes ?
It depends. If you have list of product codes, pictures are in same location and there is always same number of photos then you just add additional layer for looping, something like that (incomplete snippet):

skus = ['AMDBNBW', 'AGHJKBW', 'ARTYNBW']

for sku in skus:
    for x in range(1, 6):
        website = f'http://img.tennis-warehouse.com/new_big/{sku}-{x}.jpg'
        
If there are several product codes issue of naming downloaded files must be solved. Once again, it depends what do you have. If you have list of desired filenames then you can match them with zip:

skus = ['AMDBNBW', 'AGHJKBW', 'ARTYNBW']
names = ['FRIDAY', 'SATURDAY', 'SUNDAY']

for sku, name in zip(skus, names):
    for x in range(1, 6):
        website = f'http://img.tennis-warehouse.com/new_big/{sku}-{x}.jpg'
        filename = f'{name}-{x}.jpg'
        urllib.request.urlretrieve(website, filename)
If there is no lists and required data must be entered manually then it makes sense to write it in suitable format right away:

record = [('AMDBNBW', 'FRIDAY'),('AGHJKBW', 'SATURDAY'), ('ARTYNBW', 'SUNDAY')]

Then for-loop becomes:

for sku, name in record:

If there is variable number of product pictures then you set range to maximum possible pictures and use try... except to catch HTPPError and break out from loop.
Thank you guys ! You know you're great !