Skip to content

Instantly share code, notes, and snippets.

@larryhou
Created April 19, 2021 09:33
Show Gist options
  • Save larryhou/80607ea04fb9098b0da8ae6b133f16d1 to your computer and use it in GitHub Desktop.
Save larryhou/80607ea04fb9098b0da8ae6b133f16d1 to your computer and use it in GitHub Desktop.
Extract image channels into sperate grayscale image
#!/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