Created
March 19, 2016 19:12
-
-
Save evstraw/a84ac035d2186ae7e058 to your computer and use it in GitHub Desktop.
Program to "glitch-ify" images using the PIL library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PIL, sys, random, os, time | |
from PIL import Image | |
usage = "python glitch.py <image path>" | |
start = time.time() | |
glitchfactor_x = .3 | |
glitchlen_x = .1 | |
glitchchance = .8 | |
if len(sys.argv) < 2: | |
print "Error: Please pass the path to an image as an argument." | |
print usage | |
sys.exit(1) | |
else: | |
path = sys.argv[1] | |
def getimagematrix(image): | |
ibbox = image.getbbox() | |
iw = bbox[2] | |
ih = bbox[3] | |
ls = list(image.getdata()) | |
im_matrix = [0 for x in range(0,ih)] | |
for i in range(0,ih): | |
row = [0 for x in range(0,iw)] | |
for j in range(0,iw): | |
row[j] = ls[(i*iw)+j] | |
im_matrix[i] = row | |
return im_matrix | |
def getlist(mat): | |
rows = len(mat) | |
cols = len(mat[0]) | |
list = [0 for x in range(0,rows*cols)] | |
for y in range(0,rows): | |
for x in range(0,cols): | |
list[(y*cols)+x] = mat[y][x] | |
return list | |
try: | |
im = Image.open(path) | |
format = im.format | |
except IOError: | |
print "Error: Could not open image. File may not exist." | |
sys.exit(2) | |
bbox = im.getbbox() | |
w = bbox[2] | |
h = bbox[3] | |
maxglitch_x = int(w*glitchfactor_x) | |
maxglitchlen_x = int(h*glitchlen_x) | |
matrix = getimagematrix(im) | |
newmatrix = getimagematrix(im) | |
glitching = False | |
glitchlen = 0 | |
glitchamount = 0 | |
glitchpos = 0 | |
for i in range(0,h): | |
pct = int((float(i)/float(h))*100) | |
sys.stdout.write("\rGlitchifying... "+str(i+1)+"/"+str(h)+"("+str(pct)+"%)") | |
if glitching==True: | |
row = matrix[i] | |
newrow = [0 for x in range(0,w)] | |
for x in range(0,w): | |
ofsx = x+glitchamount | |
if ofsx >= w: | |
ofsx = ofsx-w | |
newrow[x] = row[ofsx] | |
newmatrix[i] = newrow | |
if glitchpos == (glitchlen-1): | |
glitching = False | |
else: | |
glitchpos = glitchpos+1 | |
else: | |
willglitch = random.randint(0,int(((1-glitchchance)*100))) | |
if willglitch == 0: | |
glitching = True | |
glitchlen = random.randint(1,maxglitchlen_x) | |
glitchamount = random.randint(1,maxglitch_x) | |
glitchpos = 0 | |
else: | |
glitching = False | |
im.putdata(getlist(newmatrix)) | |
currenttime = time.time() | |
length = currenttime-start | |
sys.stdout.write("\nDone! ("+"{0:.3f}".format(length)+" seconds)\n") | |
filesplit = os.path.splitext(sys.argv[1]) | |
filename = "" | |
foundname = False | |
ofs = 0 | |
while foundname == False: | |
if ofs==0: | |
filename = filesplit[0]+"_glitch"+filesplit[1] | |
else: | |
filename = filesplit[0]+"_glitch"+str(ofs)+filesplit[1] | |
if os.path.isfile(filename): | |
ofs = ofs+1 | |
else: | |
foundname = True | |
im.save(filename) | |
print "File has been saved to "+filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment