→
# In[]
#改jpg名字
import os
import cv2
import matplotlib.pyplot as plt
from pathlib import Path
PATH = "1" #文件路径
i = 0
#子文件夹路径
for childlist in os.listdir(PATH):
#子文件夹路径
childPATH = PATH + '/'+ str(childlist)
for parent, dirs, files in os.walk(childPATH):
for file in files:
if file.endswith('.jpg'):
name = os.path.join(parent, file)
img = cv2.imread(name)
newname=os.path.join(parent, str(childlist)+".jpg")
os.rename(name,newname)
i = i + 1
print(i)
# In[]//合并png文件
import os
import cv2
import matplotlib.pyplot as plt
PATH = "Pano_0111" #文件路径
i = 0
#子文件夹路径
img1=0
imgadd1=0
teethname='a'
jump=0
for childlist in os.listdir(PATH):
#子文件夹路径
childPATH = PATH + '/'+ str(childlist)
for parent, dirs, files in os.walk(childPATH):
for file in files:
if file.endswith('mask.png'):
jump=1
if jump==1:
jump=0
continue
for file in files:
if not file.startswith('mask_'):
if file.endswith('.jpg'):
teethname = os.path.join(parent, file)
continue
name = os.path.join(parent, file)
img = cv2.imread(os.path.join(parent, file))
if i==0:
img1=img
imgadd1=img
i = i + 1
continue
img = cv2.resize(img, (img1.shape[1], img1.shape[0]))
imgadd1 = img1 + img # cv2.add(img, img1)
img1 = imgadd1
i = i + 1
print(i)
print(os.path.join(parent, file))
# plt.imshow(imgadd1)
cv2.imwrite((teethname[: -4] + "_mask.png"), imgadd1)
del imgadd1
del img1
imgadd1=0
img1=0
i=0
# In[]
import shutil
# 移动文件夹,分为img、labelcol
Folder = PATH # 源目录(包含png图片的当前目录)
# 目标目录(存放png图片的新文件夹)
img_dir = Folder+'/img' # 替换为目标文件夹路径
labelcol_dir = Folder+'/labelcol' # 替换为目标文件夹路径
if not os.path.exists(img_dir):
os.makedirs(img_dir)
if not os.path.exists(labelcol_dir):
os.makedirs(labelcol_dir)
# 遍历目录文件
for filename in os.listdir(Folder):
if filename.endswith('.jpg'):
shutil.move(os.path.join(Folder, filename), os.path.join(img_dir, filename))
for filename in os.listdir(Folder):
if filename.endswith('.png'):
shutil.move(os.path.join(Folder, filename), os.path.join(labelcol_dir, filename))