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

From XPUB & Lens-Based wiki
< User:Eleanorg‎ | 1.2/Forbidden Pixels
Revision as of 14:15, 19 March 2012 by Eleanorg (talk | contribs) (Created page with "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, thi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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"