Think about how the grayscale algorithm works. Basically, if you know the luminance of anything visual (e.g., a small image, a letter), you can replace a pixel with that visual element in a similar way to create a collage image. Try implementing this. You’ll need 256 visual elements of increasing lightness, all of the same size. You can create a collage by replacing each pixel in the original image with one of these visual elements.
This is a complex problem. To avoid passing in 256 unique arguments into a function, an array was used instead. Additionally you will find “createGrayscaleArray” and “makeGrayscaleAmnt” functions included to turn any one image into an array of 256 grayscale images.
```
#Assumes everything in grayscale array has same width/height
def makeGrayscaleCollage (source, grayscaleArray):
sW = getWidth(source)
sH = getHeight(source)
collage = makeEmptyPicture(sW*getWidth(grayscaleArray[0]),
sH*getHeight(grayscaleArray[0]))
for x in range(0, getWidth(source)):
for y in range(0,getHeight(source)):
p = getPixel(source, x, y)
intensity = (getRed(p)+getGreen(p)+getBlue(p))/3
grayScaleimg = grayscaleArray[intensity]
widthImg = getWidth(grayScaleimg)
heightImg = getHeight(grayScaleimg)
for xInternal in range(0,widthImg):
for yInternal in range(0,heightImg):
targetPixel = getPixel(collage, x*widthImg+xInternal, y*heightImg+yInternal) sourcePixel = getPixel(grayScaleimg, xInternal, yInternal)
setColor(targetPixel, getColor(sourcePixel))
show(collage)
return collage
def createGrayscaleArray(sourceImg):
imagesArray = [None]*256
for i in range(0,256):
imagesArray[i]=makeGrayscaleAmnt(sourceImg, float(i/256.0))
return imagesArray
def makeGrayscaleAmnt(sourceImg, grayscaleVal):
width = getWidth(sourceImg)
height = getHeight(sourceImg)
canvas = makeEmptyPicture(width,height)
for x in range(0, width):
for y in range(0,height):
p = getPixel(sourceImg, x, y)
intensity = (getRed(p)+getGreen(p)+getBlue(p))/3
newIntensity = float(intensity*grayscaleVal)
pTarget = getPixel(canvas, x, y)
setColor(pTarget, makeColor(newIntensity,newIntensity,newIntensity))
return canvas
```
You might also like to view...
A ________ key is a field that contains a nonrepeating value, which uniquely identifies each record
A) foreign B) primary C) secondary D) common
You have been hired to help a small company set up its first Windows network. It has had the same 13 users for the entire two years it has been open, and the company has no plans to expand. What version of Windows Server 2016 would you recommend?
A. Windows Server 2016 Datacenter (Desktop Experience) B. Windows Server 2016 Standard (Desktop Experience) C. Windows Server 2016 Datacenter D. Windows Server 2016 Essentials