# MCS 260 Project Four due Wed 7 Nov 2007 : randgray.py # # A postscript picture with random grayscales. # # This program illustrates what we will need to make # general grayscale pictures. A grayscale is a number # between 0 and 255: 0 is black, 255 is white. # We define a postscript picture by writing grayscales # for each pixel in hexadecimal notation. def write_postscript(name,rows,columns): """ Writes to a file with the given name a random picture in postscript format, with given number of rows and columns, using randomly generated gray scales. """ import random XOFFSET = 54 YOFFSET = 288 SCALE = 432 file = open(name + '.ps','w') file.write('%!PS\n') file.write('%%BoundingBox: 0 300 500 720\n') file.write('/picstr %d string def\n' % rows) file.write('%d %d translate\n' % (XOFFSET,YOFFSET)) file.write('%d %d scale\n' % (SCALE,SCALE)) file.write('%d %d %d\n' % (columns,rows,8)) file.write('[%d 0 0 -%d 0 %d]\n' % (columns,rows,rows)) file.write('{currentfile picstr readhexstring pop}\n') file.write('image\n') for i in range(0,rows): for j in range(0,columns): n = random.randint(0,255) file.write('%.2x' % n) file.write('\n') file.write('showpage\n') file.close() def main(): """ Prompts the user for a file name, the number of rows and columns of the postscript file. Recommended values for rows and columns are 100. """ print 'generating a random grayscale picture' name = raw_input('Give file name : ') rows = input('Give #rows : ') cols = input('Give #columns : ') print 'do ghostview on ' + name + '.ps' write_postscript(name,rows,cols) main()