Remember that it can take a bit of time to process all the pixels in a picture! Check for errors below the code if it is taking a long time, but if you donβt see any errors just wait.
A grayscale picture is when the red, green, and blue value of a pixel are all equal to the average of the original pixel value. Write the code to turn the left half of the image gal2.jpg into gray scale.
A grayscale picture is when the red, green, and blue value of a pixel are all equal to the average of the original pixel value. Create the code to turn the left half of the image gal2.jpg into gray scale.
from image import *
---
img = Image("gal2.jpg")
---
pixels = img.getPixels()
---
for p in pixels:
r = p.getRed()
b = p.getBlue()
g = p.getGreen()
---
avg = int((r + b + g) / 3)
---
r = p.setRed(avg)
b = p.setBlue(avg)
g = p.setGreen(avg)
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)
Define a procedure to negate an image. You can negate an image by setting each color value to 255 minus the current value. Write code to negate the kitten.jpg image.
Define a procedure to negate an image. You can negate an image by setting each color value to 255 minus the current value. Create code to negate the kitten3.jpg image.
from image import *
---
img = Image("kitten.jpg")
---
pixels = img.getPixels()
---
for p in pixels:
r = p.getRed()
b = p.getBlue()
g = p.getGreen()
---
r = p.setRed(255-r)
b = p.setBlue(255-b)
g = p.setGreen(255-g)
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)
from image import *
---
img = Image("kitten.jpg")
---
halfway = (int(img.getHeight() / 2))
---
for x in range(img.getWidth()):
for y in range(halfway):
---
p = img.getPixel(x, y)
---
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
---
newPixel = Pixel(r, g, b)
---
img.setPixel(x, halfway + y, newPixel)
---
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)
Write a function to mirror the image swan.jpg from left to right around a vertical line in the middle of the image. Pass the image to the function. Do the import, create the image, call the function, and show the result.
Create a function to mirror the image swan.jpg from left to right around a vertical line in the middle of the image. Pass the image to the function. Do the import, create the image, call the function, and show the result.
from image import *
---
img = Image("swan.jpg")
---
halfway = (int(img.getWidth() / 2))
---
for x in range(img.getWidth()):
for y in range(img.getHeight()):
---
p = img.getPixel(x, y)
---
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
---
newPixel = Pixel(r, g, b)
---
img.setPixel(img.getWidth() - x - 1, y, newPixel)
---
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)
from image import *
---
img = Image("vangogh.jpg")
---
halfway = (int(img.getHeight() / 2))
---
for x in range(img.getWidth()):
for y in range(img.getHeight()):
---
p = img.getPixel(x, y)
---
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
---
newPixel = Pixel(r, g, b)
---
img.setPixel(x, img.getHeight() - y - 1, newPixel)
---
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)