批量修改图片编号python
时间: 2023-10-10 18:08:06 浏览: 75
要批量修改图片编号,你可以使用Python来完成这个任务。以下是一种可能的方法:
```python
import os
from PIL import Image
def batch_rename_images(folder_path):
file_list = os.listdir(folder_path)
pic_id = 1
for file_name in file_list:
if file_name.endswith('.jpg') or file_name.endswith('.png'):
src = os.path.join(folder_path, file_name)
new_file_name = str(pic_id) + '.jpg'
dst = os.path.join(folder_path, new_file_name)
try:
os.rename(src, dst)
pic_id += 1
print(f'Renamed {file_name} to {new_file_name}')
except Exception as e:
print(f'Failed to rename {file_name}: {e}')
print('Batch renaming complete.')
# 调用函数并传入文件夹路径作为参数
folder_path = '路径/to/your/folder'
batch_rename_images(folder_path)
```
在上述代码中,你需要将`folder_path`变量替换为你想要批量修改图片编号的文件夹路径。函数将遍历指定文件夹中的所有图片文件(以.jpg或.png结尾),并按顺序命名为`1.jpg`,`2.jpg`,`3.jpg`,依此类推。请确保在运行代码之前备份好你的图片,以免出现意外情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [python批量修改图像名](https://blog.csdn.net/qq_47629400/article/details/123413056)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [Python脚本批量给图片重命名](https://blog.csdn.net/cll_869241/article/details/129185291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文