#coding:utf-8
from PIL import Image
import os
import glob
import xml.etree.ElementTree as ET
import xml.dom.minidom
imgsdir = "/home/hs/yolov5/yolov5/data/imageyili"
newimg = "/home/hs/yolov5/yolov5/data/imageyili"
n =1
if not os.path.exists(newimg):
os.mkdir(newimg)
# for pngfiles in os.listdir(imgsdir):
# path = os.path.join(imgsdir, pngfiles)
# print("path:", path)
for pngfile in os.listdir(imgsdir):
print("pngfile:", pngfile)
# try:
pngname = os.path.splitext(pngfile)[0]
if os.path.splitext(pngfile)[1] == '.png':
olddir = os.path.join(os.path.abspath(imgsdir), pngname + ".png")
newdir = os.path.join(os.path.abspath(newimg), '%04d' % int(n)+".png")
# newdir = os.path.join(os.path.abspath(newimg), pngname + 'g' +".png")
elif os.path.splitext(pngfile)[1] == '.jpg':
olddir = os.path.join(os.path.abspath(imgsdir), pngname + ".jpg")
newdir = os.path.join(os.path.abspath(newimg), '%04d' % int(n)+".jpg")
# newdir = os.path.join(os.path.abspath(newimg), pngname + 'g' +".jpg")
else:
olddir = os.path.join(os.path.abspath(imgsdir), pngname + ".JPG")
newdir = os.path.join(os.path.abspath(newimg), '%04d' % int(n)+".JPG")
# newdir = os.path.join(os.path.abspath(newimg), pngname + 'g' +".JPG")
os.rename(olddir, newdir)
n += 1
# except Exception as e:
# pass
# continue
# os.rename(old_dir, new_dir)
print('total number is ', n)
批量修改图片name
最新推荐文章于 2023-11-09 15:45:13 发布
这段代码用于将指定目录下的png图片文件按照特定格式进行重命名并移动到新的目录中。它遍历指定目录,检查文件类型,并使用os.rename()进行重命名操作,确保新目录存在,并按0001, 0002等格式编号。
摘要由CSDN通过智能技术生成