Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Download multiple images and rename them
#1
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
Quote
#2
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
Thank you @Perfringo ! My initial code using wget is located here :

How do deal when u have multiple product codes ?
Quote
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#5
Thank you guys ! You know you're great !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  naming images adding to number within multiple functions Bmart6969 0 111 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Download multiple large json files at once halcynthis 0 360 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  Pasting multiple images on to one image with a for loop. DreamingInsanity 2 359 Feb-01-2019, 12:39 PM
Last Post: DreamingInsanity
  Python script batch download and batch rename jkhaksaar 3 639 Oct-11-2018, 03:42 PM
Last Post: snippsat
  Download multiple images with one click andie31 0 550 Sep-13-2018, 10:37 AM
Last Post: andie31

Forum Jump:


Users browsing this thread: 1 Guest(s)