User:Eleanorg/1.2/Forbidden Pixels/composing image with ImageMagick

From XPUB & Lens-Based wiki

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"

Adding pixels to an image from URLs in a text file

This script prints bash syntax which adds pixels to an image based on data scraped from URLs in a text file. Pipe to bash to run.

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

import cgi, re, urllib2
import cgitb; cgitb.enable()

# reads submitted urls from the text file where they're stored, scraping each one and adding a corresponding pixel to the image.
# again, just prints bash commands - pipe to bash to run


# open image to be edited
print "convert drawn.png \\"

# open urls file
f = open("data/urls.txt")

# for each line in file, add a pixel
for line in f:
   url = line
   text = urllib2.urlopen(url).read()		# reads page at the specified URL
   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"			# really i want "do nothing"/skip to next here ...?
   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 "-fill ' " + color + " ' \\"  	# this part adds a pixel to the image
           print "-draw 'point %d,%d' \\" %(xPos,yPos)
                
#save new version of the image; close urls file
print "updated.png"			
f.close()