Skip to content

Instantly share code, notes, and snippets.

@vvdr12
Created September 13, 2013 07:28
Show Gist options
  • Save vvdr12/6547663 to your computer and use it in GitHub Desktop.
Save vvdr12/6547663 to your computer and use it in GitHub Desktop.
Place divisiblesort.py script in same folder as the picture you want to glitch: 'test.jpg'. Delete first line if you are not using Linux. I like the effect best when using one of the recommended divisible numbers... It'll explain. For cross-hatch pixel sort: Sort once, flip image 90 degrees, sort again.
#! /usr/bin/python
import Image
#_______________________________________________________open
source = Image.open("test.jpg")
img = source.load()
print source.format
print source.size
print source.mode
x = source.size[0]
y = source.size[1]
#_______________________________________________________prep pxmap
print "\n...Loading Pixel Map..."
pxmap=[]
j=0
l1=1
while l1==1:
if j%100==0:
print j,"/",y
i=0
l2=1
while l2==1:
r=img[i,j][0]
g=img[i,j][1]
b=img[i,j][2]
ave=(r+g+b)/3
pxmap.append([ave,r,g,b])
i=i+1
if i==x:
l2=0
j=j+1
if j==y:
l1=0
#_______________________________________________________set divisor
k=1
l1=1
print '\nImage Width Divisible By: \n[choosing one gives clean vertical lines but is not neccesary]\n[10s to 50s work best]'
while l1==1:
if x%k==0:
print " ",k
k=k+1
if k==300:
l1=0
length = int(raw_input("length:\n>"))
#_______________________________________________________sort
print "\n...Sorting..."
k=0
m=0
a=0
b=length
l1=1
while l1==1:
if k==20000:
print b,"/",len(pxmap)
k=0
pxmap[a:b] = sorted(pxmap[a:b])
a=b
k=k+1
b=b+length
if b>=len(pxmap):
b=len(pxmap)
l1=0
pxmap = pxmap[0:a] + sorted(pxmap[a:b])
#_______________________________________________________apply sort
print "\n...Applying Sort..."
j=0
k=0
l1=1
while l1==1:
if j%100==0:
print j,"/",y
i=0
l2=1
while l2==1:
r=pxmap[k][1]
g=pxmap[k][2]
b=pxmap[k][3]
img[i,j]=(r,g,b)
i=i+1
k=k+1
if i==x:
l2=0
j=j+1
if j==(y-1):
l1=0
#_______________________________________________________save
print "\n\n\n\nSaved as divisiblesort.png"
print "\nTurn resulting image 90 degrees and re-run program for cross-hatch sort. Trust me. It looks cool. \n[must rename resulting image to 'test.jpg' or change code to open 'divisiblesort.png'"
source.save("divisiblesort.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment