Question

I'm trying to leave one third of the image stock, change all the black to yellow in the middle, and change the bottom third black to blue. I know how to change the colours, the problem I'm facing is I'm unaware of how I can select only one third of the pixels to manipulate them. Here s what I have..

def changeSpots1():
    file = pickAFile()
    picture = makePicture(file) 
    show(picture)
    pix = getAllPixels(picture)
    for p in pix:
        intensity = (getRed(p) + getGreen(p) + getBlue(p))
        c = getColor(p)
        if (intensity < 150):              
            newColour = setColor(p, yellow)
    repaint(picture)

I am using a program called JES to write this, incase you're wondering about commands like pickAFile. Thank you for any help!

Was it helpful?

Solution

I know nothing about JES, but I'm going to guess that getAllPixels returns the pixels in the usual order: the first row, then the next row, then the next, etc.

If so:

pix = getAllPixels(picture)
third = len(pix) // 3
for p in pix[:third]:
    # do top-third stuff
for p in pix[third:third*2]:
    # do middle-third stuff
for p in pix[third*2:]:
    # do bottom-third stuff

This does assume that the picture s divisible perfectly into thirds. If it's not, you will need to know the picture's width so you can round to the nearest complete row (because otherwise the top third might actually be 250 complete rows and the first 47 pixels of the 251st, which won't look very good). I don't know what function JES has to get the width, but I'm sure it's simple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top