代码拉取完成,页面将自动刷新
import os
from PIL import Image
# Vols填写多个文件夹名称,若名称具有规律,也可以在外层用for循环先读取所有文件夹名称进Vols里
# Vols = ['文件夹1', '文件夹2', '文件夹3', '文件夹4']
Vols = ['D:/DataSet/7classesdetection/7classesdetection/JPEGImages']
for Vol in Vols:
# 所有photo所在的文件夹目录
path_photo = Vol
print('正在重置路径', path_photo)
# 得到文件夹下的所有文件名称,存在字符串列表中
files_lists = os.listdir(path_photo)
# 转变工作目录到指定文件夹
os.chdir(path_photo)
for files_list in files_lists:
print(files_list)
# 读取每个png文件
img = Image.open(files_list)
# 将文件名中的png替换为jpg
files_list = files_list.replace('png', 'jpg')
rgb_img = img.convert('RGB')
rgb_img.save(files_list)
img.close()
print(type(files_lists))
print(files_lists) # 打印path_photo文件夹下的所有文件
# 删除原来的png
for pic in files_lists: # 遍历文件
if pic.endswith(".png"):
os.remove(path_photo + '/' + pic)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。