User:Eleanorg/1.3/Dissolute Image/Code1: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 51: Line 51:
   
   
getcolors()
getcolors()
</source>
==Give user an unadopted pixel==
Finds all the pixels in the db with no url associated. Picks one at random and gives it to the user, with an input form for them to enter the url where they have put it. #TODO needs to be made into a .cgi script.
<source lang="python">
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo, random 
from pymongo import Connection
## grab a random unadopted pixel from the database and give user a unique string for it
connection = Connection()
myDB = connection['pixelDatabaseTest4']
#--------- some db queries for testing------------#
# print whole db thus:
#for entry in myDB.collection.find():
#    print entry
#myDB.collection.update( {"id":"2"}, {"$set":{"url": "ovulation.com"}}) # update url of entry with id '2'
#print myDB.collection.find({"id": "2"})["color"]
#--------- generate pixel string ------------#
# find all empty urls in a loop:
unadopted = []
for entry in myDB.collection.find({"url": ""}):
    unadopted.append(entry) # add this pixel's hash as an item to the 'unadopted' dictionary
howMany = len(unadopted) # find out how many items in 'unadopted' list
random = random.randint(0,howMany) # pick a random number in this range
pixelHash = unadopted[random] # ...and choose the pixel at this index
print pixelHash['color']
pixelString = "I'm adopting pixel " + pixelHash['id'] + " of 10,000 at position " + pixelHash['xpos'] + "," + pixelHash['ypos'] + " with color " + pixelHash['color']
print pixelString
#--------- print input form with pixel string ------------#
htmlHeader = """<!DOCTYPE html>
<html>
  <head>
    <title>Adopt a Pixel</title>
        <link <link rel="stylesheet" href="../../pixels.css">
  </head>
  <body>"""
print "Content-Type: text/html"
print
print htmlHeader
print """
<div class="centred" style="margin-top:50px;">
Below is the unique identifier for your pixel.
Copy and paste it somewhere public on the interwebs, exactly as it appears:
</div>
<div class="container txt">
<span class="pixelString">""" +
  pixelString +
"""</span>
</div>
<div class="centred" style="margin-top:50px;">
Then tell us the URL of the page where you pasted it:
</div>
<div class="container txt">
  <form action="saveUrl.cgi" name="inputForm"> 
    <span class="containerText">Paste url: </span>
    <input name="url">
    <input type="submit" value="OK">
  </form>
</div>
<div class="centred">
Your pixel will appear in the image for as long as the text remains at this URL.
</div>
</body>
</html>"""
</source>
</source>

Revision as of 21:18, 7 June 2012

Make database of pixels

Splits up image into pixels with imageMagick, then inserts each one as a hash in a Mongo database. Shown here with dummy values for testing, imagemagick bits commented out.

#!/usr/bin/python
#-*- coding:utf-8 -*-

# this script grabs pixel data from imagemagick & creates the mongo database of pixels. to be run once with python.

import pymongo  #the library that serves as python interface to mongo database system
from pymongo import Connection


#--------- create database -----------------------#	

connection = Connection()
myDB = connection['pixelsDatabaseTest1']
collection = myDB.collection  					#tables in Mongo are called 'collections'. variable name on the left can be anything.


#--------- determine image dimensions-----------------------#	
 
#image = "20by10PixelsSection1.png"
#sizeQuery = "identify -format '%w %h' " + image		# system command gets image dimensions with ImageMagick
#size = os.popen(sizeQuery, 'r').read()				# captures output of system call
#(width, height) = size.split()					# splits ImageMagick output into separate column & row no's
#columns = int(width)
#rows = int(height)

columns = 5
rows = 2
 
#--------- assign pixel details to database-----------------#		
 # RUN ONCE ONLY

def getcolors ():
    n = 0
    for row in range (0, rows):					# for each row in the image...		
        for column in range (0, columns):			# for each column in the row...
#            colorQuery = "convert " + image + " -format '%[pixel:p{" + str(column) + "," + str(row) + "}]' info:-"
#            colorResult = os.popen(colorQuery, 'r').read()	# makes & captures output of system call
#            color = re.sub("\n", "", colorResult)		# strips newline character from end of sys call result
            color = "red"
            n = (n + 1)						# this counter increments by one on each loop, generating a unique pixel id
            pixelID = str(n)					
            pixelName = "pixel" + pixelID			# creates a unique name from the pixel ID, as a handle for this pixel in the database. warning: db has no qualms about creating duplicates if this code is run more than once; it assigns its own unique id string to each entry
            #string = "I'm adopting pixel " + str(pixelID) + " at position " + str(column) + "," + str(row) + "; color " + color
            pixelName = {'id': n , 'xpos': str(column) , 'ypos': str(row) , 'color': color, 'url': ""}
            #print pixelName
            collection.insert(pixelName)			# insert this pixel into the db
 
getcolors()

Give user an unadopted pixel

Finds all the pixels in the db with no url associated. Picks one at random and gives it to the user, with an input form for them to enter the url where they have put it. #TODO needs to be made into a .cgi script.

#!/usr/bin/python
#-*- coding:utf-8 -*-

import pymongo, random  
from pymongo import Connection

## grab a random unadopted pixel from the database and give user a unique string for it

connection = Connection()
myDB = connection['pixelDatabaseTest4']


#--------- some db queries for testing------------#

# print whole db thus:
#for entry in myDB.collection.find():
#    print entry

#myDB.collection.update( {"id":"2"}, {"$set":{"url": "ovulation.com"}}) # update url of entry with id '2'

#print myDB.collection.find({"id": "2"})["color"]

#--------- generate pixel string ------------#

# find all empty urls in a loop:
unadopted = []
for entry in myDB.collection.find({"url": ""}):
    unadopted.append(entry)			# add this pixel's hash as an item to the 'unadopted' dictionary

howMany = len(unadopted)			# find out how many items in 'unadopted' list
random = random.randint(0,howMany)		# pick a random number in this range
pixelHash = unadopted[random]			# ...and choose the pixel at this index
print pixelHash['color']

pixelString = "I'm adopting pixel " + pixelHash['id'] + " of 10,000 at position " + pixelHash['xpos'] + "," + pixelHash['ypos'] + " with color " + pixelHash['color']
print pixelString


#--------- print input form with pixel string ------------#

htmlHeader = """<!DOCTYPE html>
<html>
  <head>
    <title>Adopt a Pixel</title>
        <link <link rel="stylesheet" href="../../pixels.css">
  </head>
  <body>"""
 
 
print "Content-Type: text/html"
print 
print htmlHeader
print """
<div class="centred" style="margin-top:50px;">
Below is the unique identifier for your pixel.
Copy and paste it somewhere public on the interwebs, exactly as it appears:
</div>
<div class="container txt">
 
<span class="pixelString">""" +
  pixelString +
"""</span>
</div>
<div class="centred" style="margin-top:50px;">
Then tell us the URL of the page where you pasted it:
</div>
<div class="container txt">
  <form action="saveUrl.cgi" name="inputForm">  
    <span class="containerText">Paste url: </span>
    <input name="url">
    <input type="submit" value="OK">
  </form>
</div>
<div class="centred">
Your pixel will appear in the image for as long as the text remains at this URL.
</div>
 
</body>
</html>"""