参考 将png格式的图像转换为jpg - 云+社区 - 腾讯云
import osfrom PIL import Imagedirname_read="D:\dataset\cityscapes\cityscape_voc_clean\JPEGImages_png\\"dirname_write="D:\dataset\cityscapes\cityscape_voc_clean\JPEGImages_jpg\\"names=os.listdir(dirname_read)count=0for name in names:img=Image.open(dirname_read+name)name=name.split(".")if name[-1] == "png":name[-1] = "jpg"name = str.join(".", name)r,g,b=img.split()img=Image.merge("RGB",(r,g,b))to_save_path = dirname_write + nameimg.save(to_save_path)count+=1print(to_save_path, "------conut:",count)else:continue