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 does Python rename and sort according to the photo modification time

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "Python how to rename and sort according to photo modification time", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python how to rename and sort according to photo modification time" article.

1. Background

When you want to synthesize a photo sequence into a delayed photography video, you may find that one of the photos is missing, or the photo sequence is non-contiguous across time, as shown in figure 1, but only continuous photo sequences can be imported in PR.

II. Method

In this case, you often need to rename photos according to the modification time. A very simple python script is written below, which theoretically supports renaming a photo sequence of less than 10000 photos, as follows:

1. Back up the photos (there is no undo mechanism to avoid script errors that cannot be undone)

two。 Put the rename.exe file in the photo file and double-click to rename it (cannot change the name). If you have installed python, you can also double-click the script file directly.

3. Codeimport os def reNameByTime (path): mlist = [] # get file name list files = os.listdir (path) # get name new file name list with timestamp for filename in files: # get the last modification time of the file modifytimes = os.path.getmtime (path + filename) filename_lower = filename.lower () # filter .jpg format If ".jpg" in filename_lower: mlist.append (str (int (modifytimes)) + "-" + filename) # .jpg mlist = sorted (mlist) # traversal modification timestamp to sequence number for i in range (len (mlist)): # intercept to get the original file name oldname = mlist [I] [11:] # slice operation Change the timestamp to the serial number from 11 to # to get the new file name if (I + 1)

< 10: # 0-9 newname = "000" + str(i + 1) + ".jpg" elif (i + 1) >

9 and (I + 1)

< 100: # 10-100 newname = "00" + str(i + 1) + ".jpg" elif (i + 1) >

99 and (I + 1) < 1000: # 100-1000 newname = "0" + str (I + 1) + ".jpg" else: newname = str (I + 1) + ".jpg" # 1000-9999 # print (newname, oldname) # rename File Sort by modification time and add the prefix os.rename (path + oldname, path + newname) # if you want to execute a py file, you can change the suffix to ".py". If you want to package it into exe, you need to change the suffix to ".exe" if _ _ name__ = "_ _ main__": filepath = os.sys.argv [0]. Replace ("rename.py", ") print (os.sys.argv [0]) reNameByTime (filepath)

If you want to sort by another time:

# os.path.getatime (file) output file access time # os.path.getctime (file) creation time of output file # os.path.getmtime (file) output file last modified time

If you want to sort other types of files, change the corresponding suffix name.

The above is about the content of this article "how to rename and sort Python according to photo modification time". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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