Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Wednesday, 15 July 2009

Generating Multiple Images from One Rule

There are some cfdg rule files that you might want to run a number of times so I've created a little python script to automate the process.
   1:#!/usr/bin/env python2.5
2:'''
3:Utility to call cfdg file
4:'''
5:import sys, getopt
6:from subprocess import Popen
7:from subprocess import PIPE
8:# set following variables to suit your environment
9:cfdg = "/usr/bin/cfdg"
10:temp_folder = "/home/tux/tmp/"
11:start_file = "start.cfdg" # default file
12:kid_folder = "kids/"
13:empty_count = 0
14:
15:def help_message():
16: '''handle usage'''
17: print '''run_rules.py [cfgd file]
18:Options --help --displays this help message
19: -w [width]
20: -h [height]
21: --width=[width]
22: --height=[height]'''
23: return 0
24:
25:def call_cfdg(cfdg_file, count = 0, width=500, height=500):
26: """
27: Call cfdg file cfdg file.cfdg file.png
28: """
29: out = temp_folder + kid_folder + str(count) + '.png'
30: p1 = Popen([cfdg, '-w', str(width), '-h', str(height), cfdg_file, out], stdout=PIPE)
31: p2 = Popen(['grep', ' 0 '], stdin=p1.stdout, stdout=PIPE)
32: output = p2.communicate()[0]
33: if len(output) > 3:
34: # no shapes have been produced so delete png file and try again
35: global empty_count
36: empty_count += 1
37: p3 = Popen(["rm", out], stdout=sys.stderr)
38: while empty_count < 50:
39: call_cfdg(cfdg_file, count, width, height)
40: # call cfdg again until we reach an empty_count of 50
41: p1.wait()
42:
43:def check_args(a_list, width, height):
44: cfdg_file = start_file # default cfdg file
45: if len(a_list) == 1:
46: cfdg_file = a_list[0]
47: for i in range(0, 10):
48: if width == 0:
49: call_cfdg(cfdg_file, i) # use default
50: else:
51: call_cfdg(cfdg_file, i, width, height)
52:
53:def main():
54: '''
55: Is the main function
56: '''
57: sys.setrecursionlimit(100) # safety net
58: width = 0
59: height = 0
60: try:
61: options, args = getopt.getopt(sys.argv[1:], 'w:h:', ['help', 'width=', 'height='])
62: for a in options[:]:
63: if a[0] == '--help' and a[1] == '':
64: options.remove(a)
65: help_message()
66: sys.exit(0)
67: for a in options[:]:
68: if a[0] == '-w' and a[1] != '':
69: options.remove(a)
70: width = a[1]
71: break
72: if a[0] == '--width' and a[1] != '':
73: options.remove(a)
74: width = a[1]
75: break
76: for a in options[:]:
77: if a[0] == '-h' and a[1] != '':
78: options.remove(a)
79: height = a[1]
80: break
81: if a[0] == '--height' and a[1] != '':
82: options.remove(a)
83: height = a[1]
84: break
85: check_args(args, width, height)
86: except getopt.GetoptError:
87: sys.stdout = sys.stderr
88: help_message()
89: except RuntimeError:
90: sys.stdout = sys.stderr
91: return 0
92:
93:if __name__ == "__main__":
94: sys.exit(main())


The main feature of this script is it checks for images with no shapes, and re-runs script up to 50 times (500 in total as a maximum). Perhaps a bit excessive but you should either end up with 10 images with at least one shape or no images. You can also specify width and height, however be sure to know how complicated the rules are, it can take quite a time to run some rules, also other rules have no real random features. Good examples to run are trees, where the random nature of the branches makes it worth running the scripts several times.

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2