# # Constants for the size of the image # WIDTH = 600 HEIGHT = 400 # # Constants for the position of RED, GREEN and BLUE within the list of color # components # RED = 0 GREEN = 1 BLUE = 2 # # A function that displays an image # # Parameters: # x,y: The location of the upper left corner of the image # img: The image to draw # # Returns: nothing # def displayImage(x,y,img): # # Since the outer list is a list of columns, len(img) is the width # of the image. # # Assuming that each column has the same number of rows in it, len(img[0]) # is the height of the image. # # The pixels command expects the data to come in row major order, meaning # all the pixels from row 0, then row 2, ... # print "pixels",x,y,len(img),len(img[0]), # comma to prevent newline for j in range(0,len(img[0])): for i in range(0,len(img)): print int(img[i][j][RED]),int(img[i][j][GREEN]),int(img[i][j][BLUE]), print "" def displayImage2(x,y,img): # # Since the outer list is a list of columns, len(img) is the width # of the image. # # Assuming that each column has the same number of rows in it, len(img[0]) # is the height of the image. # # Output instructions of the form # color r,g,b # pixel x,y # drawing the picture from top to bottom # print "flush false" for j in range(0, len(img[0])): for i in range(0, len(img)): print "color",img[i][j][RED],img[i][j][GREEN],img[i][j][BLUE] print "pixel",x+i,x+j print "refresh" print "flush true" # # Create a new blank image # img = [] for i in range(0, WIDTH): img.append([]) # Add a new column to the image for j in range(0, HEIGHT): img[i].append([0, 0, 0]) # Add each row to column i # # Set the pixels in the image to a nice color gradient # for i in range(0, WIDTH): for j in range(0, HEIGHT): img[i][j] = [255 * j / HEIGHT, 192 * j / HEIGHT, 64 * j / HEIGHT] #displayImage(100,100,img) displayImage2(100,100,img)