Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Charles Power
- #Consulted Dylan Ray, UNC Chapel Hill Mathematics
- from cImage import *
- import sys, traceback
- def flag(w, h):
- newimg = EmptyImage(w, h)
- for row in range(h):
- #for each row in (h) height
- for column in range(w):
- #for each column in row
- if column < w/3:
- #if pixel is in 1/3 make red
- red = 255
- green = 0
- blue = 0
- elif column < (w/3 + w/3):
- #if pixel is in 2/3 make white
- red = 255
- green = 255
- blue = 255
- else:
- #if pixel is in 3/3 make blue
- red= 0
- green = 0
- blue = 255
- p = Pixel(red, green, blue)
- newimg.setPixel(column,row,p)
- return newimg
- def flip_horizontal(img):
- """Flip the specified image left to right and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(w,h)
- for y in range(h):
- #iterate through each pixel in row
- for x in range(w):
- #gets current pixel in row
- p = img.getPixel(x,y)
- newimg.setPixel((w-1) - x, y, p)
- return newimg
- def sepia_tone(img):
- """Apply the sepia tone transformation to the specified image, and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(w,h)
- for y in range(h):
- #iterate through each pixel in image
- for x in range(w):
- #iterate through each pixel in row
- p = img.getPixel(x,y)
- # gets the current pixel's red intensity
- r = p.getRed()
- g = p.getGreen()
- b = p.getBlue()
- #apply formula to get new pixel
- newR = int(r * 0.393 + g * 0.769 + b * 0.189)
- newG = int(r * 0.349 + g * 0.686 + b * 0.168)
- newB = int(r * 0.272 + g * 0.534 + b * 0.131)
- if newR > 255:
- newR = 255
- if newG > 255:
- newG = 255
- if newB > 255:
- newB = 255
- newpixel = Pixel(newR, newG, newB)
- newimg.setPixel(x,y,newpixel)
- return newimg
- def convert_to_gray_scale(img):
- """Apply the gray scale transformation to the specified image, and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(w,h)
- for y in range(h):
- #iterate through each pixel in image
- for x in range(w):
- #iterate through each pixel in row
- p = img.getPixel(x,y)
- # gets the current pixel's red intensity
- r = p.getRed()
- g = p.getGreen()
- b = p.getBlue()
- #apply formula to get new pixel
- newR = int(r * 0.21 + g * 0.71 + b * 0.07)
- newG = int(r * 0.21 + g * 0.71 + b * 0.07)
- newB = int(r * 0.21 + g * 0.71 + b * 0.07)
- if newR > 255:
- newR = 255
- if newG > 255:
- newG = 255
- if newB > 255:
- newB = 255
- newpixel = Pixel(newR, newG, newB)
- newimg.setPixel(x,y,newpixel)
- return newimg
- def flip_vertical(img):
- """Flip the specified image upside down and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(w,h)
- for y in range(h):
- #iterate through each pixel in row
- for x in range(w):
- #gets current pixel in row
- p = img.getPixel(x,y)
- newimg.setPixel(x, (h-1) - y, p)
- return newimg
- def rotate_right_90(img):
- """Rotate the specified image 90 degrees to the right and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(h,w)
- for y in range(h):
- #iterate through each pixel in row
- for x in range(w):
- #gets current pixel in row
- p = img.getPixel(x,y)
- newimg.setPixel(y, x, p)
- return flip_horizontal(newimg)
- #construct a new image that
- #is a copy of img in which
- #each pixel retains all the red
- #and loses the green and blue components.
- #the function returns the modified image
- def red_filter(img):
- """Apply the red filter transformation to the specified image, and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(w,h)
- for y in range(h):
- for x in range(w):
- p = img.getPixel(x,y)
- newpixel = Pixel(p.getRed(),0,0)
- newimg.setPixel(x,y,newpixel)
- return newimg
- #construct a new image that
- #is a copy of img in which
- #each pixel's red, green, and blue
- #components are replaced with their complement with respect to 255
- #the function returns the modified image
- def negative(img):
- """Apply the negative transformation to the specified image, and return the modified image"""
- w = img.getWidth()
- h = img.getHeight()
- newimg = EmptyImage(w,h)
- for y in range(h):
- for x in range(w):
- p = img.getPixel(x,y)
- newpixel = Pixel(255-p.getRed(),255-p.getGreen(),255 - p.getBlue())
- newimg.setPixel(x,y,newpixel)
- return newimg
- ##DO NOT MODIFY THE FUNCTIONS BELOW
- ##THEY ARE USED FOR TESTING YOUR FUNCTIONS
- ##AND DISPLAYING IMAGES
- #display the specified image in a window
- def display_image(img, t):
- """Display the specified image in a window"""
- win = ImageWin(title = t, width = img.getWidth(), height = img.getHeight())
- img.draw(win)
- #don't modify this function
- def test_one(transform, filename):
- transformName = str(transform).split()[1]
- print("Starting "+ transformName)
- try:
- #create an image corresponding to the specified file
- img = Image(filename)
- #apply the specified transformation to the image
- img = transform(img)
- #show the transformed image
- display_image(img, transformName)
- except:
- print("\t"+transformName + ":Error")
- #don't modify this function
- #I will use it to test all your functions
- def test_all():
- transforms = [flip_horizontal, sepia_tone, convert_to_gray_scale, flip_vertical, rotate_right_90]
- for t in transforms:
- test_one(t, "penguins.gif")
- #test_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement