Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import os
- # the distance function takes 2 pixels as input
- # and returns the squared distance between them.
- def distance(pix1, pix2):
- dist = 0
- for i in range(3):
- dist = dist + (pix1[i] - pix2[i])*(pix1[i] - pix2[i])
- return dist
- # place your code for the "scale" function here
- def scale(im, widthFactor, heightFactor):
- wid, ht = im.size
- newIm = Image.new("RGB", (wid*widthFactor,ht*heightFactor))
- newWid, newHt = newIm.size
- for i in range(newWid):
- for j in range(newHt):
- pixCol = im.getpixel((i/widthFactor,j/heightFactor))
- newIm.putpixel((i,j),pixCol)
- return newIm
- # place your code for the "putBlock" function here
- def PutBlock(source, destination, location):
- width, height=source.size
- for i in range(width):
- for j in range(height):
- color=img.getpixel((width+i, height+j))
- destination.putpixel((location[0]+i, location[1]+j),(color))
- return destination
- def average(image):
- width,height=image.size
- R=0 #start a counter
- G=0
- B=0
- numPix=0
- for x in range(width):
- for y in range(height):
- pixels=image.getpixel((x,y))
- R=R+pixels[0] #for each value in the corresponding RGB channels,
- G=G+pixels[1] #they are added together, continued for each pixel
- B=B+pixels[2]
- numPix=numPix+1
- RAvg=R/numPix #to get the average, divide total value by number of pixels
- GAvg=G/numPix
- BAvg=B/numPix
- return(RAvg, GAvg, BAvg)
- def buildTile(img, (tileSize)):
- resize=scale(img,tileSize[0],tileSize[1]) #calling the scale function
- avcolor=average(resize) #calling the average function
- tile=(resize,avcolor) #coupling the scaled image
- #with the average color value
- return tile
- def scaleTarget(img,res):
- resizeTarget=scale(img, res[0], res[1]) #using the red parameter input as the
- #size for the scale function
- return resizeTarget
- def findBestThumb(pix, tileList):
- for x in range(tileList):
- tiledist=distance(pix, tileList[x][1]) #distance between the pix value
- #and the tiles
- smalldist=min(tiledist) #finding the smallest value
- if tileList[x][1]==smalldist: #calling the tileList that matches the
- #smallest value
- return tileList[x][0]#returning that tile as the BestThumb
- else:
- return none
- def makeTileList(imageFileList, (tileSize)):
- tileList=[] #creating a blank list
- for k in range(len(imageFileList)): #using only the imageFileList
- img=Image.open(imageFileList[k])
- newtile=buildTile(img, (tileSize[0], tileSize[1]))
- tileList.append(newtile) #adding the new tile to the blank tileList
- return tileList
- def stitchMosaic(image, tileList):
- target=scaleTarget(image, (res))
- tileList=makeTileList(imageFileList, (tilesize))
- width, height=target.size
- numPix=(width*height) #total number of pixels in the image
- for x in range(width):
- for y in range(height):
- pix=target.getpixel #getting the pixel values of the scaled image
- bestThumb=findBestThumb(pix, tileList)
- mosaicsize=numPix*bestThumb
- mosaicCanvas=Image.new("RGB", mosaicsize) #new image for the mosaic
- MWid, MHgt=mosaic(canvas.size)
- for i in range(MWid):
- for j in range(MHgt):
- Mosaic=PutBlock(bestThumb, mosaicCanvas, (i,j))
- return Mosaic
- # the buildMosaic function orchestrates the creation of the mosaic.
- # It requires a target image file, and directory of images to use
- # as a tile library, a tile size, and a mosaic resolution, as input.
- # Though it returns nothing, its effect is to save a photoMosaic in
- # a .jpg file.
- def buildMosaic(imageFile, imageDirectory, tileSize, res):
- # Task 1: create a list of image file names from a directory
- imgFileList = os.listdir(imageDirectory)
- for item in range(len(imgFileList)):
- imgFileList[item] = imageDirectory+os.sep+imgFileList[item]
- # Task 2: create a list of tiles by calling makeTileList.
- tileList=makeTileList(imgFileList, tileSize)
- # Task 3: scale target image by calling scaleTarget.
- img=Image.open(imageFile)
- scaledTarget=scaleTarget(img, res)
- # Task 4: create the mosaic image by calling stitchMosaic.
- mosaic=stitchMosaic(img, tileList)
- # Task 5: save the mosaic image to a file.
- mosaic.save("finishedmosaic.jpg")
- done=buildMosaic("bunn.jpg","C:/Documents and Settings/Christina/Desktop/New Folder" , (1,1),(2,2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement