User:Mths/rgb sort

From XPUB & Lens-Based wiki

Lastf.jpg Rgb loop.gif

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)