User:Eleanorg/1.2/Forbidden Pixels/composing image with ImageMagick: Difference between revisions

From XPUB & Lens-Based wiki
Line 24: Line 24:
#-*- coding:utf-8 -*-
#-*- coding:utf-8 -*-
   
   
import random
   
   
# this is bash syntax which is just printed out; run with python and pipe into bash
# this is bash syntax which is just printed out; run with python and pipe into bash

Revision as of 23:10, 28 March 2012

As per Michael's advice, I've dispensed with making system commands from within the python script. Eventually, figure out how to use the PyhtonMagick interface - but for now, this script is just printing out bash commands which use Imagemagick. Run the script with Python, then pipe to bash to execute.

Drawing pixels onto a grey image

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

import random

# this is bash syntax which is just printed out; run with python and pipe into bash

print "convert -size 640x480 xc:grey -fill 'rgb(255,0,0)' \\"    # creates a new image of size 640x480, bg colour grey, fg colour red. \\ prints out the bash newline character              
for i in range(100):                                             # creates 100 randomly spaced red pixels over the grey background
  y = random.randint(0,480)
  x = random.randint(0,640)
  print "-draw 'point %d,%d' \\" %(x,y)                          # %d means a digit from the range given in the parentheses at the end - which points to variables x and y
print "drawn.png"

Adding pixels to an existing image

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
 
# this is bash syntax which is just printed out; run with python and pipe into bash

color = "rgb(255, 255, 255)"
x = 300
y = 200

# adds a pixel to pre-existing image, drawn.png
print "convert drawn.png -fill ' " + color + " ' \\"   
print "-draw 'point %d,%d' \\" %(x,y)                         
print "drawn.png"

Drawing pixels based on scraped data

This script goes to a specified url, finds the magic string, scrapes it and draws the appropriate pixel onto a new .png image. fuck yeh! next: figure out how to add pixels onto the same file; this script currently overwrites any preexisting file with the same name.

Run with python and pipe to bash to execute. Next step: how to run imagemagick commands via a cgi script?

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

import re, urllib2

 
# scrapes pixel data and pushes it to imagemagick
 

#------------- get URL -------------------#

url = "http://ox4.org/~nor/trials/hostedString.html"

#------------- scrape webpage----------------------------#

text = urllib2.urlopen(url).read()		# reads page at the specified URL

#------------- draw on a pixel -------------------------#

pat = r"Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\))"

if not re.search(pat, text):			# if pattern 'pat' isn't matched within 'text'
    print "nothing found"
else:
    for m in re.finditer(pat, text):		# for each match in text
        string = m.group(0)                     # m.group() lets you access data within the match object
	xPos = int(m.group(1))			# note: m.group(1) is first capture () - m.group(0) is always whole string
	yPos = int(m.group(2))
	color = m.group(3)

	print "convert -size 640x480 xc:black -fill ' " + color + " ' \\"
	print "-draw 'point %d,%d' \\" %(xPos,yPos)  
        print "drawn2.png"