#import a package that works with jpg images from PIL import Image #also import ipythonblocks import ipythonblocks #create an image object using a picture of us im = Image.open('sare2.jpg') #check what the size of the image is im.size #resize, keeping the general dimensions; 1/4 of original size im = im.resize((153, 204), Image.ANTIALIAS) #thank goodness, the package we imported before can do all that work for us imdata = im.getdata() #check and see what the first (r, g, b) value is imdata[0] #we can use blocks to see what that first color is ipythonblocks.show_color(imdata[0][0], imdata[0][1],imdata[0][2]) #how many pixels are there in this image? len(imdata) import random for i in range(10): val = random.randint(0,31212) print "The random pixel is:", val ipythonblocks.show_color(imdata[val][0],imdata[val][1],imdata[val][2]) #import packages that we will need import os import itertools #this code is stuff matt wrote #I think it makes a sare2.txt file. with open('sare2.txt', 'w') as f: s = ['# width height', '{0} {1}'.format(im.size[0], im.size[1]), '# block size', '4', '# initial color', '0 0 0', '# row column red green blue'] f.write(os.linesep.join(s) + os.linesep) for ((row, col), colors) in itertools.izip(itertools.product(xrange(im.size[1]), xrange(im.size[0])), imdata): things = [str(x) for x in (row, col) + colors] f.write(' '.join(things + ['\n'])) #indeed, it does. this block just shows what is in the file. #but I'm still not sure why we need this file. #I don't see it referenced later. #but Matt wrote it so I'm leaving it in cause he's amazing !head sare2.txt grid = BlockGrid(153, 204, block_size=3, lines_on=True) for block, colors in itertools.izip(grid, imdata): block.rgb = colors grid grid2 = BlockGrid(153, 204, block_size=3, lines_on=False) for block, colors in itertools.izip(grid2, imdata): block.rgb = colors grid2