python——批量png转jpg(通道数问题)

def png2jpg(dirname_read, dirname_write):
    """
    :param dirname_read: 例:r"OriDataset/"
    :param dirname_write: 例:r"jpgDataset/"
    :return:
    """
    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, a = 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

你可能感兴趣的:(python)