summaryrefslogtreecommitdiff
path: root/other_script.py
blob: a057646af588d30ea2257c03326ce069b59e58c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python

import downloader
import filenamer
import inventnamer
import fieldstorager
import hexdirer
import cgi
from subprocess import Popen, PIPE
import os
import sys

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"

PARAM_LIST = "theurl thedither username format"

form = cgi.FieldStorage()
newvalues = fieldstorager.fieldstorage(PARAM_LIST, form)
thekeys = PARAM_LIST.split()
for key in thekeys:
  globals()[key] = newvalues[key]

if username == None:
	username = ""

###################################
### TEST WITH VALUES HERE
theurl = "http://asdf.us/im/10/friedeggstresstoy_1321140711_1322282769_pepper.gif"
thedither = "1.png"
username = "pepper"
format = "png"
###################################
if format == None:
	format = "png"	

class Dither:
	def file_size (self, file):
		return os.stat(file)[6]
	def identify(self, thefile):
		ident = Popen([BIN_IDENTIFY, thefile], stdout=PIPE).communicate()[0]
		partz = ident.split(" ")
		return partz[2].split("x")
	def makeCanvas(self, dimensions, thefile):
		os.system(BIN_CONVERT+" -size "+self.dimensions[0]+"x"+self.dimensions[1]+" canvas:transparent "+thefile)
	def makeMask(self, themask, thedither, thebackground):
		os.system(BIN_COMPOSITE+"  -tile "+thedither+"   "+thebackground+"  "+themask)
		#convert thebg.gif -compose Dst_In null: thefile.gif -matte -layers composite new.gif
		os.system(BIN_CONVERT+" "+themask+" -compose Dst_In null: "+self.mainfile+" -matte -layers composite "+themask)
	#	os.system("rm "+thebackground)
	def fuseMask(self, themask, theimage):
		os.system(BIN_COMPOSITE+" "+theimage+" -compose Pin_Light "+themask+" "+theimage)
	#	os.system("rm "+themask)
	def __init__(self, theurl, thedither, username):
		self.mainfile = filenamer.filename(theurl, "imDither", username)
		filepaths = hexdirer.hexdir()
		self.privatepath = filepaths[0]
		self.publicpath = filepaths[1]
		self.privatepath = "" #
		downloader.download(theurl, self.mainfile, self.privatepath)
		parts = self.mainfile.split('.')
		print parts
		if "gif" in parts[-1]:
			canvastype = "gif"
		else:
			canvastype = "png"
		self.canvasfile = inventnamer.inventname(canvastype, "canvasfile")
		self.dimensions = self.identify(self.mainfile)
		self.makeCanvas(self.dimensions, self.canvasfile)
		self.theMask = inventnamer.inventname(canvastype, "maskfile")
		self.makeMask(self.theMask, thedither, self.canvasfile)
		self.fuseMask(self.theMask, self.mainfile)
		#self.mainfile = self.mainfile.replace(, do something with s3)
		print self.mainfile
		print self.file_size(self.mainfile)
		print 'width: '+self.dimensions[0]+'px'
		print 'height: '+self.dimensions[1]+'px'
daddy = Dither(theurl, thedither, username)