summaryrefslogtreecommitdiff
path: root/im/cgi-bin/imdither.cgi
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-02-13 02:42:02 -0800
committeryo mama <pepper@scannerjammer.com>2015-02-13 02:42:02 -0800
commitfd640b170a64584fd9c295be53c91972ff9f9ec1 (patch)
treec67e95b332e183bbf14065bba55dd77e86a71fbe /im/cgi-bin/imdither.cgi
parent64f41d53728a966f10aef6d7ffbc00853d754300 (diff)
fixed some basics
Diffstat (limited to 'im/cgi-bin/imdither.cgi')
-rwxr-xr-xim/cgi-bin/imdither.cgi145
1 files changed, 145 insertions, 0 deletions
diff --git a/im/cgi-bin/imdither.cgi b/im/cgi-bin/imdither.cgi
new file mode 100755
index 0000000..29837ed
--- /dev/null
+++ b/im/cgi-bin/imdither.cgi
@@ -0,0 +1,145 @@
+#!/usr/bin/python
+
+import downloader
+import time
+import re
+import cgi
+from subprocess import Popen, PIPE, call
+from os import stat, path, makedirs
+import sys
+import sha
+BASE_DIR = ""
+OTHER_BASE = ""
+#BASE_DIR = "/var/www/asdf.us/httpdocs/im/"
+#OTHER_BASE = "http://asdf.us/im/"
+
+print "Content-Type: text/html"
+print ""
+print ""
+
+NAMETAG = "imDither"
+
+MAIN_DIRECTORY = ""
+#BIN_CONVERT = "/usr/bin/convert"
+#BIN_IDENTIFY = "/usr/bin/identify"
+#BIN_COMPOSITE = "/usr/bin/composite"
+BIN_CONVERT = "convert"
+BIN_IDENTIFY = "identify"
+BIN_COMPOSITE = "composite"
+
+NOW = str(int(time.time()))
+MAX_LENGTH_BEFORE_ABRIDGED = 30
+ABBREVIATION = "xx_abridged__"
+
+###################################
+### DEFAULT VALUES HERE
+params = {
+ "url" : "http://asdf.us/im/10/friedeggstresstoy_1321140711_1322282769_pepper.gif",
+ "ditherType" : "1.png",
+ "username" : "pepper",
+ "formatType" : "png",
+}
+###################################
+
+form = cgi.FieldStorage()
+if form:
+ for key, value in form.iteritems():
+ formValues[key] = params[key] #CHECKME
+
+class Utils:
+ def file_size (self, imgFile):
+ return stat(imgFile)[6]
+
+ def imageDimensions(self, thefile):
+ ident = Popen([BIN_IDENTIFY, thefile], stdout=PIPE).communicate()[0]
+ parts = ident.split(" ")
+ return parts[2].split("x")
+
+ def hash_dir(self, s):
+ """get a random number-letter pair"""
+ return sha.new(s).hexdigest()[:2]
+
+ def hexdir(self):
+ """create a random path name"""
+ directoryName = utils.hash_dir(NOW)
+
+ dirs = [
+ BASE_DIR+directoryName+'/',
+ OTHER_BASE+directoryName+'/',
+ ]
+ ###################################
+ if not path.exists(directoryName):
+ makedirs(directoryName)
+
+
+ return dirs
+
+ def makeOriginalName(self, params, canvas_type="", nametag_part=""):
+ if not nametag_part:
+ nametag_part = NAMETAG
+ if not canvas_type:
+ canvastype = params["formatType"]
+ filename = '%s_%s_%s.%s' % (nametag_part, NOW, params["username"], canvas_type)
+ return filename
+
+ def sanitize (self,string):
+ """strip out all non-word characters"""
+ return re.sub(r'\W+', '', string)
+
+ def makeNameFromFilename(self, params):
+ parts = params["url"].split("?")[0].split('/')
+ filename = parts[-1]
+ name, extension = filename.split('.')
+ if len(extension) >= 5:
+ sys.stdout.write('the file is not an image or any standard format')
+ sys.exit()
+ if len(name)> MAX_LENGTH_BEFORE_ABRIDGED:
+ name = ABBREVIATION
+ sanitized = self.sanitize(name)
+ thefilename = "%s_%s_%s_%s.%s" % (NAMETAG, NOW, sanitized, params["username"], extension)
+ return thefilename
+
+utils = Utils();
+
+
+class Dither:
+ def makeCanvas(self, thefile):
+ call([BIN_CONVERT,"-size", self.dimensions[0]+"x"+self.dimensions[1],"canvas:transparent", thefile])
+ def makeMask(self, themask, thedither, thebackground):
+ """ composite -tile DITHERNAME BACKGROUNDNAME MASKNAME """
+ call([BIN_COMPOSITE,"-tile",thedither,thebackground,themask])
+ #convert thebg.gif -compose Dst_In null: thefile.gif -matte -layers composite new.gif
+ call([BIN_CONVERT,themask,"-compose","Dst_In","null:",self.privatepath+self.mainfile,"-matte","-layers","composite",themask])
+ # os.system("rm "+thebackground)
+ def fuseMask(self, themask, theimage):
+ call([BIN_COMPOSITE,theimage,"-compose","Pin_Light",themask,theimage])
+ # os.system("rm "+themask)
+ def __init__(self, url, thedither, username):
+ self.mainfile = utils.makeNameFromFilename(params)
+ filepaths = utils.hexdir()
+ self.privatepath = filepaths[0]
+ self.publicpath = filepaths[1]
+ downloader.download(url, self.mainfile, self.privatepath)
+
+ parts = self.mainfile.split('.')
+ print parts
+ if "gif" in parts[-1]:
+ canvastype = "gif"
+ else:
+ canvastype = "png"
+
+ self.canvasfile = "/tmp/"+utils.makeOriginalName(params, canvastype, "canvasfile")
+ self.dimensions = utils.imageDimensions(self.privatepath+self.mainfile)
+ self.makeCanvas(self.canvasfile)
+ self.theMask = "/tmp/"+utils.makeOriginalName(params, canvastype, "maskfile")
+ #good up to here
+ self.makeMask(self.theMask, thedither, self.canvasfile)
+ self.fuseMask(self.theMask, self.privatepath+self.mainfile)
+ #self.mainfile = self.mainfile.replace(, do something with s3)
+ print self.privatepath+self.mainfile
+ print utils.file_size(self.privatepath+self.mainfile)
+ print 'width: '+self.dimensions[0]+'px'
+ print 'height: '+self.dimensions[1]+'px'
+
+if __name__ == "__main__":
+ newImage = Dither(params["url"], params["ditherType"], params["username"])