When scraping websites containing thousands or even lakhs of images, dynamic naming is crucial. Many images may have identical filenames and saving them directly can cause overwriting and data loss. Dynamic naming ensures that every image is stored with a unique filename, making the process safe and scalable. In this article we will see how to do it using Python.
Before downloading all images, let us first download one sample image to understand the process.
Downloading a Single Image
Downloading a single image is the first step in image scraping, used to verify the image URL and ensure the download and saving process works correctly before handling multiple images. Here we will use Python Request library to do this.
The website used in this article: https://picsum.photos/300/200
- requests.get(url): downloads the image.
- with open(..., 'wb') as f: opens file in binary write mode and auto-closes it.
- f.write(res.content): saves the image bytes to img.jpg.
import requests
url = "https://picsum.photos/300/200"
res = requests.get(url)
fd = open('img.jpg', 'wb')
fd.write(res.content)
fd.close()
Output:

Defining a Function to Download Images
Defining a function to download images helps reuse the same logic, reduces code repetition and makes the image downloading process cleaner and easier to scale.
import requests
def download(link, img_path):
res = requests.get(link)
fd = open(img_path, 'wb')
fd.write(res.content)
fd.close()
link = "https://picsum.photos/300/200"
img_path = "img.jpg"
download(link, img_path)
Output:

Downloading and Dynamically Naming All Images
Now, we use the function to download all images and assign unique IDs to each image. Here we:
- Creates a folder Imgs to save images.
- Loops through each URL, downloads the image, saves it with a unique ID.
- Tracks image paths and IDs in the DataFrame.
- Exports everything to imgs.csv.
import pandas as pd
import requests
from tqdm import tqdm
import os
os.makedirs('Imgs', exist_ok=True)
df = pd.DataFrame({
'img_link': [
'https://picsum.photos/300/200',
'https://picsum.photos/300/201',
'https://picsum.photos/300/202'
]
})
def download(link, img_path):
res = requests.get(link)
with open(img_path, 'wb') as fd:
fd.write(res.content)
paths = []
img_ids = []
ref = 100000
i = 1
for link in tqdm(df['img_link']):
img_id = 'A' + str(i + ref)[1:]
i += 1
img_path = 'Imgs/' + img_id + '.jpg'
download(link, img_path)
paths.append(img_path)
img_ids.append(img_id)
df['img_id'] = img_ids
df['img_path'] = paths
df.to_csv('imgs.csv', index=False)
Output:
Here we can see a folder named Imgs is created where all images are saved and dyamically named.