summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmakePattern.py106
-rw-r--r--newimagefromjson.py32
2 files changed, 138 insertions, 0 deletions
diff --git a/makePattern.py b/makePattern.py
new file mode 100755
index 0000000..d460865
--- /dev/null
+++ b/makePattern.py
@@ -0,0 +1,106 @@
+#!/usr/bin/python2.7
+
+import sys
+import cgi
+from os import getpid, path
+from subprocess import call, Popen, PIPE
+import time
+
+MAIN_DIRECTORY = ""
+BIN_CONVERT = "convert"
+BIN_IDENTIFY = "identify"
+BIN_COMPOSITE = "composite"
+ACCEPTABLE_FILE_TYPES = [".png", ".jpg", ".gif", ".jpeg"]
+
+def usage():
+ sys.stderr.write("$>makePattern.py [ input_file ] [ pattern_file ] [ username ]");
+
+def now():
+ return str(int(time.time()))
+
+def image_dimensions_and_test(filename):
+ ident = Popen([BIN_IDENTIFY, filename], stdout=PIPE).communicate()[0]
+ partz = ident.split(" ")
+ filetype = "."+partz[1]
+ size = partz[6]
+ if filetype.lower() not in ACCEPTABLE_FILE_TYPES:
+ error("file was not an image")
+ return partz[2].split("x")
+
+def error(s):
+ " returns an error and exits the program "
+ print("ERROR: "+s)
+ exit(1)
+
+def hexdir(filename):
+ " creates a two-letter directory name "
+ return sha1(filename.encode()).hexdigest()[:2]
+
+class Pattern:
+ def __init__(self):
+ self.nametag = "imPattern";
+ self.pid = str(getpid())
+ self.pattern_file = "";
+ self.original_file = "";
+ self.username = "";
+
+ def makeResultFilename(self):
+ file_base, extension = path.splitext(self.original_file)
+ if len(file_base) > self.MAX_NAME_LENGTH:
+ file_base = file_base[0:self.MAX_NAME_LENGTH]
+ return "%s_%s_%s%s%s" % (file_base, self.username, self.nametag, now(), extension)
+
+ #first step
+ def makeCanvas(self):
+ call([BIN_CONVERT,"-size",self.dimensions[0]+"x"+self.dimensions[1],"canvas:transparent", self.canvas_file])
+
+ #second step use the Canvas as a background
+ def makeMask(self):
+ #tile the pattern pattern on the canvas
+ call([BIN_COMPOSITE,"-tile", self.pattern_file, self.canvas_file, self.mask_file])
+
+ #fuse the tiled file to create a mask
+ #convert thebg.gif -compose Dst_In null: thefile.gif -matte -layers composite new.gif
+ call([BIN_CONVERT, self.mask_file, "-compose", "Dst_In", "null:", self.original_file, "-matte", "-layers", "composite", self.mask_file])
+
+ #cleanup
+ call(["rm", self.canvas_file])
+
+ #third step
+ def fuseMask(self, fuse_mode="Pin_Light"):
+ call([BIN_CONVERT, self.mask_file, "null:", self.original_file, "-matte", "-compose", fuse_mode, "-layers", "composite", self.result_file])
+ call(["rm", self.mask_file])
+
+ def main(self, input_file_path, pattern_file, username=""):
+ self.pattern_file = pattern_file;
+ self.original_file = input_file_path;
+ self.username = username;
+ self.MAX_NAME_LENGTH = 10;
+
+ self.image_format = self.original_file.split(".")[-1]
+ self.dimensions = ""
+ try:
+ self.dimensions = image_dimensions_and_test(self.original_file);
+ except Exception as e:
+ sys.stderr.write(str(e));
+ error("Unable to determine dimensions")
+ self.canvas_file = self.pid+"blank_canvas.png";
+ self.makeCanvas();
+ self.mask_file = self.pid+"mask_file"+"."+self.image_format;
+ self.makeMask();
+ self.result_file = self.makeResultFilename();
+ self.fuseMask();
+
+if __name__ == "__main__":
+ p = Pattern();
+ if len(sys.argv) < 2 or len(sys.argv) > 3:
+ usage();
+ sys.exit(0);
+ input_file_path = sys.argv[1]
+ pattern_file = sys.argv[2]
+ username = "";
+# username = sys.argv[3]
+ print input_file_path;
+ print pattern_file;
+ print username;
+ p.main(input_file_path, pattern_file, username);
diff --git a/newimagefromjson.py b/newimagefromjson.py
new file mode 100644
index 0000000..25fdfcd
--- /dev/null
+++ b/newimagefromjson.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python2.7
+import simplejson as json
+import Image
+
+f = open("jsonfile", 'r');
+myjson = f.read();
+f.close();
+
+specs = json.loads(myjson);
+img = Image.new('RGBA', (int(specs.width), int(specs.height), "black"));
+
+def boolToColor(boolean):
+ if boolean:
+ return (0,0,0,255);
+ else:
+ return (255,255,255,0)
+
+pixels = img.load();
+for i in specs.matrix:
+ for j in specs.matrix[i]:
+ pixels[i,j] = boolToColor(specs.matrix[i][j]);
+
+
+img = Image.new( 'RGBA', (500,500), "black") # create a new black image
+pixels = img.load() # create the pixel map
+
+
+for i in range(img.size[0]): # for every pixel:
+ for j in range(img.size[1]):
+ pixels[i,j] = (255, 255, 255, 255) # set the colour accordingly
+
+img.save("myimage.png", "PNG")