Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to name pictures in batch in linux

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use Python in linux to name pictures in batches. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

When we conduct machine learning training, in order to facilitate the management of different kinds of pictures, we need to name the same kind of pictures in batches.

The 1 picture folder has a lot of photos, and we need to rename it.

At this point we can start the terminal in the picture folder and create a python script named rename.py.

Touch rename.py

3 open a new terminal, enter

Gedit rename.py

4 paste the following code into the rename.py script

Import osclass BatchRename ():''batch rename the picture files in the folder' 'def _ _ init__ (self): self.path =' / media/hyr/D952-0C52/ meat training set / picture' # indicates the folder to be named def rename (self): filelist = os.listdir (self.path) # get the file path print ('filelist' Filelist) total_num = len (filelist) # get the file length (number) I = 1 # indicates that the naming of the file starts with 1: for item in filelist: print ('item',item) if item.endswith (' .jpg'): # the format of the initial image is in jpg format (or the source file is in png format or other formats The later conversion format can be adjusted to the format you want) src = os.path.join (os.path.abspath (self.path), item) print ('src',src) # dst = os.path.join (os.path.abspath (self.path),' + str (I) + '.jpg') # the processed format is also jpg format Of course, it can be changed to png format dst = os.path.join (os.path.abspath (self.path), 'rou' + format (str (I),' 0 > 3s') + '.jpg') # in this case, the naming format is in 0000000.jpg form You can define the desired format try: os.rename (src, dst) print ('converting% s to% s...'% (src, dst)) I = I + 1 except: continue print ('total% d to rename & converted% d jpgs'% (total_num) I) if _ _ name__ = ='_ _ main__': demo = BatchRename () demo.rename ()

Change self.path ='/ media/hyr/D952-0C52/ meat training set / picture' to your own file absolute path.

5 start the terminal, enter

Python3 rename.py

6 after the operation is completed

7 at this point, we looked at the picture folder and found that all the photos had been renamed.

The above is all the contents of the article "how to use Python to name pictures in batch in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report