Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import sys,os, infostream, datetime
- try:
- workpath = sys.argv[1] #<--THIS NEEDS TO BE A DIRECTORY
- dimensions = eval(sys.argv[2]) #<-- TUPLE WITH *NO* SPACES : (100,515)
- if not os.path.isdir(workpath): raise Exception, "Not a directory."
- except Exception, e:
- print infostream.info("Usage: python blend.py <path> <tuple>\nex: 'python blend.py pics (500,500)'", 5)
- print infostream.info(str(e), 5)
- sys.exit(0)
- print infostream.info("Resolution: "+str(dimensions), 1)
- def avAll(object):
- """LIST OF TUPLES(LEN=3)"""
- a,b,c=[],[],[]
- a.append([i[0] for i in object])
- b.append([i[1] for i in object])
- c.append([i[2] for i in object])
- a = [sum(i)/len(i) for i in a]
- b = [sum(i)/len(i) for i in b]
- c = [sum(i)/len(i) for i in c]
- return (a,b,c)
- files = [i for i in os.listdir(workpath) if ".gif" not in i] #PIL hates .gifs for some reason, raises an exception, sorry
- def stream(n):
- return round((0.000001*n + 2.301186)*1.5, 2) #Estimate Time
- def product(cont):
- base = 1
- for e in cont:
- base *= e
- return base
- on = True
- while on:
- b = raw_input(infostream.info(str(len(files))+" files detected. Total Pixels to generate: "+str(len(files)*product(dimensions))+"\n\n*Estimated* time to render: "+str(stream(len(files)*product(dimensions)))+" seconds.\nContinue? (Y/N)", 3)).lower()
- if b in ('y', 'n'):
- if b == 'n':
- on = False
- print infostream.info("Stopping...", 5)
- sys.exit(0)
- else:
- on = False
- ta1 = datetime.datetime.now()
- try:
- print infostream.info("Gathering Images from directory.", 1)
- pix = []
- for i in files:
- pic1 = Image.open(workpath+'/'+i).resize(dimensions)
- print infostream.info("Processing Image: "+str(i), 1)
- p1dat = pic1.getdata()
- pix.append(p1dat)
- print infostream.info("Beginning render...", 1)
- print infostream.info("Indexing pixels...", 1)
- vecs = zip(*pix)
- print infostream.info("Merging color data...", 1)
- total = [avAll(i) for i in vecs]
- def fix(a):
- t = []
- for i in a:
- t2 = []
- for z in i:
- t2.append(z[0])
- t.append(tuple(t2))
- return t
- print infostream.info("Creating vectors from pixel data...", 1)
- total = fix(total)
- img = Image.new('RGB', dimensions, "white")
- img.putdata(total)
- img.show()
- print infostream.info("Render complete.", 6)
- print infostream.info("Saving image...", 1)
- img.save(workpath+'/'+"Generated_Image.png")
- print infostream.info("Save complete.", 6)
- ta2 = datetime.datetime.now()
- tot = (ta2-ta1).total_seconds()
- print infostream.info("Operation completed in: "+str(tot)+" seconds.", 6)
- except Exception, e:
- print infostream.info(str(e), 5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement