Create a function removeGreen(img) that sets the green in all pixels in image img to 0.
from image import *
---
def removeGreen(img):
---
pixels = img.getPixels()
---
for p in pixels:
---
p.setGreen(0)
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
---
win = ImageWin(img.getHeight(), img.getWidth()) #paired
---
img.draw(win)
Checkpoint18.9.2.
Create a function changeColors(img) that sets the red in all pixels equal to the blue value in the image img. Then, it sets the green and blue in all pixels to 255.
from image import *
---
def changeColors(img):
---
pixels = img.getPixels()
---
pixels = img.Pixels() #paired
---
for p in pixels:
---
b = p.getBlue()
---
p.setRed(b)
p.setGreen(255)
p.setBlue(255)
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
---
win = ImageWin(img.getHeight(), img.getWidth()) #paired
---
img.draw(win)
Checkpoint18.9.3.
Create a function reduceGreen(img) that reduces the green in all pixels by 75% in image img.
from image import *
---
def reduceGreen(img):
---
pixelList = img.getPixels()
---
pixelList = img.Pixels() #paired
---
for p in pixelList:
---
g = p.getGreen()
---
p.setGreen(g * 0.25)
---
p.setGreen(g * 0.75) #paired
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
---
img.draw(win)
Checkpoint18.9.4.
Create a function changeRed(img) that sets all red values equal to 1.5x the blue value in the image img.
from image import *
---
def changeRed(img):
---
pixels = img.getPixels()
---
for p in pixels:
---
r = p.getRed() #paired
---
b = p.getBlue()
---
p.setRed(b * 1.5)
---
r.setRed(b * 1.5) #paired
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
---
img.draw(win)
Checkpoint18.9.5.
Create a function modifyColors(img) that decreases the red to 60% of its original value, increases the blue by 60% of its original value, and sets the green to 0 in the image img.
from image import *
---
def modifyColors(img):
---
pixels = img.getPixels()
---
for p in pixels:
---
r = p.getRed()
b = p.getBlue()
---
p.setRed(r * 0.6)
p.setBlue(b * 1.6)
p.setGreen(0)
---
p.setRed(r / 0.6)
p.setBlue(b * 0.6)
p.setGreen(0) #paired
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
---
img.draw(win)
---
img.draw() #paired
Checkpoint18.9.6.
Create a function imageToWhite(img) that makes the image img completely white.
from image import *
---
from picture import * #paired
---
def imageToWhite(img):
---
pixels = img.getPixels()
for p in pixels:
---
p.setRed(255)
p.setGreen(255)
p.setBlue(255)
---
p.setRed(0)
p.setGreen(0)
p.setBlue(0) #paired
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
Checkpoint18.9.7.
Create a function alternatingRed(img) that sets the red of every other pixel to 0 in the image img.
from image import *
---
def alternatingRed(img):
---
for x in range(0, img.getWidth(), 2):
---
for x in range(img.getWidth()): #paired
---
for y in range(0, img.getHeight(), 2):
---
for y in range(img.getHeight()): #paired
---
p = img.getPixel(x,y)
---
p.setRed(0)
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
Checkpoint18.9.8.
Create a function changeQuadrantColors(img) that only changes the color of the pixels in the bottom left quadrant of the image img. The code should set the red value to the original blue value, the green value to the original red value, and the blue value to the original green value.
from image import *
---
def changeQuadrantColors(img):
---
halfWidth = (int) (img.getWidth() / 2)
halfHeight = (int) (img.getHeight() / 2)
---
for x in range(halfWidth):
---
for y in range(halfHeight, img.getHeight()):
---
for y in range(halfHeight): #paired
---
p = img.getPixel(x, y)
---
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
---
newPixel = Pixel(b, r, g)
---
img.setPixel(x, y, newPixel)
---
win = ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
Checkpoint18.9.9.
Create a function copyRightSide(img) that copies the right side of the image onto the left side in the image img.
from image import *
---
def copyRightSide(img):
---
halfway = (int) (img.getWidth() / 2)
---
for x in range(halfway, img.getWidth()):
---
for x in range(halfway): #paired
---
for y in range(img.getHeight()):
---
p = img.getPixel(x, y)
---
img.setPixel(x - halfway, y, p)
---
img.setPixel(halfway + x, y, p) #paired
---
win = ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
Checkpoint18.9.10.
Create a function copyTopQuarter(img) that copies the pixels from the top quarter of the y-axis to the bottom quarter of the y-axis in the image img.
from image import *
---
def copyTopQuarter(img):
---
quarterHeight = (int) (img.getHeight() / 4)
---
for x in range(img.getWidth()):
---
for y in range(quarterHeight):
---
p = img.getPixel(x, y)
---
img.setPixel(x, quarterHeight * 3 + y, p)
---
win = ImageWin(img.getWidth(), img.getHeight())
img.draw(win)