[python] Change the image file name to a serial number

Thing you want to do

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

Preparation

・ Assuming that python is already installed -Collect images whose names you want to serialize in an arbitrary folder (image this time)

Create code

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)
  1. Get the name of the image in the image folder
files = glob.glob('image/*')
  1. Repeat the name change process for the 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.

  1. Decompose the file name
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 ".

  1. Rename the file
os.rename(f, format(idx, '04d')+fext)

Recommended Posts

[python] Change the image file name to a serial number
Change the standard output destination to a file in Python
I want to get the file name, line number, and function name in Python 3.4
A Python script that saves a clipboard (GTK) image to a file.
[Python] Save the video data imported by OpenCV as a serial number jpg file
Try to decipher the garbled attachment file name with Python
[Python] Change the alphabet to numbers
Create a shell script to run the python file multiple times
[Go] Create a CLI command to change the extension of the image
Python vba to create a date string for creating a file name
A script that returns 0, 1 attached to the first Python prime number
[Python] A program that calculates the number of socks to be paired
[Python] Create a linebot to write a name and age on an image
How to put a line number at the beginning of a CSV file
The file name was bad in Python and I was addicted to import
[Bash] Command to write a file line by line to a serial number file [One liner (?)]
I made a program to check the size of a file in Python
How to read a CSV file with Python 2/3
[Python] How to change the date format (display format)
How to create a JSON file in Python
Save the object to a file with pickle
Export Python3 version OpenCV KeyPoint to a file
A python amateur tries to summarize the list ②
I want to write to a file with Python
The story of making a tool to load an image with Python ⇒ save it as another name
[Python] Throw a message to the slack channel
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
Create a Python image in Django without a dummy image file and test the image upload
Python / subprocess> Symbolic link Implementation to get only the destination file name> os.readlink ()
How to read a serial number file in a loop, process it, and graph it
[Python] I tried to get the type name as a string from the type function
Python Memorandum: Refer to the text and edit the file name while copying the target file
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
File name batch change
I want to see the file name from DataLoader
Check if the string is a number in python
python beginners tried to predict the number of criminals
Parse a JSON string written to a file in Python
How to get the variable name itself in python
[Python] A program that counts the number of valleys
Template of python script to read the contents of the file
Get the file name in a folder using glob
How to get the number of digits in Python
python> Change file owner> os.chown ("path / to / file", uid, gid)
I tried to touch the CSV file with Python
A memorandum to run a python script in a bat file
Read the xml file by referring to the Python tutorial
Convert the image in .zip to PDF with Python
I want to randomly sample a file in Python
[Python] Change standard input from keyboard to text file
Python program that looks for the same file name
[Work efficiency] How to change file names in Python
[Python] Mask the image into a circle using Pillow
Python script to create a JSON file from a CSV file
Python OpenCV tried to display the image in text.
Python Note: The mystery of assigning a variable to a variable
The file name saved by pysheng was a hexadecimal number, so I fixed it.
How to identify the element with the smallest number of characters in a Python list?
[Python] You can save an object to a file by using the pickle module.
How to change the generated image of GAN to a high quality one to your liking
I tried to find the entropy of the image with python