Change the image file name to a serial number. [Change before] a.jpg b.jpg c.jpg [After change] 0001.jpg 0002.jpg 0003.jpg
・ Assuming that python is already installed -Collect images whose names you want to serialize in an arbitrary folder (image this time)
import os
import glob
files = glob.glob('image/*')
for idx, f in enumerate(files):
ftitle, fext = os.path.splitext(f)
os.rename(f, format(idx, '04d')+fext)
files = glob.glob('image/*')
for idx, f in enumerate(files):
By enumrate, a number is entered in idx, and each time it is repeated with for, the number is incremented by 1.
ftitle, fext = os.path.splitext(f)
ftitle: File name ftext: Extension For example, for a.jpg, ftitle will contain "a" and ftext will contain ".jpg ".
os.rename(f, format(idx, '04d')+fext)
Recommended Posts