Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. from PIL import Image
  2. import os
  3.  
  4. # the distance function takes 2 pixels as input
  5. # and returns the squared distance between them.
  6. def distance(pix1, pix2):
  7. dist = 0
  8. for i in range(3):
  9. dist = dist + (pix1[i] - pix2[i])*(pix1[i] - pix2[i])
  10. return dist
  11.  
  12. # place your code for the "scale" function here
  13.  
  14. def scale(im, widthFactor, heightFactor):
  15.  
  16. wid, ht = im.size
  17.  
  18. newIm = Image.new("RGB", (wid*widthFactor,ht*heightFactor))
  19. newWid, newHt = newIm.size
  20.  
  21. for i in range(newWid):
  22. for j in range(newHt):
  23.  
  24. pixCol = im.getpixel((i/widthFactor,j/heightFactor))
  25.  
  26. newIm.putpixel((i,j),pixCol)
  27.  
  28. return newIm
  29.  
  30. # place your code for the "putBlock" function here
  31.  
  32. def PutBlock(source, destination, location):
  33. width, height=source.size
  34. for i in range(width):
  35. for j in range(height):
  36. color=img.getpixel((width+i, height+j))
  37. destination.putpixel((location[0]+i, location[1]+j),(color))
  38. return destination
  39.  
  40. def average(image):
  41. width,height=image.size
  42.  
  43. R=0 #start a counter
  44. G=0
  45. B=0
  46. numPix=0
  47.  
  48. for x in range(width):
  49. for y in range(height):
  50. pixels=image.getpixel((x,y))
  51. R=R+pixels[0] #for each value in the corresponding RGB channels,
  52. G=G+pixels[1] #they are added together, continued for each pixel
  53. B=B+pixels[2]
  54. numPix=numPix+1
  55.  
  56. RAvg=R/numPix #to get the average, divide total value by number of pixels
  57. GAvg=G/numPix
  58. BAvg=B/numPix
  59.  
  60. return(RAvg, GAvg, BAvg)
  61.  
  62. def buildTile(img, (tileSize)):
  63.  
  64.  
  65. resize=scale(img,tileSize[0],tileSize[1]) #calling the scale function
  66. avcolor=average(resize) #calling the average function
  67. tile=(resize,avcolor) #coupling the scaled image
  68. #with the average color value
  69. return tile
  70.  
  71. def scaleTarget(img,res):
  72. resizeTarget=scale(img, res[0], res[1]) #using the red parameter input as the
  73. #size for the scale function
  74. return resizeTarget
  75.  
  76. def findBestThumb(pix, tileList):
  77. for x in range(tileList):
  78. tiledist=distance(pix, tileList[x][1]) #distance between the pix value
  79. #and the tiles
  80. smalldist=min(tiledist) #finding the smallest value
  81. if tileList[x][1]==smalldist: #calling the tileList that matches the
  82. #smallest value
  83. return tileList[x][0]#returning that tile as the BestThumb
  84. else:
  85. return none
  86.  
  87. def makeTileList(imageFileList, (tileSize)):
  88. tileList=[] #creating a blank list
  89. for k in range(len(imageFileList)): #using only the imageFileList
  90. img=Image.open(imageFileList[k])
  91. newtile=buildTile(img, (tileSize[0], tileSize[1]))
  92. tileList.append(newtile) #adding the new tile to the blank tileList
  93. return tileList
  94.  
  95. def stitchMosaic(image, tileList):
  96. target=scaleTarget(image, (res))
  97. tileList=makeTileList(imageFileList, (tilesize))
  98. width, height=target.size
  99. numPix=(width*height) #total number of pixels in the image
  100. for x in range(width):
  101. for y in range(height):
  102. pix=target.getpixel #getting the pixel values of the scaled image
  103. bestThumb=findBestThumb(pix, tileList)
  104. mosaicsize=numPix*bestThumb
  105. mosaicCanvas=Image.new("RGB", mosaicsize) #new image for the mosaic
  106. MWid, MHgt=mosaic(canvas.size)
  107. for i in range(MWid):
  108. for j in range(MHgt):
  109. Mosaic=PutBlock(bestThumb, mosaicCanvas, (i,j))
  110. return Mosaic
  111.  
  112.  
  113.  
  114. # the buildMosaic function orchestrates the creation of the mosaic.
  115. # It requires a target image file, and directory of images to use
  116. # as a tile library, a tile size, and a mosaic resolution, as input.
  117. # Though it returns nothing, its effect is to save a photoMosaic in
  118. # a .jpg file.
  119. def buildMosaic(imageFile, imageDirectory, tileSize, res):
  120.  
  121. # Task 1: create a list of image file names from a directory
  122. imgFileList = os.listdir(imageDirectory)
  123. for item in range(len(imgFileList)):
  124. imgFileList[item] = imageDirectory+os.sep+imgFileList[item]
  125.  
  126. # Task 2: create a list of tiles by calling makeTileList.
  127. tileList=makeTileList(imgFileList, tileSize)
  128.  
  129. # Task 3: scale target image by calling scaleTarget.
  130. img=Image.open(imageFile)
  131. scaledTarget=scaleTarget(img, res)
  132.  
  133. # Task 4: create the mosaic image by calling stitchMosaic.
  134. mosaic=stitchMosaic(img, tileList)
  135.  
  136. # Task 5: save the mosaic image to a file.
  137. mosaic.save("finishedmosaic.jpg")
  138.  
  139.  
  140. 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