用就行,不需要理解。批量處理python程序

回答: 這麽複雜ymliu2017-07-18 20:30:04

from PIL import Image, ImageOps

from glob import glob

import os

os.chdir('/your/image/folder')

files = glob('*.jpg')
for f in files:
    image = Image.open(f)
    if image.mode == 'RGBA':
        r,g,b,a = image.split()
        rgb_image = Image.merge('RGB', (r,g,b))
        inverted_image = ImageOps.invert(rgb_image)
        r2,g2,b2 = inverted_image.split()
        final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a))
        final_transparent_image.save('rev_' + f)
    else:
        inverted_image = ImageOps.invert(image)
        inverted_image.save('rev_' + f)

 

請您先登陸,再發跟帖!