Python Forum
Download multiple images and rename them
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
Reply
#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.
Reply
#3
Thank you @perfringo ! My initial code using wget is located here :

How do deal when u have multiple product codes ?
Reply
#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.
Reply
#5
Thank you guys ! You know you're great !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rename multiple photos with DateTimeOriginal timestamp Stjude1982 2 1,109 Oct-21-2022, 12:24 AM
Last Post: Pedroski55
  Rename part of filename in multiple files atomxkai 7 7,217 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  download with internet download manager coral_raha 0 2,883 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,237 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Copy mp3 file multiple times and rename Mar10 4 3,678 Sep-23-2020, 01:09 AM
Last Post: Mar10
  naming images adding to number within multiple functions Bmart6969 0 1,894 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Download multiple large json files at once halcynthis 0 2,752 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  Pasting multiple images on to one image with a for loop. DreamingInsanity 2 6,416 Feb-01-2019, 12:39 PM
Last Post: DreamingInsanity
  Python script batch download and batch rename jkhaksaar 3 3,371 Oct-11-2018, 03:42 PM
Last Post: snippsat
  Download multiple images with one click andie31 0 2,241 Sep-13-2018, 10:37 AM
Last Post: andie31

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020