Advertisement
jewalky

Untitled

Jan 5th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # python 3, requires python -m pip install pillow
  4.  
  5. import sys
  6. import os
  7. import math
  8. from PIL import Image
  9.  
  10. if len(sys.argv) < 2:
  11.     print('usage: %s <directory1> [directory2 [...]]'%(sys.argv[0]))
  12.     exit(0)
  13.  
  14. def check_process(file):
  15.     if file.split('.')[-1].lower() != 'png':
  16.         return
  17.     print('input: %s'%(file))
  18.     im = Image.open(file)
  19.     rgba_im = im.convert('RGBA')
  20.     #rgba_im.getpixel((0, 0))
  21.     #print(repr(rgba_im.size))
  22.     for y in range(rgba_im.size[1]):
  23.         for x in range(rgba_im.size[0]):
  24.             px = rgba_im.getpixel((x, y))
  25.            
  26.             # check if px meets the color range.
  27.             px1_expected = px[0]*1.5
  28.             px2_expected = px[0]*2.0
  29.            
  30.             if (abs(px[1]-px1_expected) > 32) or (abs(px[2]-px2_expected) > 32):
  31.                 continue
  32.            
  33.             # get the luminance
  34.             lum = px[0] * 0.3 + px[1] * 0.4 + px[2] * 0.3
  35.             px = (0, int(lum), 0, px[3])
  36.            
  37.             rgba_im.putpixel((x, y), px)
  38.     #rgba_im.save(file+'.2.png')
  39.     rgba_im.save(file)
  40.    
  41. for rootDir in sys.argv[1:]:
  42.     for dirName, dDirList, dFileList in os.walk(rootDir):
  43.         for file in dFileList:
  44.             file = os.path.normpath(dirName+'/'+file)
  45.             check_process(file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement