PhotoMosaics Solution

Write a program that takes a target image and a directory of source images and creates a photomosaic. One approach is to first scale the target to the desired output size (in tiles). For each pixel in the scaled target, find the tile that is the best match in the source images. Replace the pixel in the target with the tile from the source. Output the result.
The project requires 10 functions –two of which you have already written –one of which we are providing • All the functions for the project should be added to the file photomosaic.py –download from the course moodle
For this project, we define a “tile” to be a tuple consisting of a thumbnail image and the average color of that image (image, (155, 200, 135)) • Each time we refer to a “tile” we are talking about such a tuple
Each of the following pages describes a function you will write. In the python file, you will find the outline of a function definition for each of them • The function, variable, and parameter names must be specified exactly
The function average takes an image as input and returns a color –recall that a color is a 3-tuple in the form (R, G, B) • The returned color consists of the average of each color channel –use variables to accumulate the sums of each color channel and divide by the total pixels –total number of pixels can be calculated from the size attribute of the image
The function buildTile takes an image and a tileSize as input and returns a tile –tileSize is a tuple in the form (width, height) • The returned tile consists of the input image scaled to tileSize and the average color of the scaled image –call your scale and average functions
The function scaleTarget takes an image and tuple as input and returns an image –newsize is a tuple in the form (width, height) • The returned image is the original image scaled to the tuple newSize – call scale with the appropriate scale factors
The function findBestThumbnail takes a color and a list of tiles as input and returns an image • The returned image is the thumbnail of the tile whose average color is nearest the color passed as a parameter –call the distance function which we have provided –a thumbnail is the first element of a tile tuple
The function makeTileList takes a list of image file names and a tile size as input and returns a list of tiles • Each item (k) in the resulting list must correspond to the item (k) in the list of image files passed to the function • For each file in the image file list –open the image file –call buildTile to get a tile of the appropriate size –append the new tile to a list
The function takes an image and a list of tiles as input and returns a image • The input image is the scaled target image for the mosaic. The returned image is the photomosaic. –this function creates a new image whose size is determined by the size of a tile and the target • For each pixel in the target –find the tile whose average color is nearest the pixel color and place it in the image using putBlock
The function buildMosaic takes an image file name, a directory containing image files, a tile size, and a resolution –the resolution is a tuple describing the width and height (in tiles) that will make up your mosaic • Orchestrates the creation of the mosaic
• Task 1 – create a list of image file names from the image directory (code provided) • Task 2 – create a list of tiles by calling makeTileList • Task 3 – scale target image by calling scaleTarget • Task 4 – create the mosaic image by calling stitchMosaic • Task 5 – save the mosaic image to a file
Each function is worth N points –N points for adequate comments and N points for functionality.
Powered by