User:Mths/rgb sort

From XPUB & Lens-Based wiki
< User:Mths
Revision as of 17:20, 10 March 2013 by Mths (talk | contribs) (Created page with "<source lang="python"> from PIL import Image, ImageDraw import sys filename = sys.argv[1] newfilename = sys.argv[2] im = Image.open(filename) colors = im.getcolors(im.size[...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
from PIL import Image, ImageDraw
import sys

filename = sys.argv[1]
newfilename = sys.argv[2]

im = Image.open(filename)
colors = im.getcolors(im.size[0] * im.size[1])
pixels = []

for i, color in colors:
    pixels.extend(i * [color])
    
def inputselect():
    sortinput = int(sys.argv[3])
    
    if sortinput == 0:
        pixels.sort(key=lambda x: x[0])
        print sortinput
    elif sortinput == 1:
        pixels.sort(key=lambda x: x[1])
        print sortinput
    elif sortinput == 2:
        pixels.sort(key=lambda x: x[2])
        print sortinput

try: 
    inputselect()
    
except IndexError: 
    pixels.sort()
    print 'sorting...'
    

im2 = Image.new(im.mode, im.size)
im2.putdata(pixels)    
im2.save(newfilename)