change images to RGB
2019-12-18 本文已影响0人
狼无雨雪
import os
from PIL import Image
import sys
def change_image_channels(input_image_path):
image = Image.open(input_image_path)
if image.mode == 'RGBA':
r, g, b, a = image.split()
image = Image.merge("RGB", (r, g, b))
try:
os.remove(input_image_path)
except:
pass
image.save(input_image_path)
elif image.mode != 'RGB':
image = image.convert("RGB")
try:
os.remove(input_image_path)
except:
pass
image.save(input_image_path)
else:
pass
return image
if __name__ == "__main__":
input_dir = sys.argv[1]
for root, dirs, files in os.walk(input_dir):
for file in files:
path = os.path.join(root, file)
change_image_channels(path)