将png格式的图像转换为jpg

2022-09-02 20:53:28 浏览数 (1)

代码语言:javascript复制
import os
from PIL import Image

dirname_read="D:datasetcityscapescityscape_voc_cleanJPEGImages_png\"
dirname_write="D:datasetcityscapescityscape_voc_cleanJPEGImages_jpg\"
names=os.listdir(dirname_read)
count=0
for 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   name
        img.save(to_save_path)
        count =1
        print(to_save_path, "------conut:",count)
    else:
        continue
png

0 人点赞