Created
April 19, 2021 09:33
-
-
Save larryhou/80607ea04fb9098b0da8ae6b133f16d1 to your computer and use it in GitHub Desktop.
Extract image channels into sperate grayscale image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
from PIL import Image | |
import re | |
def main(): | |
import sys | |
for filename in sys.argv[1:]: | |
items = Image.open(filename).split() | |
names = ('r', 'g', 'b', 'a') | |
for n in range(len(items)): | |
channel = items[n] | |
savename = re.sub(r'(\.[^.]+)$', r'_{}\g<1>'.format(names[n]), filename) | |
channel.save(savename) | |
print('>>> {}'.format(savename)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment