Example #1
0
def interactive_ui() -> None:
    """ __author__ = "Trong Nguyen"
    
    Prompt the interactive user-interface.
    
    >>> interactive_ui()
    """
    prompts = ["L", "S", "2", "3", "X", "T", "P", "E", "I", "V", "H"]
    
    new_image = None
    on = True
    while on:
        command = get_command()  
        if command in prompts:
            if new_image:
                if command == "L":
                    new_image = load()
                else:   
                    new_image = execute_command(command, new_image)
                    show(new_image)
            else:
                if command == "L":
                    new_image = load()              
                else:
                    print("\t=> No image loaded\n")                                 
        elif command == "Q":
            on = False
        else:
            print("\t=> No such command\n")
def open_image() -> Image:
    """ Author: Ian Holmes 
    Type annotation: (None) -> Cimple.Image
    
    This functions asks prompts the user to select an image that they want to 
    apply the filter to.
    
    >>> open_image()
    (asks the user to select their image)
    (shows the image to the user)
    """
    
    FILENAME = choose_file()
    if FILENAME != "":
        original_image = load_image(FILENAME)
        show(original_image)
        return original_image
    else:
        print("No image loaded")
Example #3
0
            set_color(new_image, width-x-1,y, col1)
            
    return new_image


#Main Code      
# Assumption: There is a image stored in the same folder as this script
# with the given name

if __name__ == "__main__":      
    #Loading images 
    FILENAME_ORIGINAL = choose_file()
    original_image = load_image(FILENAME_ORIGINAL)
        
    print("Showing the original image")
    show(original_image)
    
    #Red Filter
    print("Applying the red filter")
    red_image=red_channel (original_image)
    show(red_image)
    
    #Green Filter
    print("Applying the green filter")
    green_image=green_channel (original_image)
    show(green_image)
    
    #Blue Filter
    print("Applying the blue filter")
    blue_image=blue_channel (original_image)
    show(blue_image)
Example #4
0
    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



filename = load_image(choose_file())
show(filename)

#MIlESTONE 1
redimage = red_channel(filename)
show(redimage)
greenimage = green_channel(filename)
show(greenimage)
blueimage = blue_channel(filename)
show(blueimage)
show(combine(redimage,greenimage,blueimage))

#MILESTONE 2
show(two_tone(filename, "black", "white"))
show(three_tone(filename, "black", "white","cyan"))
show(extreme_contrast(filename))
show(sepia(filename))
Example #5
0
from Cimpl import choose_file, load_image, copy, create_color, set_color,\
                  show, Image, get_color, create_image, get_width, get_height,\
                  save_as

run = True
image = None

while run == True:
    filterinput = input(
        "L)oad image  S)ave-as\n2)-tone  3)-tone  X)treme contrast" +
        " T)int sepia  P)osterize\nE)edge detect  I)mproved edge detect" +
        " V)ertical flip  H)orizontal flip\nQ)uit\n\n: ")

    if filterinput == "L" or filterinput == "l":
        image = copy(load_image(choose_file()))
        show(image)

    elif filterinput == "S" or filterinput == "s":
        if image is None:
            print("Please load an image")
        else:
            filename = input("Please enter a filename with the type of file" +
                             "(example: mypicture.jpg)\n: ")
            save_as(image, filename)

    elif filterinput == '2':
        if image is None:
            print("Please load an image")
        else:
            image = two_tone(image, "yellow", "cyan")
            show(image)
    Testing Combined Image...
    Testing Pixels: 225 473 RGB: (151, 144, 90) with RGB: (151, 144, 90)
    ...
    ...
    ...
    '''
   
    print("Testing Combined Image...")
    for pixel in combined_image:
        x, y, (r, g, b) = pixel
        comb_pix = r,g,b
        r1,g,b = get_color(red_image,x,y)
        r,g1,b = get_color(green_image,x,y)
        r,g,b1 = get_color(blue_image,x,y)
        acc_pix = (r1,g1,b1)
        print ("Testing Pixels:",x,y,"RGB:",comb_pix,"with RGB:", acc_pix) 
        if (acc_pix) != (comb_pix):
            print("FAILED")
        else:
            print("PASSED")

r_image = "red_image.png"
g_image = "green_image.png"
b_image = "blue_image.png"
r_image = load_image(r_image)
g_image = load_image(g_image)
b_image = load_image(b_image)

combined_image = combine(r_image, g_image, b_image)
show(combined_image)
combine_test(r_image, g_image, b_image, combined_image)
    return new_image


def _adjust_component(original_val: int) -> int:
    """Determines where each pixel lies in the 4 quadrants (0 to 63, 64 to 127,
     128 to 191, and 192 to 255)
    and sets the new pixel values to the midpoint of that specific quadrant.
    >>> _adjust_component(50)
    31
    >>> _adjust_component(90)
    95
    >>> _adjust_component(155)
    159
    """

    if original_val <= 63:
        new_val = 31
    elif original_val <= 127:
        new_val = 95
    elif original_val <= 191:
        new_val = 159
    elif original_val <= 255:
        new_val = 223
    return new_val


# print(_adjust_component(192))
#
show(posterize(original_image))
# save(posterize(original_image))
Example #8
0
from Cimpl import choose_file, load_image, copy, create_color, set_color,\
                  show, Image, get_color


def black_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
        r = 30
        black = create_color(r, 0, 0)
        set_color(new_image, x, y, black)
    return new_image


show(black_channel(copy(load_image("Bear.jpg"))))
    '''
    #Creates the test image
    original = create_image(6, 1)
    set_color(original, 0, 0, create_color(0, 0, 0))
    set_color(original, 1, 0, create_color(0, 128, 128))
    set_color(original, 2, 0, create_color(127, 127, 127))
    set_color(original, 3, 0, create_color(125, 73, 224))
    set_color(original, 4, 0, create_color(254, 255, 255))
    set_color(original, 5, 0, create_color(126, 127, 128))

    #Proper Image after extreme contrast is applied
    actual = create_image(6, 1)
    set_color(actual, 0, 0, create_color(0, 0, 0))
    set_color(actual, 1, 0, create_color(0, 255, 255))
    set_color(actual, 2, 0, create_color(0, 0, 0))
    set_color(actual, 3, 0, create_color(0, 0, 255))
    set_color(actual, 4, 0, create_color(255, 255, 255))
    set_color(actual, 5, 0, create_color(0, 0, 255))

    twot_image = extreme_contrast(original)
    for x, y, col in twot_image:
        check_equal('Checking pixel @(' + str(x) + ', ' + str(y) + ')', col,
                    get_color(actual, x, y))


filename = "p2-original.jpg"
filename = copy(load_image(filename))
threep_img = extreme_contrast(filename)
show(threep_img)
test_extreme_contrast()
            print("FAILED")
        else:
            print("PASSED")


'''Program Body'''
filename = "p2-original.jpg"
filename = copy(load_image(filename))

red_image = red_channel(filename)
green_image = green_channel(filename)
blue_image = blue_channel(filename)
combined_image = combine(red_image, green_image, blue_image)

#Displaying
print("Showing Red Image")
show(red_image)
print("Showing Green Image")
show(green_image)
print("Showing Blue Image")
show(blue_image)
print("Showing Combined Image")
show(combined_image)

#Testing:

test_red_channel(red_image)
test_green_channel(green_image)
test_blue_channel(blue_image)
combine_test(red_image, green_image, blue_image, combined_image)
Example #11
0
COLOUR3 = "cyan"

# A list of all the possible commands
ACCEPTED_INPUTS = ["L","S","2","3","X","T","P","E","I","V","H","Q"]

# A list of all the filters.
FILTERS = [two_tone, three_tone, extreme_contrast, sepia, posterize, 
           detect_edges, detect_edges_better, flip_vertical,flip_horizontal]


while command != "Q": # Stops the program when the user enters 'Q'
    command = get_command()
    
    if command != "Q":
        if command == "L":
            original_image = open_image ()
            
        # Makes sure there is an image already selected before letting the user
        # Choose any filters
        elif original_image == None:
            print ("No image loaded")
            
        # If the user the wants to save the image it prompts them for the name    
        elif command == "S":
            save_as (original_image)
            
        else:
            original_image = apply_filter (original_image, command)
            show(original_image)

from Cimpl import choose_file, load_image, copy, create_color, set_color, \
    show, Image, get_color


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


if __name__ == "__main__":
    image = load_image(choose_file())
    new_image = red_channel(image)
    show(new_image)
Example #13
0
    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   

filename = "p2-original.jpg"
filename = copy(load_image(filename))
show(detect_edges_better(filename,15))
test_detect_edges_better()
Example #14
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


filename = "p2-original.jpg"
filename = copy(load_image(filename))
twop_img = two_tone(filename, "black", "white")
show(twop_img)
    set_color(blue_img, 5, 0, create_color(0, 0, 255))

    # Create an image that's identical to the one a correct implementation of
    # combine should produce when it is passed the red_img, green_img and blue_img as inputs.
    expected = create_image(6, 1)
    set_color(expected, 0, 0, create_color(0, 0, 0))
    set_color(expected, 1, 0, create_color(13, 0, 1))
    set_color(expected, 2, 0, create_color(255, 127, 127))
    set_color(expected, 3, 0, create_color(125, 73, 224))
    set_color(expected, 4, 0, create_color(254, 255, 255))
    set_color(expected, 5, 0, create_color(87, 13, 255))

    # Now compare the transformed image returned by the filter with the
    # expected image, one pixel at a time.

    combined_img = combine(red_img, green_img, blue_img)
    for x, y, col in combined_img:  # col is the Color object for the pixel @ (x,y)
        # There's no need to unpack that object into
        # RGB components.
        check_equal('Checking pixel @(' + str(x) + ', ' + str(y) + ')', col,
                    get_color(expected, x, y))


if __name__ == "__main__":
    red_img = load_image(choose_file())
    green_img = load_image(choose_file())
    blue_img = load_image(choose_file())
    # combine images
    combined = combine(red_img, green_img, blue_img)
    show(combined)
    test_combine()
Example #16
0
    original = create_image(2, 6)
    set_color(original, 0, 0, create_color(0, 0, 0))
    set_color(original, 0, 1, create_color(55, 35, 29))
    set_color(original, 0, 2, create_color(22, 12, 43))
    set_color(original, 0, 3, create_color(55, 83, 26))
    set_color(original, 0, 4, create_color(123, 85, 200))
    set_color(original, 0, 5, create_color(122, 82, 211))

    expected = create_image(2, 6)
    set_color(expected, 0, 0, create_color(0, 0, 0))
    set_color(expected, 0, 1, create_color(255, 255, 255))
    set_color(expected, 0, 2, create_color(255, 255, 255))
    set_color(expected, 0, 3, create_color(0, 0, 0))
    set_color(expected, 0, 4, create_color(255, 255, 255))
    set_color(expected, 0, 5, create_color(122, 82, 211))
    '''NOTE: Can't expect pixels (2,0) and (1,2) because there are no pixels
    defined that are below or to the right of them (No 3 pixel defined in x or
    or y). '''

    threshold = 30  #Assumed threshold
    test_edge = detect_edges(original, threshold)
    for x, y, col in test_edge:
        check_equal('Checking pixel @(' + str(x) + ', ' + str(y) + ')', col,
                    get_color(expected, x, y))


filename = "p2-original.jpg"
filename = copy(load_image(filename))
show(detect_edges(filename, 15))

test_detect_edges()