Example #1
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
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
Example #3
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
Example #4
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 detect_edges(image: Image, threshold: float) -> Image:
    """Returns a copy of an image with the pixels changed to either black 
    or white based on the contrast of the pixel above or below based on 
    inputed threshold.
    
    *Written by Sam Hurd*
    
    >>>detect_edges(image, 5)
    """

    image = copy(image)
    height = get_height(image)
    width = get_width(image)
    black = create_color(0, 0, 0)
    white = create_color(255, 255, 255)

    for x in range(0, width):
        set_color(image, x, height - 1, white)

    for y in range(0, height - 1):
        for x in range(0, width):
            r, g, b = get_color(image, x, y)
            brightness1 = (r + g + b) // 3
            r2, g2, b2 = get_color(image, x, y + 1)
            brightness2 = (r2 + g2 + b2) // 3
            contrast = abs(brightness1 - brightness2)

            if contrast >= threshold:
                set_color(image, x, y, black)
            else:
                set_color(image, x, y, white)

    return image
Example #6
0
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)

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

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

    return edges_copy
def detect_edges_better(img: Image, threshold: int) -> Image:
    """
    Returns a copy of an image with the pixels changed to either black 
    or white based on the contrast of the pixel above, below, or to the right
    based on the inputed threshold.
    
    Author: Anita Ntomchukwu
    
    >>>detect_edges(img, 5)
    """
    new_img = copy(img)
    height = get_height(img)
    width = get_width(img)
    black = create_color(0, 0, 0)
    white = create_color(255, 255, 255)

    set_color(new_img, width - 1, height - 1, white)

    for x in range(0, width):
        set_color(new_img, x, height - 1, white)

    for y in range(0, height - 1):
        for x in range(0, width):

            color1 = get_color(img, x, y)
            color2 = get_color(img, x, y + 1)
            brightness1 = (color1[0] + color1[1] + color1[2]) // 3
            brightness2 = (color2[0] + color2[1] + color2[2]) // 3

            difference1 = abs(brightness1 - brightness2)

            if difference1 > threshold:
                set_color(new_img, x, y, black)
            else:
                set_color(new_img, x, y, white)

    for y in range(0, height):
        for x in range(0, width - 1):

            color3 = get_color(img, x, y)
            color4 = get_color(img, x + 1, y)
            brightness3 = (color3[0] + color3[1] + color3[2]) // 3
            brightness4 = (color4[0] + color4[1] + color4[2]) // 3

            difference2 = abs(brightness3 - brightness4)

            if difference2 > threshold:
                set_color(new_img, x, y, black)
            else:
                set_color(new_img, x, y, white)

    return new_img
Example #8
0
def extreme_contrast(original_image: Image) -> Image:
    """ Author: Hao Lin 
    Type Annotations: Cimpl.Image -> Cimpl.Image

    This function applies the extreme contrast filter to the image. It converts
    the rgb values of every pixel to either 255 or 0 depending on their range.
    
    >>> new_image= extreme_contrast(original_image,10)
    >>> show(new_image)
    (Shows a version of the image after extreme contrast filter is applied)
    """
    # Constants
    RANGE_LOW =0
    RANGE_MID =127
    RANGE_HIGH =255
    
    HIGH_RED, LOW_RED = 255,0
    HIGH_BLUE, LOW_BLUE = 255,0
    HIGH_GREEN, LOW_GREEN = 255,0
    
    h = get_height(original_image)
    w = get_width(original_image)
    new_image = copy(original_image)
    
    for x in range(w):
        for y in range(h):
            r,g,b = get_color(original_image,x,y)
            
            # Based on the value of rgb they are either replaced with 0 or 255
            if r >= RANGE_LOW and r <= RANGE_MID:
                r = LOW_RED
            elif r > RANGE_MID and r <= RANGE_HIGH:
                r = HIGH_RED
                
            if g >= RANGE_LOW and g <= RANGE_MID:
                g=LOW_GREEN
            elif g>RANGE_MID and g <= RANGE_HIGH:
                g=HIGH_GREEN
                
            if b >= RANGE_LOW and b <= RANGE_MID:
                b = LOW_BLUE
            elif b > RANGE_MID and b <= RANGE_HIGH:
                b = HIGH_BLUE
                
            new_color = create_color(r,g,b)
            set_color(new_image, x,y, new_color)
    return new_image
def combine(image1: Image, image2: Image, image3: Image) -> Image:
    """ Developed by Trong Nguyen, 100848232
    
    Return a new image that is a combination of the RGB components
    of the three user-selected input images.
    
    >>> combine(red_image, green_image, blue_image)
    """
    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)
            new_color = create_color(r1 + r2 + r3, g1 + g2 + g3, b1 + b2 + b3)
            set_color(new_image, x, y, new_color)
    return new_image
Example #10
0
def flip_vertical(image: Image) -> Image:
    """ Returns a vertically flipped copy of the image.
   
    image = load_image(choose_file())
    new_vertical = flip_vertical(image)
    show(new_vertical
    """
    new_image_vertical = copy(image)
   
    width = get_width(new_image_vertical) 
    height = get_height(new_image_vertical)
   
    for w in range(width):
        for h in range(height//2):
            color1 = get_color(new_image_vertical, w, h)
            color2 = get_color(new_image_vertical, w, height -h-1)
            
            set_color(new_image_vertical, w, height-h-1, color1)  
            set_color(new_image_vertical, w, h, color2)            
    return new_image_vertical
def detect_edges_better(image:Image,threshold:float) -> Image:
    '''
    Function Name: detect_edges_better
    Author: Mohamed Kaddour 101140829
    '''
    ''' Takes in an original image as well as a thershold as a float and 
    returns and an image with the improved edge detection filter applied
    based on the value of the threshold. This filter compares the brightness
    of a pixel's neighbouring pixels and then prints on a filter similar to
    that of a pencil drawing.
    >>>show(detect_edges_better(filename,15))
    [Displays proper image]
    
    '''
     
    new_image = copy(image)
    
    width = get_width(new_image)
    height = get_height(new_image)     
    
    for x in range(width-1):
        for y in range(height-1):
              
            r1,g1,b1 = get_color(new_image,x,y)
            r2,g2,b2 = get_color(new_image,x+1,y)
            r3,g3,b3 = get_color(new_image,x,y+1)
            
            brightness1 = (r1+g1+b1)/3
            brightnessv = (r2+g2+b2)/3
            brightnessr = (r3+g3+b3)/3
  
            
            if abs(brightness1-brightnessv) >= threshold or abs(brightness1-brightnessr) >= threshold:
                black = create_color(0,0,0)
                set_color(new_image, x, y, black)      
            else: 
                white = create_color(255,255,255)
                set_color(new_image, x, y, white)
                  
               
    return new_image   
def flip_vertical(original_image: Image) -> Image:
    """ Developed by Hussein Rashid, 101141962
        Reviewed by Trong Nguyen, 100848232
    
    Return a vertically flipped copy of an image from a user-selected 
    original image.  
    
    >>> original_image = load_image(choose_file())
    >>> flip_vertical_image = flip_vertical(original_image)
    """
    new_image = copy(original_image)
    width = get_width(original_image)
    height = get_height(original_image)

    for y in range(height):
        for x in range(width // 2):
            color = get_color(new_image, x, y)
            color_x = get_color(new_image, width - x - 1, y)
            set_color(new_image, x, y, color_x)
            set_color(new_image, width - x - 1, y, color)
    return new_image
def flip_horizontal(original_image: Image) -> Image:
    """ Developed by Ahmed Abdellah, 101163588
        Reviewed by Karandev Andotra, 101141882
    
    Return a horizontally flipped copy of an image from a user-selected 
    original image.  
    
    >>> original_image = load_image(choose_file())
    >>> flip_horizontal_image = flip_horizontal(original_image)
    """
    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 // 2):
            color = get_color(new_image, x, y)
            color_y = get_color(new_image, x, height - y - 1)
            set_color(new_image, x, y, color_y)
            set_color(new_image, x, height - y - 1, color)
    return new_image
Example #14
0
def flip_horizontal(image:Image) ->Image:
    '''
    Function Name: Flip Horizontal 
    Author: Taekwan Oh 101148069
    '''
    """ Returns a horizontally flipped copy of the image.
    >>>show(flip_horizontal(filename))
    [displays expected image]
    """
    new_image_horizontal = copy(image)
   
    width = get_width(new_image_horizontal) - 1 # rotates the height of image
    height = get_height(new_image_horizontal) - 1 # rotates the width of image
   
    for h in range(height):
        for w in range((width//2)+1):
            color1 = get_color(image, w, h)
            color2 = get_color(image, width-w-1, h)
            set_color(new_image_horizontal, width-w-1, h, color1)
            set_color(new_image_horizontal, w, h, color2)
    return new_image_horizontal
def flip_horizontal(inpic: Image) -> Image:
    """Returns a copy of an image that is flipped along a horizontal line.
    
    Author: Yahya Shah
    
    >>>flip_horizontal(image)
    image flipped on a horizontal line down centre
    """
    pic = copy(inpic)

    width = get_width(pic)
    height = get_height(pic)

    for y in range(height // 2):
        for x in range(width):
            colour1 = get_color(pic, x, (height - y - 1))
            colour2 = get_color(pic, x, y)
            set_color(pic, x, (height - y - 1), colour2)
            set_color(pic, x, y, colour1)

    return pic
def flip_horizontal(original_image: Image) -> Image:
    """ Author: Siddharth Natamai - 1011403016
        Date: Nov 19, 2019

    Returns a original_image after flipping along the x-axis (horizontal flip)
    >>> flip_horizontal(original_image)
    <Cimpl.original_image object at 0x7f7ba88dbd10>
    """

    new_image = copy(original_image)
    img_width = get_width(new_image)
    img_height = get_height(new_image)
    img_center = img_height // 2

    for x in range(img_width):
        for y in range(img_center):
            r, g, b = get_color(new_image, x, y)
            r2, g2, b2 = get_color(new_image, x, img_height - y - 1)
            set_color(new_image, x, y, create_color(r2, g2, b2))
            set_color(new_image, x, img_height - y - 1, create_color(r, g, b))

    return new_image
def flip_vertical(image: Image) -> Image:
    """
    Returns a copy of an image that is flipped along a vertical line.
    
    *written by Nathan MacDiarmid*
    
    >>>flip_vertical(image)
    image flipped on a vertical line down centre
    """
    image = copy(image)

    width = get_width(image)
    height = get_height(image)

    for y in range(height):
        for x in range(width // 2):
            col1 = get_color(image, (width - x - 1), y)
            col2 = get_color(image, x, y)
            set_color(image, x, y, col1)
            set_color(image, (width - x - 1), y, col2)

    return image
Example #18
0
def flip_vertical(image: Image) -> Image:
    '''
    Function Name: Flip Vertical
    Author: Kiyara De Silva 101165279
    '''
    """ Returns a vertically flipped copy of the image.
    >>>show(flip_vertical(filename))
    [displays expected image]
    """
    
    new_image_vertical = copy(image)
   
    width = get_width(new_image_vertical) 
    height = get_height(new_image_vertical)
   
    for w in range(width):
        for h in range(height//2):
            color1 = get_color(new_image_vertical, w, h)
            color2 = get_color(new_image_vertical, w, height -h-1)
            
            set_color(new_image_vertical, w, height-h-1, color1)  
            set_color(new_image_vertical, w, h, color2)            
    return new_image_vertical
Example #19
0
def flip_vertical(image: Image) -> Image:
    """Function author : Nathan Gomes - 101143780
    Function takes an image and returns a copy of the image that has been
    flipped vertically (across the "y" axis in an x-y co-ordinate system).

    >>> original_image = load_image("filename")
    >>> flip_vertical(original_image)
    <Cimpl.Image object at 0x000001A90D7A7408>
    """

    new_image = copy(image)
    mid_pixel = get_width(new_image) // 2
    width = get_width(new_image)
    height = get_height(new_image)

    for x in range(mid_pixel):
        for y in range(height):
            r, g, b = get_color(image, x, y)
            new_r, new_g, new_b = get_color(image, abs(width - x) - 1, y)
            set_color(new_image, x, y, create_color(new_r, new_g, new_b))
            set_color(new_image, width - x - 1, y, create_color(r, g, b))

    return new_image
def detect_edges_better(original_image: Image, threshold: int) -> Image:
    """ Developed by Trong Nguyen, 100848232
        Reviewed by Ahmed Abdellah, 101163588
    
    Return an improved 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_better_image = detect_edges_better(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 and not x == width - 1:
                r, g, b = get_color(new_image, x, y)
                r_down, g_down, b_down = get_color(new_image, x, y + 1)
                r_right, g_right, b_right = get_color(new_image, x + 1, y)

                brightness = (r + g + b) // 3
                brightness_down = (r_down + g_down + b_down) // 3
                brightness_right = (r_right + g_right + b_right) // 3

                if abs(brightness - brightness_down) > threshold \
                or abs(brightness - brightness_right) > 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)
        new_color = create_color(255, 255, 255)
        set_color(new_image, x, y, new_color)
    return new_image