在使用YOLO时,发现需要的image需要的是jpg格式,而现有的数据集是png格式。 于是需要一个小脚本来进行批量转换
看到有人已经做了相关工作,于是在此基础上稍作修改,完成需求。
import os
from PIL import Image
dirname_read = "D:/Dataset/wangzhe/images/"
dirname_write = "D:/Dataset/wangzhe/images2/"
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 = img.convert('RGB')
img.save(to_save_path)
count += 1
print(to_save_path, "------conut:", count)
else:
continue