-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_namechange.py
More file actions
39 lines (29 loc) · 1.41 KB
/
Copy pathimage_namechange.py
File metadata and controls
39 lines (29 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
def image_namechange(images_directory):
if not os.path.exists(images_directory):
os.makedirs(images_directory)
# List all files in the directory
files = os.listdir(images_directory)
# Filter only .webp files
webp_files = [file for file in files if file.endswith(".webp")]
# Sort the existing numeric filenames
numeric_files = sorted([file for file in webp_files if file[:-5].isdigit()], key=lambda x: int(x[:-5]))
# Rename the existing numeric filenames to maintain sequential ordering
for index, file in enumerate(numeric_files, start=1):
old_path = os.path.join(images_directory, file)
new_name = f"{index}.webp"
new_path = os.path.join(images_directory, new_name)
os.rename(old_path, new_path)
# Find the next available number for renaming non-numeric filenames
next_number = len(numeric_files) + 1
# Rename the remaining non-numeric filenames starting from 1
for file in webp_files:
if not file[:-5].isdigit():
old_path = os.path.join(images_directory, file)
new_name = f"{next_number}.webp"
new_path = os.path.join(images_directory, new_name)
os.rename(old_path, new_path)
next_number += 1
# Example usage:
# images_directory = "channels/the_universe_decoded/images/" # Replace with your images directory
# image_namechange(images_directory)