One: bat script implementation
Step 1: Create a .txt file
Step 2: Write ren *.png *.jpg in the .txt file
Step 3: Right-click to rename and change the suffix from .txt to .bat
Step 4: Put the file into the folder of the picture to be modified, and then double-click it!
PS: If you want to modify all types of files under the folder to jpg format, use the ren *.* *.jpg command
Two: Python implementation
code show as below:
import os
from PIL import Image
dirname_read="D:/MyDataset/" # 注意后面的斜杠
dirname_write="D:/MyDataset_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)
to_save_path = dirname_write + name
img = img.convert('RGB')#RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度。而JPG不支持透明度,所以要么丢弃Alpha,要么保存为.png文件
img.save(to_save_path)
count+=1
print(name)
else:
continue
print('运行完成!共转换了 %d 张图片' % (count))
Reproduced the original text: Python and bat scripts realize batch conversion of PNG images into JPG images_python png to jpg_una_mattina7's blog-CSDN blog