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
```

Computer Science & Information Technology

You might also like to view...

Which of the following statements is true?

a. Predicate methods typically test a condition and modify the object on which they're called. b. Predicate methods typically do not test a condition and do not modify the object on which they're called. c. Predicate methods typically test a condition and do not modify the object on which they're called. d. Predicate methods typically do not test a condition and modify the object on which they're called.

Computer Science & Information Technology

If a child class is to contain the public and protected members of its parent class as protected members, which access specifier must be used in the line: class ClassName : ??? ParentClassName ?.

What will be an ideal response?

Computer Science & Information Technology

Which of the following statements is true of storage systems?

A. ?In a magnetic hard drive, a cylinder is smaller in size than a sector. B. ?Optical discs are less durable than magnetic media. C. ?Cloud storage is also known as USB-drive storage. D. ?Smart cards can store biometric data in the card.

Computer Science & Information Technology

The is often omitted from a for header when the control variable has already been assigned a value.

a) semicolon b) initial value of the control variable c) for keyword d) final value of the control variable

Computer Science & Information Technology