예제 #1
0
def detect_edges(original_image: Image, threshold: int) -> Image:
    """ Developed by Karandev Andotra, 101141882
        Reviewed by Hussein Rashid, 101141962
    
    Return an edge detection modified copy of an image from a user-selected 
    original image and an input threshold value.
    
    >>> original_image = load_image(choose_file())
    >>> detect_edges_image = detect_edges(original_image, 10)
    """
    new_image = copy(original_image)
    width = get_width(original_image)
    height = get_height(original_image)

    for x in range(width):
        for y in range(height):
            if not y == height - 1:
                r, g, b = get_color(new_image, x, y)
                r_down, g_down, b_down = get_color(new_image, x, y + 1)

                brightness = (r + g + b) // 3
                brightness_down = (r_down + g_down + b_down) // 3

                if abs(brightness - brightness_down) > threshold:
                    new_color = create_color(0, 0, 0)
                else:
                    new_color = create_color(255, 255, 255)
                set_color(new_image, x, y, new_color)
            else:
                new_color = create_color(255, 255, 255)
                set_color(new_image, x, y, new_color)
    return new_image
def extreme_contrast(inpic: Image) -> Image:
    """Returns a copy of the inputed image with all pixel values set to
    their maximum.
    
    Author: Yahya Shah
    
    >>>extreme_contrast(image)
    image with only black or white pixels
    """
    pic = copy(inpic)

    for i, m, (r, g, b) in inpic:
        if r < 128:
            r = 0
        else:
            r = 255

        if g < 128:
            g = 0
        else:
            g = 255

        if b < 128:
            b = 0
        else:
            b = 255

        newcol = create_color(r, g, b)
        set_color(pic, i, m, newcol)

    return pic
예제 #3
0
def extreme_contrast(original_image: Image) -> Image:
    """ Developed by Ahmed Abdellah, 101163588
        Reviewed by Karandev Andotra, 101141882
    
    Return an extreme contrast filtered image from a user-selected original 
    image.
    
    >>> original_image = load_image(choose_file())
    >>> extreme_contrast_image = extreme_contrast(original_image)
    """
    new_image = copy(original_image)

    for pixel in original_image:
        x, y, (r, g, b) = pixel
        if r <= 127:
            r = 0
        elif r >= 128:
            r = 255
        if g <= 127:
            g = 0
        elif g >= 128:
            g = 255
        if b <= 127:
            b = 0
        elif b >= 128:
            b = 255
        new_color = create_color(r, g, b)
        set_color(new_image, x, y, new_color)
    return new_image
예제 #4
0
def extreme_contrast(image:Image) -> Image:
    '''
    Function Name: Extreme Contrast
    Author: Taekwan Oh 101148069
    '''
    '''
    Returns an extremely contrasted copy of the image passed in by looking 
    at the brightness of each colour in each pixel in the passed in image.
    >>>show(extreme_contrast(filename))
    displays expected image
    '''
    
    new_image = copy(image)
    for pixel in image:
        x, y, (r, g, b) = pixel 
        if 0<=r<=127:
            r = 0
        elif 128<=r<=255:
            r = 255
        if 0<=g<=127:
            g = 0
        elif 128<=g<=255:
            g = 255        
        if 0<=b<=127:
            b = 0
        elif 128<=b<=255:
            b = 255   
        new_colour = create_color(r,g,b)
        set_color(new_image, x, y, new_colour)
    return new_image
def combine(red_image: Image, green_image: Image, blue_image: Image) -> Image:
    """
    Returns a single image that is a combination
    of the three r, g, b values of the inputed images.
    
    written by: Anita Ntomchukwu
    
    >>>combine(red_image, green_image, blue_image)
    image that looks exactly like the image before being passed through the red,
    green and blue channel filters.
    """

    original_image1 = red_image
    original_image2 = green_image
    original_image3 = blue_image

    comb = copy(original_image1)

    for x, y, (r, g, b) in comb:

        red = get_color(original_image1, x, y)[0]
        green = get_color(original_image2, x, y)[1]
        blue = get_color(original_image3, x, y)[2]

        new_color = create_color(red, green, blue)
        set_color(comb, x, y, new_color)

    return comb
예제 #6
0
def detect_edges(original_image: Image, threshold: float) -> Image:
    """Takes an image, makes a copy of it and changes the color of the pixels
    to black or white, depending on their contrast.

    - Function written by Malak Abdou - 101139692

    >>> original_image = load_image(choose_file())
    >>> show(detect_edges(original_image, 15))
    # Returns image with detect_edges filter applied with a threshold of 15
    without modifying original.
    """

    original2 = copy(original_image)

    black = create_color(0, 0, 0)
    white = create_color(255, 255, 255)

    for x in range(get_width(original2)):

        for y in range(get_height(original2) - 1):

            r, g, b = get_color(original2, x, y)
            r1, g1, b1 = get_color(original2, x, (y + 1))

            brightness = (r + g + b) / 3
            brightness1 = (r1 + g1 + b1) / 3

            if abs(brightness - brightness1) > threshold:
                set_color(original2, x, y, black)
            else:
                set_color(original2, x, y, white)

    return original2
예제 #7
0
def flip_horizontal (image: Image) -> Image:
    """ Author: Bardia Parmoun 
    Type Annotations: Cimpl.Image -> Cimpl.Image

    This function takes an image, makes a copy of it and then it flips it along
    the horizontal axis. It does that by swapping the colours of the pixels that
    are the same distance from the top and bottom of the image. 
    
    >>> new=flip_horizontal (original_image)
    >>> show(new)
    (It will display a version of the image that has been flipped along the 
    horizontal axis)
    """    
    
    new_image = copy(image)
    WIDTH = new_image.get_width()
    HEIGHT = new_image.get_height()
    
    for x in range (WIDTH):
        for y in range (HEIGHT//2): # Only half of the height is needed
            # Replaces the colour of the pixel with a pixel that is equally
            # Distant from the bottom of the image (Same x value but the y value
            # Becomes HEIGHT - y -1
            col1 = get_color(new_image,x,y)
            col2 = get_color(new_image,x,HEIGHT-y-1)
            set_color(new_image, x,y, col2)
            set_color(new_image, x,HEIGHT-y-1, col1)
    return new_image
예제 #8
0
def sepia(original_image: Image) -> Image:
    """Returns a black and white image which has been tinted yellow.

    >>> original_image = load_image(choose_file())
    >>> sepia(original_image)
    <Cimpl.Image object at 0x00000212C566DD88>

    """
    new_image = copy(original_image)
    sepia_filter = grayscale(new_image)

    for pixel in sepia_filter:
        x, y, (r, g, b) = pixel
        if r < 63:
            r *= 1.1
            b *= 0.9

        if 63 <= r <= 191:
            r *= 1.15
            b *= 0.85

        if r > 191:
            r *= 1.08
            b *= 0.93

        new_color = create_color(r, g, b)
        set_color(sepia_filter, x, y, new_color)

    return sepia_filter
예제 #9
0
def extreme_contrast(original_image: Image) -> Image:
    """Returns the 'extreme contrast' version of an image without having
    modified it.
    - Function written by Malak Abdou - 101139692

    >>> original_image = load_image(choose_file())
    >>> show(extreme_contrast(original_image))
    # Returns a copy of original_image with extreme_contrast filter on it.
    """
    extreme_copy = copy(original_image)
    for pixel in extreme_copy:
        x, y, (r, g, b) = pixel
        if r <= 127:
            r = 0
        else:
            r = 255
        if g <= 127:
            g = 0
        else:
            g = 255
        if b <= 127:
            b = 0
        else:
            b = 255
        extreme_color = create_color(r, g, b)
        set_color(extreme_copy, x, y, extreme_color)
    return extreme_copy
예제 #10
0
def flip_vertical(image: Image) -> Image:
    """Author: Ian Holmes 
    Type Annotations: Cimpl.Image -> Cimpl.Image

    Return a copy of the inputted image, with the pixels flipped along the
    y axis. 
    
    >>> new = flip_horizontal(image)
    >>> show(new)
    """
    
    new_image = copy(image)
    
    width = get_width(image)
    height = get_height(image)
    
    for y in range(height):
        for x in range(width//2):
            # Replaces the colour of the pixel with a pixel that is equally
            # Distant from the right of the image (Same y value but the x value
            # Becomes WIDTH - x -1            
            col1 = get_color(new_image, x, y)
            col2 = get_color(new_image, width-x-1, y)
            set_color(new_image, x, y, col2)
            set_color(new_image, width-x-1,y, col1)
            
    return new_image
def combine_test() -> None:
    '''Function Name: Combine Test Function
    Author: Mohamed Kaddour 101140829
    '''
    '''
    Takes in the three images being combined and checks if their combination 
    is equal to that of the combined image. Prints out the pixel being tested
    and the RGB values. 
    >>>combine_test(r_image, g_image, b_image, combined_image)
    Testing Combined Image...
    Testing Pixels: 225 473 RGB: (151, 144, 90) with RGB: (151, 144, 90)
    ...
    ...
    ...
    '''

    original_image = create_image(2, 1)
    set_color(original_image, 0, 0, create_color(85, 44, 12))
    set_color(original_image, 1, 0, create_color(24, 128, 18))

    combined_image = combine(red_channel(original_image),
                             green_channel(original_image),
                             blue_channel(original_image))

    print("Testing Combined Image...")
    for pixel in combined_image:
        x, y, (r, g, b) = pixel
        comb_pix = r, g, b
        acc_pix = get_color(original_image, x, y)
        print("Testing Pixels:", x, y, "RGB:", comb_pix, "with RGB:", acc_pix)
        if (acc_pix) != (comb_pix):
            print("FAILED")
        else:
            print("PASSED")
예제 #12
0
def combine (image1: Image, image2: Image, image3: Image) -> Image: 
    """ Author: Bardia Parmoun 
    Type Annotations: Cimpl.Image, Cimpl.Image, Cimpl.Image -> Cimpl.Image
   
    This functions three filtered images (red, green, blue) and creates a new 
    image by combining the rgb's of these three images. 
   
    Note that in order for this function to work properly the three input images
    must be in .png format and should be properly filtered (for example the red
    image must only have a red component in its rgb).
   
    >>> combined_image= combine (red_image,green_image, blue_image)
    >>> show (combined_image)
    (This will display the combined image made from the three input images)
    """
    
    new_image = copy(image1)
    width = get_width(image1)
    height = get_height(image1)
    
    for x in range(width):
        for y in range(height):
            r1,g1,b1= get_color(image1, x, y)
            r2,g2,b2= get_color(image2, x, y)
            r3,g3,b3= get_color(image3, x, y)  
            
            # Combining the there pictures by adding their rgb values
            new_colour = create_color(r1+r2+r3, g1+g2+g3, b1+b2+b3)
            set_color (new_image, x,y, new_colour)   
    return new_image
def two_tone(image: Image, colour1: str, colour2: str) -> Image:
    '''Returns a copy of inputted image with two different tones 
    based on user selection. 
    
    written by: Anita Ntomchukwu

    >>> two_tone(image, 'red', 'white')
    image with only red and white pixels
    '''

    two_tone_image = copy(image)

    for i in range(len(COLOURS)):
        if colour1 == COLOURS[i]:
            tone1 = RGB_VALUES[i]
            new_c1 = create_color(tone1[0], tone1[1], tone1[2])
        if colour2 == COLOURS[i]:
            tone2 = RGB_VALUES[i]
            new_c2 = create_color(tone2[0], tone2[1], tone2[2])

    for x, y, (r, g, b) in two_tone_image:
        brightness = (r + g + b) // 3
        if 0 <= brightness <= 127:
            set_color(two_tone_image, x, y, new_c1)
        elif 128 <= brightness <= 255:
            set_color(two_tone_image, x, y, new_c2)

    return two_tone_image
def sepia(image: Image) -> Image:
    """Returns a copy of an image with a sepia filter.
    
    *Written by Sam Hurd*
    
    >>> sepia(image)
    image with grayscale and a yellow tint
    """
    pic = grayscale(copy(image))

    for x, y, (r, g, b) in pic:

        if r < 63:
            b = b * 0.9
            r = r * 1.1

        elif r <= 191:
            b = b * 0.85
            r = r * 1.15

        else:
            b = b * 0.93
            r = r * 1.08

        color = create_color(r, g, b)
        set_color(pic, x, y, color)

    return pic
예제 #15
0
def two_tone(image: Image, colour_1: str, colour_2: str) -> Image:
    """Function an image and two colours as strings from the given list:
    black
    white
    red
    lime
    blue
    yellow
    cyan
    magenta
    gray
    
    Function returns an image in two tones as per the colours given in the 
    second and third function parameters, decided by an individual pixel's 
    brightness.
    -Function written by Nathan Gomes, 101143780
    
    >>> image = load_image(choose_file())
    >>> two_tone_image = two_tone(image, "red", "gray")
    >>> show(two_tone_image)
    >>> two_tone(image, "black", "pink")
    #Error because colour passed ("pink") is not in the given list
    """
    
    black = create_color(0, 0, 0)
    white = create_color(255, 255, 255)
    red = create_color(255, 0, 0)
    lime = create_color(0, 255, 0)
    blue = create_color(0, 0, 255)
    yellow = create_color(255, 255, 0)
    cyan = create_color(0, 255, 255)
    magenta = create_color(255, 0, 255)
    gray = create_color(128, 128, 128)
    
    colours = [("black", black), ("white", white), ("red", red), 
               ("lime", lime), ("blue", blue), ("yellow", yellow), 
               ("cyan", cyan), ("magenta", magenta), ("gray", gray)]
    
    new_image = copy(image)
    for pixel in new_image:
        x, y, (r, g, b) = pixel
        average = (r + g + b) / 3
        
        if (average >= 0) and (average < 128):
            for i in range(len(colours)):
                if colour_1 == colours[i][0]:
                    set_color(new_image, x, y, colours[i][1])
                    
        elif (average > 127) and (average < 256):
            for i in range(len(colours)):
                if colour_2 == colours[i][0]:
                    set_color(new_image, x, y, colours[i][1])
    
    return new_image
예제 #16
0
def red_channel(original_image: Image) -> Image:
    """Returns the red channel of image without having modified it.
    - Function written by Malak Abdou - 101139692

    >>> red_channel(original_image)
    <Cimpl.Image object at 0x000001C447BDFAC8>
    """
    red_filter = copy(original_image)
    for pixel in original_image:
        x, y, (r, g, b) = pixel
        red = create_color(r, 0, 0)
        set_color(red_filter, x, y, red)
    return red_filter
def blue_channel(inpic) -> Image:
    """Returns a copy of the inputed image with only bluescale pixels.
    
    written by Yahya Shah
    
    >>>blue_channel(image)
    image with all pixels blue
    """
    pic = copy(inpic)
    for i, m, (r, g, b) in pic:
        blue = create_color(0, 0, b)
        set_color(pic, i, m, blue)
    return pic
예제 #18
0
def grayscale_from_blue(image: Image) -> Image:
    """Return a grayscale copy of image. Each pixel's blue component provides
    the RGB components for the corresponding gray shade.
    
    >>> image = load_image(choose_file())
    >>> gray_image = grayscale_from_blue(image)
    >>> show(gray_image)
    """
    new_image = copy(image)
    for x, y, (r, g, b) in image:
        gray = create_color(b, b, b)      
        set_color(new_image, x, y, gray)        
    return new_image
def red_channel(image: Image) -> Image:
    """Return a red filtered copy of image; that is, an image that is showing only the red color of the original image.

    >>> image = load_image(choose_file())
    >>> inverted = invert(image)
    >>> show(inverted)
    """
    new_image = copy(image)
    # filter the intensities of every component in every pixel.
    for x, y, (r, g, b) in image:
        red = create_color(r, 0, 0)
        set_color(new_image, x, y, red)
    return new_image
def red_channel(image: Image) -> Image:
    """Returns a copy of an image with only redscale pixels.
    
    *written by Sam Hurd*
    
    >>>red_channel(image)
    image with all pixels red
    """
    new_image = copy(image)
    for x, y, (r, g, b) in new_image:
        red = create_color(r, 0, 0)
        set_color(new_image, x, y, red)
    return new_image
예제 #21
0
def detect_edges_better (image:Image, threshold: float) -> Image:  
    """ Author: Benjamin Richards 
    Type Annotations: Cimpl.Image, float -> Cimpl.Image

    This function tries to find the edges of a given image by going through 
    every pixel and checking its brightness with the one below and to the right
    of it. That difference is compared with a given threshold value. In other 
    words the function will produce a black and white version of the image where
    the background is white and the main parts of the image is black. 
    
    >>> new_image= detect_edge_better(copied_image,10)
    >>> show(new_image)
    (Shows the new black and white image generated by detecting edges)
    """
    
    new_image = copy(image)  
    
    # Constants
    WIDTH = new_image.get_width()
    HEIGHT = new_image.get_height()
    
    BACK = (255,255,255)
    EDGE = (0,0,0)
    
    for x in range (WIDTH):
        for y in range (HEIGHT):
            r,g,b = get_color(new_image,x,y)
            brightness = (r+g+b)/3
            
            # The default colour value is set to BACK (white). This value later
            # Can change to EDGE (black) if the difference exceeds the threshold
            new_colour = create_color(BACK[0],BACK[1],BACK[2])
            
            if y != HEIGHT-1 and x != WIDTH -1:  
                # Makes sure the range doesn't exceed the height and the width
                # Calculates the brightness of the pixel below it
                r_down,g_down,b_down = get_color(new_image,x,y+1)
                brightness_down = (r_down+g_down+b_down)/3
                
                r_right,g_right,b_right=get_color(new_image,x+1,y)
                brightness_right = (r_right+g_right+b_right)/3                 
                
                # Checks to see if the difference between the brightness values
                # Exceeds the threshold and if so converts it to black
                if (abs(brightness-brightness_down) > threshold or 
                    abs(brightness-brightness_right) > threshold):
                    new_colour= create_color (EDGE[0],EDGE[1],EDGE[2])
                    
            set_color(new_image, x, y, new_colour)
       
    return new_image 
예제 #22
0
def green_channel(original_image: Image) -> Image:
    """ Developed by Karandev Andotra, 101141882
    
    Return a filtered image from a user-selected original image.
    RBG code (0,g,0) where g is the original green pixel value.
    
    >>> green_channel(original_image)
    """
    new_image = copy(original_image)
    for pixel in original_image:
        x, y, (r, g, b) = pixel
        new_color = create_color(0, g, 0)
        set_color(new_image, x, y, new_color)
    return new_image
예제 #23
0
def red_channel(image:Image) -> Image:
    '''Return a copy of the provided image that is completley red, (r,0,0).
    Each pixel of the image must be red
    >>> image = load_image(choose_file()) 
    >>> red_image = red_channel(image)
    >>> show(red_image)
    '''
    
    new_image = copy(image)
    for pixel in image:
        x, y, (r, g, b) = pixel        
        red = create_color(r,0,0)
        set_color(new_image, x, y, red)
    return new_image
예제 #24
0
def blue_channel(image:Image) -> Image:
    '''Return a copy of the provided image that is completley blue, (0,0,b).
    Each pixel of the image must be blue.
    >>> image = load_image(choose_file()) 
    >>> blue_image = blue_channel(image)
    >>> show(blue_image)
    '''
    
    new_image = copy(image)
    for pixel in image:
        x, y, (r, g, b) = pixel        
        green = create_color(0,0,b)
        set_color(new_image, x, y, green)
    return new_image    
예제 #25
0
def green_channel(image:Image) -> Image:
    '''Return a copy of the provided image that is completley green, (0,g,0).
    Each pixel of the image must be green
    >>> image = load_image(choose_file()) 
    >>> green_image = green_channel(image)
    >>> show(green_image)
    '''
    
    new_image = copy(image)
    for pixel in image:
        x, y, (r, g, b) = pixel        
        green = create_color(0,g,0)
        set_color(new_image, x, y, green)
    return new_image
예제 #26
0
def two_tone(image: Image, colour1: str, colour2: str) -> Image:
    '''
    Function Name: two_tone
    Author: Mohamed Kaddour 101140829
    '''
    '''Returns a filtered image consisting of the two colours passed in as 
    strings from a list of colors:
    black
    white
    red
    lime
    yellow
    cyan
    magenta
    gray
    >>> two_tone(filename, "red", "blue")
    -> Displays proper image.
    >>> two_tone(filename, "green", "purple") 
    -> Displays incorrect image.
    '''

    colours = {
        'black': (0, 0, 0),
        'white': (255, 255, 255),
        'red': (255, 0, 0),
        'lime': (0, 255, 0),
        'blue': (0, 0, 255),
        'yellow': (255, 255, 0),
        'cyan': (0, 255, 255),
        'magenta': (255, 0, 255),
        'gray': (128, 128, 128)
    }

    new_image = copy(image)
    new_colour = create_color(0, 0, 0)
    rgb = (0, 0, 0)

    for pixel in image:
        x, y, (r, g, b) = pixel
        avg = (r + b + g) / 3
        if 0 <= avg <= 127:
            rgb = colours[colour1]
            new_colour = create_color(rgb[0], rgb[1], rgb[2])
            set_color(new_image, x, y, new_colour)
        elif 128 <= avg <= 255:
            rgb = colours[colour2]
            new_colour = create_color(rgb[0], rgb[1], rgb[2])
            set_color(new_image, x, y, new_colour)

    return new_image
예제 #27
0
def blue_channel(original_image: Image) -> Image:
    """ Developed by Hussein Rashid, 101141962
    
    Return a filtered image from a user-selected original image.
    RBG code (0,0,b) where b is the original blue pixel value.
    
    >>> blue_channel(original_image)
    """
    new_image = copy(original_image)
    for pixel in original_image:
        x, y, (r, g, b) = pixel
        new_color = create_color(0, 0, b)
        set_color(new_image, x, y, new_color)
    return new_image
예제 #28
0
def red_channel(original_image: Image) -> Image:
    """ Developed by Ahmed Abdellah, 101163588
    
    Return a filtered image from a user-selected original image.
    RBG code (r,0,0) where r is the original red pixel value.
    
    >>> red_channel(original_image)
    """
    new_image = copy(original_image)
    for pixel in original_image:
        x, y, (r, g, b) = pixel
        new_color = create_color(r, 0, 0)
        set_color(new_image, x, y, new_color)
    return new_image
def detect_edges_better(original_image: Image, threshold: float) -> Image:
    """Returns an image that looks like a pencil sketch of the original image,
    changing the pixels' colors to black or white.
    - Function written by Leanne Matamoros - 101147405

    >>> imp_edge(original_image, 15)
    <Cimpl.Image object at 0x000002096DEEA148>
    """
    edges_copy = copy(original_image)

    width = get_width(edges_copy)
    height = get_height(edges_copy)

    for x in range(width):
        for y in range(height):

            r, g, b = get_color(edges_copy, x, y)
            brightness = (r + g + b) / 3

            if y != (height - 1):
                r1, g1, b1 = get_color(edges_copy, x, (y + 1))
                brightness1 = (r1 + g1 + b1) / 3

            if x != (width - 1):
                r2, g2, b2 = get_color(edges_copy, (x + 1), y)
                brightness2 = (r2 + g2 + b2) / 3

            black = create_color(0, 0, 0)
            white = create_color(255, 255, 255)

            if x == (width - 1) and y == (height - 1):
                set_color(edges_copy, x, y, white)

            elif x != (width - 1) or y != (height - 1):
                if abs(brightness - brightness1) > threshold or abs(
                        brightness - brightness2) > threshold:
                    set_color(edges_copy, x, y, black)

                elif abs(brightness - brightness1) < threshold or abs(
                        brightness
                        - brightness2) < threshold:
                    set_color(edges_copy, x, y, white)

            elif x == (width - 1):
                if abs(brightness - brightness1) > threshold:
                    set_color(edges_copy, x, y, black)

                elif abs(brightness - brightness1) < threshold:
                    set_color(edges_copy, x, y, white)
예제 #30
0
def invert(image: Image) -> Image:
    """Return an inverted copy of image; that is, an image that is a colour 
    negative of the original image.
    
    >>> image = load_image(choose_file())
    >>> inverted = invert(image)
    >>> show(inverted)
    """
    new_image = copy(image)
    
    # Invert the intensities of every component in every pixel.
    for x, y, (r, g, b) in image:
        inverted = create_color(255 - r, 255 - g, 255 - b)
        set_color(new_image, x, y, inverted)
    return new_image