def apply_filters(filename: str, commands: list) -> Cimpl.Image:
    """
    Authors: Zakaria Ismail, Yanglong Liu

    RETURNS a Cimpl.Image object and applies filters
    to an image after being PASSED filename and commands

    filename is a string directing to the location of the image file

    commands is a list containing the image filter sequences

    >>> apply_filters('filename.png', ['2', 'X', 'V', 'P'])
    """
    filter_functions = {
        '2': two_tone,
        '3': three_tone,
        'X': extreme_contrast,
        'T': sepia,
        'P': posterize,
        'E': detect_edges,
        'I': detect_edges_better,
        'V': flip_vertical,
        'H': flip_horizontal,
    }

    image = Cimpl.load_image(filename)

    i = 0
    while i < len(commands):
        # Note: Functions that have parameters have default arguments set in T121_image_filters.py
        image = filter_functions[commands[i]](image)
        i += 1
    return image
예제 #2
0
def load_image() -> Cimpl.Image:
    """
    Author: Ibrahim Kasim, Himanshu Singh

    RETURNS a Cimpl.Image object.

    Prompts the user to select
    an image file to load, and
    displays the loaded image

    >>> load_image()
    """
    print("Select an image file to load")
    image = Cimpl.load_image(Cimpl.choose_file())
    Cimpl.show(image)
    return image
def green_channel():
    """
    The Author: Ibrahim Kasim
    green_channel() -> Cimpl.Image:
    Returns green filtered image. It directs user to choose an image that 
    is desired for filtering.
    
    """

    image_file = Cimpl.choose_file()
    original_image = Cimpl.load_image(image_file)
    new_image = Cimpl.copy(original_image)
    for pixel in original_image:
        x, y, (r, g, b) = pixel
        green_color = Cimpl.create_color(0, g, 0)
        Cimpl.set_color(new_image, x, y, green_color)

    return new_image
예제 #4
0
def algorithm():
    for image in list(list_of_image):
        path = '%s/%s' % (pathh, image)
        img = Cimpl.load_image(path)

        def lower_brightness(img):

            average_brightness = 0
            min_brightness = 60

            for x, y, col in img:

                r, g, b = col

                brightness = (r+g+b) // 3

                average_brightness += brightness

            average_brightness //= (150*150)

            if average_brightness >= min_brightness:

                for x, y, col in img:

                    r, g, b = col

                    new_col = create_color((r*(3/4)), (g*(3/4)), (b*(3/4)))
                    set_color(img, x, y, new_col)

        def black_and_white(img, threshold):
            """ (Cimpl.Image) -> None

            Convert the specified image to a black-and-white (two-tone) image.

            >>> image = load_image(choose_file())
            >>> black_and_white(image)
            >>> show(image)
            """

            # Brightness levels range from 0 to 255.
            # Change the colour of each pixel to black or white, depending on whether
            # its brightness is in the lower or upper half of this range.

            black = create_color(0, 0, 0)
            white = create_color(255, 255, 255)
            number = 0
            for x, y, col in img:
                red, green, blue = col

                brightness = (red + green + blue) // 3

                if brightness < threshold:
                    set_color(img, x, y, black)
                else:     # brightness is between threshold and 255, inclusive
                    set_color(img, x, y, white)
                    number += 1

        def scan(img):
            """(Cimpl.Image) -> (Int)

            Scans image for white pixel chunks. Returns the size of the largest chunk.
            """
            black_and_white(img, 75)
            lst = []

            for x, y, col in img:
                red, green, blue = col
                if (x > 10) and (x < 140) and (y > 10) and (y < 140):
                    if red == 255:
                        lst.append(sumplusplus(img, x, y))

            largest = 0
            for number in lst:
                if largest <= number:
                    largest = number

            return largest

        green_color = create_color(0, 255, 0)

        def sumplusplus(img, x, y):
            """(Cimpl.Image, Int, Int) -> (Int)

            Recursively determines the size of a chunk in pixels.
            """
            col = get_color(img, x, y)
            r, g, b = col
            if r == 255:
                sump = 0
                set_color(img, x, y, green_color)
                sump += 1
                if x + 1 < 150:
                    sump += sumplusplus(img, x + 1, y)
                    if y + 1 < 150:
                        sump += sumplusplus(img, x + 1, y + 1)
                    if y - 1 > 0:
                        sump += sumplusplus(img, x + 1, y - 1)
                if x - 1 > 0:
                    sump += sumplusplus(img, x - 1, y)
                    if y + 1 < 150:
                        sump += sumplusplus(img, x - 1, y - 1)
                    if y - 1 > 0:
                        sump += sumplusplus(img, x - 1, y + 1)
                if y + 1 < 150:
                    sump += sumplusplus(img, x, y + 1)
                if y - 1 > 0:
                    sump += sumplusplus(img, x, y - 1)

                return sump
            else:
                return 0

        def filter_resolution(img):
            """
            (Cimpl.image)->(Cimple.image)
            Creates a more general picture by using the average color to
            all pixels in 3x3 grid
            >>> image = load_image(choose_file())
            >>> show(filter_resolution(image))
            """

            lastpixel = (0, 0)

            for x, y, col in img:
                (xL, yL) = lastpixel

                sum_color = 0

                if (x >= 10 and x <= 140) and (y >= 10 and x <= 140):
                    #Inside border

                    if (x >= (3 + xL)) or (y >= (3 + yL)):
                        #Is 1 box width away

                        for x2 in range(-1, 2):

                            for y2 in range(-1, 2):

                                color = get_color(img, (x + x2), (y + y2))
                                r, g, b = color

                                sum_color += ((r + g + b)//3) # will add either 0 or 255

                        if(sum_color >= 1275): # 5 or more pixels of out 9 are white, changes all 9 to white

                            for x2 in range(-1, 2):

                                for y2 in range(-1, 2):
                                    new_color = create_color(255, 255, 255)
                                    set_color(img, (x + x2), (y + y2), new_color)

                        else: # 5 or more pixels of out 9 are black, changes all 9 to black

                            for x2 in range(-1, 2):

                                for y2 in range(-1, 2):
                                    new_color = create_color(0, 0, 0)
                                    set_color(img, (x + x2), (y + y2), new_color)

                        lastpixel = (x, y)

            return img

        def final_check(img):

            image_value = scan(img)

            if 750 < image_value < 1300:
                return 'Pass'
            else:
                return 'Fail'

        lower_brightness(img)
        ans = final_check(img)
        result.append('%s = %s' % (image, ans))
    str1 = ','.join(result)
    f = open("Results.txt","a")
    f.write(str1)
    f.close()
    toplevel = Toplevel()
    label1 = Label(toplevel, text="All images were processed and saved in Results.txt", height=0, width=50)
    label1.grid(column=0, row=0)
예제 #5
0
                      "Outcome: {}".format(x, y, exp_col, out_col))
                errors += 1
        if errors == 0:
            print(
                "SUCCESS: expected and outcome are of the same type and have identical pixels."
            )
        else:
            print("{} ERRORS detected.".format(errors))
    else:
        print("ERROR: Different types detected.\n"
              "Expected: Type {}\n"
              "Outcome: Type {}".format(type(expected), type(outcome)))


print("---TESTING FUNCTION green_channel()---")
expected = Cimpl.load_image(input("Select expected image filename: "))
print("DISPLAYING EXPECTED IMAGE: ")
Cimpl.show(expected)

testfile = Cimpl.load_image(
    input(
        "Input filename of image to be passed through FUNCTION green_channel: "
    ))
print("DISPLAYING TEST IMAGE: ")
Cimpl.show(testfile)

print("PASSING TEST IMAGE INTO function green_channel: ")
outcome = green_channel(testfile)
print("DISPLAYING OUTCOME: ")
Cimpl.show(outcome)
print("COMPARING EXPECTED AND OUTCOME")
                      "Outcome: {}".format(x, y, exp_col, out_col))
                errors += 1
        if errors == 0:
            print(
                "SUCCESS: expected and outcome are of the same type and have identical pixels."
            )
        else:
            print("{} ERRORS detected.".format(errors))
    else:
        print("ERROR: Different types detected.\n"
              "Expected: Type {}\n"
              "Outcome: Type {}".format(type(expected), type(outcome)))


print("---TESTING FUNCTION red_channel()---")
expected = Cimpl.load_image(
    input("Select expected (A RED RESULT) image filename: "))
print("DISPLAYING EXPECTED IMAGE: ")
Cimpl.show(expected)

testfile = Cimpl.load_image(
    input(
        "Input filename of image to be passed (AN UNCOLORED IMAGE) through FUNCTION red_channel: "
    ))
print("DISPLAYING TEST IMAGE: ")
Cimpl.show(testfile)

print("PASSING TEST IMAGE INTO function red_channel: ")
outcome = red_channel(testfile)
print("DISPLAYING OUTCOME: ")
Cimpl.show(outcome)
예제 #7
0
        #Convert the altered pixel green component in binary from a list to a string
        new_green = ''.join(str(e) for e in pixel_green_binary)
        new_green = int(new_green, 2)

        #Convert the altered pixel blue component in binary from a list to a string
        new_blue = ''.join(str(e) for e in pixel_blue_binary)
        new_blue = int(new_blue, 2)

        #Create and Set color
        new_color = Cimpl.create_color(new_red, new_green, new_blue)
        Cimpl.set_color(img, x, y, new_color)


#load image
image = Cimpl.load_image(Cimpl.choose_file())


#Convert user input string to binary
string = input("Input the text you would like to hide:")
string = "*****" + string + '|||||'
string_in_bin = text_to_bits(string)
string_in_bin = list(string_in_bin)


#calculate how many bits we can alter
image_width = Cimpl.get_width(image)
image_height = Cimpl.get_height(image)
open_bits = image_width*image_height*3

    Author: Zakaria Ismail

    RETURNS the sum of three numbers
    PASSED. If sum exceeds 255, then
    the sum is 255.

    >>> compute_sum(5,6,7)
    18
    """
    if r + g + b <= 255:
        return r + g + b
    else:
        return 255


original = Cimpl.load_image(input("Input image filename: "))
print("DISPLAYING IMAGE: ")
Cimpl.show(original)

print("COMPUTING RED FILTERED IMAGE")
red = red_channel(original)
print("DISPLAYING RED FILTERED IMAGE: ")
Cimpl.show(red)

print("COMPUTING GREEN FILTERED IMAGE")
green = green_channel(original)
print("DISPLAYING GREEN FILTERED IMAGE: ")
Cimpl.show(green)

print("COMPUTING BLUE FILTERED IMAGE")
blue = blue_channel(original)
예제 #9
0
                      "Expected: {}\n"
                      "Outcome: {}".format(x, y, exp_col, out_col))
                errors += 1
        if errors == 0:
            print(
                "SUCCESS: expected and outcome are of the same type and have identical pixels."
            )
        else:
            print("{} ERRORS detected.".format(errors))
    else:
        print("ERROR: Different types detected.\n"
              "Expected: Type {}\n"
              "Outcome: Type {}".format(type(expected), type(outcome)))


expected = Cimpl.load_image(input('Input EXPECTED image filename: '))
print("DISPLAYING EXPECTED IMAGE")
Cimpl.show(expected)

red = Cimpl.load_image(
    input("Input red image filename: "))  # choose_file does not work for me.
print("DISPLAYING RED IMAGE")
Cimpl.show(red)
green = Cimpl.load_image(input("Input green image filename: "))
print("DISPLAYING GREEN IMAGE")
Cimpl.show(green)
blue = Cimpl.load_image(input("Select blue image filename: "))
print("DISPLAYING BLUE IMAGE")
Cimpl.show(blue)

print("COMBINING IMAGES")