Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # python 3, requires python -m pip install pillow
- import sys
- import os
- import math
- from PIL import Image
- if len(sys.argv) < 2:
- print('usage: %s <directory1> [directory2 [...]]'%(sys.argv[0]))
- exit(0)
- def check_process(file):
- if file.split('.')[-1].lower() != 'png':
- return
- print('input: %s'%(file))
- im = Image.open(file)
- rgba_im = im.convert('RGBA')
- #rgba_im.getpixel((0, 0))
- #print(repr(rgba_im.size))
- for y in range(rgba_im.size[1]):
- for x in range(rgba_im.size[0]):
- px = rgba_im.getpixel((x, y))
- # check if px meets the color range.
- px1_expected = px[0]*1.5
- px2_expected = px[0]*2.0
- if (abs(px[1]-px1_expected) > 32) or (abs(px[2]-px2_expected) > 32):
- continue
- # get the luminance
- lum = px[0] * 0.3 + px[1] * 0.4 + px[2] * 0.3
- px = (0, int(lum), 0, px[3])
- rgba_im.putpixel((x, y), px)
- #rgba_im.save(file+'.2.png')
- rgba_im.save(file)
- for rootDir in sys.argv[1:]:
- for dirName, dDirList, dFileList in os.walk(rootDir):
- for file in dFileList:
- file = os.path.normpath(dirName+'/'+file)
- check_process(file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement