Advertisement
Peaser

blend

Aug 1st, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. from PIL import Image
  2. import sys,os, infostream, datetime
  3.  
  4. try:
  5.     workpath = sys.argv[1] #<--THIS NEEDS TO BE A DIRECTORY
  6.     dimensions = eval(sys.argv[2]) #<-- TUPLE WITH *NO* SPACES : (100,515)
  7.     if not os.path.isdir(workpath): raise Exception, "Not a directory."
  8. except Exception, e:
  9.     print infostream.info("Usage: python blend.py <path> <tuple>\nex: 'python blend.py pics (500,500)'", 5)
  10.     print infostream.info(str(e), 5)
  11.     sys.exit(0)
  12. print infostream.info("Resolution: "+str(dimensions), 1)
  13. def avAll(object):
  14.     """LIST OF TUPLES(LEN=3)"""
  15.     a,b,c=[],[],[]
  16.     a.append([i[0] for i in object])
  17.     b.append([i[1] for i in object])
  18.     c.append([i[2] for i in object])
  19.     a = [sum(i)/len(i) for i in a]
  20.     b = [sum(i)/len(i) for i in b]
  21.     c = [sum(i)/len(i) for i in c]
  22.     return (a,b,c)
  23. files = [i for i in os.listdir(workpath) if ".gif" not in i] #PIL hates .gifs for some reason, raises an exception, sorry
  24. def stream(n):
  25.     return round((0.000001*n + 2.301186)*1.5, 2) #Estimate Time
  26. def product(cont):
  27.   base = 1
  28.   for e in cont:
  29.     base *= e
  30.   return base
  31. on = True
  32.  
  33. while on:
  34.     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()
  35.     if b in ('y', 'n'):
  36.         if b == 'n':
  37.             on = False
  38.             print infostream.info("Stopping...", 5)
  39.             sys.exit(0)
  40.         else:
  41.             on = False
  42. ta1 = datetime.datetime.now()
  43. try:
  44.  
  45.     print infostream.info("Gathering Images from directory.", 1)
  46.     pix = []
  47.     for i in files:
  48.         pic1 = Image.open(workpath+'/'+i).resize(dimensions)
  49.         print infostream.info("Processing Image: "+str(i), 1)
  50.         p1dat = pic1.getdata()
  51.         pix.append(p1dat)
  52.     print infostream.info("Beginning render...", 1)
  53.     print infostream.info("Indexing pixels...", 1)
  54.     vecs = zip(*pix)
  55.     print infostream.info("Merging color data...", 1)
  56.     total = [avAll(i) for i in vecs]
  57.     def fix(a):
  58.         t = []
  59.         for i in a:
  60.             t2 = []
  61.             for z in i:
  62.                 t2.append(z[0])
  63.             t.append(tuple(t2))
  64.         return t
  65.     print infostream.info("Creating vectors from pixel data...", 1)
  66.     total = fix(total)
  67.     img = Image.new('RGB', dimensions, "white")
  68.     img.putdata(total)
  69.     img.show()
  70.     print infostream.info("Render complete.", 6)
  71.     print infostream.info("Saving image...", 1)
  72.     img.save(workpath+'/'+"Generated_Image.png")
  73.     print infostream.info("Save complete.", 6)
  74.     ta2 = datetime.datetime.now()
  75.     tot = (ta2-ta1).total_seconds()
  76.     print infostream.info("Operation completed in: "+str(tot)+" seconds.", 6)
  77. except Exception, e:
  78.     print infostream.info(str(e), 5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement