コード例 #1
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    photo_number = len(images)
    pixels = []
    result = SimpleImage.blank(width, height)

    for i in range(height):
        for j in range(width):
            for k in range(photo_number):
                get_pixel = images[k].get_pixel(j, i)
                pixels.append(get_pixel)
            best = get_best_pixel(pixels)
            result_get = result.get_pixel(j, i)
            result_get.red = best.red
            result_get.green = best.green
            result_get.blue = best.blue
            pixels = []

    ######## YOUR CODE STARTS HERE #########

    # Write code to populate image and create the 'ghost' effect
    image_blank = SimpleImage.blank(width, height)
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #2
0
def crop_image(filename, n):
    """
    Takes the left half of image, and copies it on top of the right half.
    """
    image = SimpleImage(filename)
    width = image.width
    new_width = width - (2 * n)
    height = image.height
    new_height = height - (2 * n)
    image_crop_width = SimpleImage.blank(new_width, height)
    for y in range(height):
        for x in range(new_width):
            pixel = image.get_pixel((x + n), y)
            image_crop_width.set_pixel(x, y, pixel)
    image_crop_width.show()

    image_crop_height = SimpleImage.blank(width, new_height)
    for y in range(new_height):
        for x in range(width):
            pixel = image.get_pixel(x, y + n)
            image_crop_height.set_pixel(x, y, pixel)
    image_crop_height.show()

    image_crop_width_height = SimpleImage.blank(new_width, new_height)
    for y in range(new_height):
        for x in range(new_width):
            pixel = image.get_pixel(x + n, y + n)
            image_crop_width_height.set_pixel(x, y, pixel)
    image_crop_width_height.show()
コード例 #3
0
def trim_crop_image(original_img, trime_size):
    """
    This function returns a new SimpleImage which is a trimmed and
    cropped version of the original image by shaving trime_size pixels
    from each side (top, left, bottom, right) of the image. You may
    assume trime_size is less than half the width of the image.

    Inputs:
        - original_img: The original image to process
        - trime_size: The number of pixels to shave from each side
                   of the original image

    Returns:
        A new SimpleImage with trime_size pixels shaved off each
        side of the original image
    """
    new_width = original_img.width - 2 * trime_size
    new_height = original_img.height - 2 * trime_size
    new_img = SimpleImage.blank(new_width, new_height)

    for x in range(new_width):
        for y in range(new_height):
            old_x = x + trime_size
            old_y = y + trime_size
            orig_pixel = original_img.get_pixel(old_x, old_y)
            new_img.set_pixel(x, y, orig_pixel)

    return new_img
コード例 #4
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    for x in range(width):
        for y in range(height):
            pixels = []
            for image in images:
                pixels.append(image.get_pixel(x, y))
            best = get_best_pixel(pixels)
            result_pixel = result.get_pixel(x, y)
            result_pixel.red = best.red
            result_pixel.green = best.green
            result_pixel.blue = best.blue
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #5
0
def reflect(filename):
    """
    This function uses the original image to create a reflected image.
    :param filename: str, the file path of the original image
    :return: a new image with the original image and the mirrored one
    """
    img = SimpleImage(filename)
    img_new = SimpleImage.blank(img.width, img.height * 2)
    for y in range(img.height):
        for x in range(img.width):
            pixel_img = img.get_pixel(x, y)

            # divide the new blank canvas into two parts
            pixel_new1 = img_new.get_pixel(x, y)
            pixel_new2 = img_new.get_pixel(x, img_new.height - y - 1)

            # fill the first empty pixel(upper part)
            pixel_new1.red = pixel_img.red
            pixel_new1.green = pixel_img.green
            pixel_new1.blue = pixel_img.blue

            # fill the second empty pixel(mirrored part)
            pixel_new2.red = pixel_img.red
            pixel_new2.green = pixel_img.green
            pixel_new2.blue = pixel_img.blue
    return img_new
コード例 #6
0
def blur(old_img):
    """
    :param old_img: SimpleImage, the original image
    :return: new_img: SimpleImage, the image after blur
    """

    old_img = SimpleImage("images/smiley-face.png")
    # blank image for blurred
    new_img = SimpleImage.blank(old_img.width, old_img.height)
    for x in range(old_img.width):
        for y in range(old_img.height):
            # initiate with 0
            r_sum = 0
            g_sum = 0
            b_sum = 0
            count = 0
            # find pixel of x-1, x, x+1 and y-1, y, y+1 and add it with the neighbors
            for i in range(-1, 2, 1):
                for j in range(-1, 2, 1):
                    pixel_x = x + i
                    pixel_y = y + j
                    if 0 <= pixel_x < old_img.width:
                        if 0 <= pixel_y < old_img.height:
                            pixel = old_img.get_pixel(pixel_x, pixel_y)
                            r_sum += pixel.red
                            g_sum += pixel.green
                            b_sum += pixel.blue
                            # count the number of neighbors
                            count += 1
            new_pixel = new_img.get_pixel(x, y)
            new_pixel.red = r_sum / count
            new_pixel.green = g_sum / count
            new_pixel.blue = b_sum / count
    return new_img
コード例 #7
0
ファイル: blur.py プロジェクト: pfkevinma/stanCode_Projects
def blur(img):
    """
    :param img: str
    :return: SimpleImage
    This function will blur the image by make pixels RGB value to the average value with nearby pixels.
    1. Create blank image
    2. loop the pixels original image
    3. loop the nearby pixels RGB value
    4. if the nearby pixel in the range of the picture, include into calculation.
    """
    img = img
    new_img = SimpleImage.blank(img.width, img.height)
    for x in range(img.width):
        for y in range(img.height):
            red = 0
            green = 0
            blue = 0
            num = 0
            for i in range(x - 1, x + 2):
                for j in range(y - 1, y + 2):
                    if img.width > i >= 0 and img.height > j >= 0:
                        pixel_img = img.get_pixel(i, j)
                        red += pixel_img.red
                        green += pixel_img.green
                        blue += pixel_img.blue
                        num += 1
            pixel_new = new_img.get_pixel(x, y)
            pixel_new.red = red // num
            pixel_new.green = green // num
            pixel_new.blue = blue // num
    return new_img
コード例 #8
0
def add_border(original_img, border_size):
    """
    This function returns a new SimpleImage which is the same as
    original image except with a black border added around it. The
    border should be border_size many pixels thick.

    Inputs:
        - original_img: The original image to process
        - border_size: The thickness of the border to add around the image

    Returns:
        A new SimpleImage with the border added around original image
    """
    # add twice the border size since we will have a border on both sides
    new_width = original_img.width + 2*border_size
    new_height = original_img.height + 2*border_size

    # gives us a blank image of size new_width and new_height
    bordered_img = SimpleImage.blank(new_width, new_height)

    for x in range(bordered_img.width):
        for y in range(bordered_img.height):
            if is_border_pixel(x, y, border_size, bordered_img):
                pixel = bordered_img.get_pixel(x, y)
                pixel.red = 0
                pixel.green = 0
                pixel.blue = 0
            else:
                # need to set pixel originally at (x,y) to shifted new position
                orig_x = x - border_size
                orig_y = y - border_size
                orig_pixel = original_img.get_pixel(orig_x, orig_y)
                bordered_img.set_pixel(x, y, orig_pixel)

    return bordered_img
コード例 #9
0
def reflect(filename):
    """
    :param filename: str, the file path of the original image
    :return:SimpleImage, image generated by a mirror-reversal of an original across a horizontal axis
    """
    img = SimpleImage('images/mt-rainier.jpg')
    # Make an empty canvas
    blank_img = SimpleImage.blank(img.width, img.height*2)

    for y in range(img.height):
        for x in range(img.width):  # it has pixel data!
            img_pixel = img.get_pixel(x, y)

            # empty spot 1
            blank_p1 = blank_img.get_pixel(x, y)
            # empty spot 2
            blank_p2 = blank_img.get_pixel(x, blank_img.height-1-y)

            # coloring
            blank_p1.red = img_pixel.red
            blank_p1.blue = img_pixel.blue
            blank_p1.green = img_pixel.green
            # coloring
            blank_p2.red = img_pixel.red
            blank_p2.blue = img_pixel.blue
            blank_p2.green = img_pixel.green

    return blank_img
コード例 #10
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    pixel_lst = []

    for i in range(width):
        for j in range(height):
            for image in images:
                pixel = image.get_pixel(i, j)
                pixel_lst.append(pixel)
            pixel = get_best_pixel(pixel_lst)
            pixel_result = result.get_pixel(i, j)
            pixel_result.red = pixel.red
            pixel_result.green = pixel.green
            pixel_result.blue = pixel.blue
            pixel_lst = []

    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #11
0
ファイル: blur.py プロジェクト: pcdd05/stanCode_python
def blur(img):
    """
    Using pixel coordinates to get the pixel and its neighbors for average of rgb and then get blurred.
    :param img: SimpleImage, the image object which will be blurred.
    :return: SimpleImage, the image which were all blurred.
    """
    new_img = SimpleImage.blank(img.width, img.height)
    for x in range(new_img.width):
        for y in range(new_img.height):
            sum_red = 0
            sum_green = 0
            sum_blue = 0
            neighbor_pixels_num = 0
            new_img_p = new_img.get_pixel(x, y)
            for i in range(3):
                for j in range(3):
                    if 0 <= x + (i-1) < img.width and 0 <= y + (j-1) < img.height:
                        neighbor_pixels_num += 1
                        sum_red += img.get_pixel(x + (i-1), y + (j-1)).red
                        sum_green += img.get_pixel(x + (i-1), y + (j-1)).green
                        sum_blue += img.get_pixel(x + (i-1), y + (j-1)).blue
            new_img_p.red = sum_red // neighbor_pixels_num
            new_img_p.green = sum_green // neighbor_pixels_num
            new_img_p.blue = sum_blue // neighbor_pixels_num
    return new_img
コード例 #12
0
def triplicate(filename):
    image = SimpleImage(filename)
    new = SimpleImage.blank(image.width * 3, image.height)
    for y in range(image.height):
        for x in range(image.width):
            old = image.get_pixel(x, y)

            # left image
            left = new.get_pixel(x, image.height - 1 - y)
            left.red = old.red
            left.green = old.green
            left.blue = old.blue

            # middle image
            middle = new.get_pixel(x + image.width, y)
            middle.red = old.red
            middle.green = old.green
            middle.blue = old.blue

            # right image
            right = new.get_pixel(image.width - 1 - x, y)
            right.red = old.red
            right.green = old.green
            right.blue = old.blue
    pass
コード例 #13
0
ファイル: trim_crop.py プロジェクト: sidtrip/hello_world
def trim_crop_image(original_img, trim_size):
    """
    This function returns a new SimpleImage which is a trimmed and
    cropped version of the original image by shaving trim_sz pixels
    from each side (top, left, bottom, right) of the image. You may
    assume trim_sz is less than half the width of the image.

    Inputs:
        - original_img: The original image to process
        - trim_size: The number of pixels to shave from each side
                   of the original image

    Returns:
        A new SimpleImage with trim_sz pixels shaved off each
        side of the original image
    """
    nudimx = original_img.width - 2 * trim_size
    nudimy = original_img.height - 2 * trim_size
    new = SimpleImage.blank(nudimx, nudimy)
    for y in range(new.height):
        for x in range(new.width):
            newx = x + trim_size - 1
            newy = y + trim_size - 1
            new.set_pixel(x, y, original_img.get_pixel(newx, newy))
    return new
コード例 #14
0
ファイル: add_border.py プロジェクト: edithturn/code-in-place
def add_border(original_img, border_size):
    """
    This function returns a new SimpleImage which is the same as
    original image except with a black border added around it. The
    border should be border_size many pixels thick.

    Inputs:
        - original_img: The original image to process
        - border_size: The thickness of the border to add around the image

    Returns:
        A new SimpleImage with the border added around original image
    """
    # TODO: your code here
    new_width = original_img.width + 2 * border_size
    new_height = original_img.height + 2 * border_size
    new_image = SimpleImage.blank(new_width, new_height)

    for x in range(new_image.width):
        for y in range(new_image.height):
            if is_border_pixel(x, y, border_size, new_image):
                pixel = new_image.get_pixel(x, y)
                pixel.red = 0
                pixel.blue = 0
                pixel.green = 0
            else:
                old_x = x - border_size
                old_y = y - border_size
                old_pixel = original_img.get_pixel(old_x, old_y)
                new_image.set_pixel(x, y, old_pixel)

    return new_image
コード例 #15
0
def get_blur_color(x, y):
    """
    This function calculate the Red, Green, and Blue from each
    point. Then, assign the blur color to (x,y).
    --------------------------------------------------
    :param x: int, the column of each spot.
    :param y: int, the row of each spot.
    :return: new_pixel: the R, G, and B of that point pixel.
    """
    img = SimpleImage("images/smiley-face.png")
    new_img = SimpleImage.blank(img.width, img.height)
    new_pixel = new_img.get_pixel(x, y)
    start_x = 0
    start_y = 0
    end_x = 0
    end_y = 0

    if x == 0:
        start_x = 0
        end_x = x + 2
        start_y = 0
        end_y = y + 2
    elif x == img.width - 1:
        start_x = x - 1
        end_x = x + 1
    else:
        start_x = x - 1
        end_x = x + 2

    if y == 0:
        start_y = 0
        end_y = y + 2
    elif y == img.height - 1:
        start_y = y - 1
        end_y = y + 1
    else:
        start_y = y - 1
        end_y = y + 2
    total_r = 0
    total_g = 0
    total_b = 0
    counts = 0

    for y in range(start_y, end_y):
        for x in range(start_x, end_x):
            pixel = img.get_pixel(x, y)
            total_r += pixel.red
            total_g += pixel.green
            total_b += pixel.blue
            counts += 1
    print('total r g b', total_r, total_g, total_b)     # make sure the algorithm is running

    new_r = total_r / counts
    new_g = total_g / counts
    new_b = total_b / counts
    new_pixel.red = new_r
    new_pixel.green = new_g
    new_pixel.blue = new_b

    return new_pixel
コード例 #16
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    # The first two for-loops (used with indexes i and j) are used for filling up blank image with the best pixels
    # The third loop (for-each loop) is used for create the RGB list of original images
    for i in range(width):
        for j in range(height):
            images_pixel = []
            for image in images:
                red = image.get_pixel(i, j).red
                green = image.get_pixel(i, j).green
                blue = image.get_pixel(i, j).blue
                images_pixel.append([red, green, blue])
            best_pixel = get_best_pixel(images_pixel)
            result.get_pixel(i, j).red = best_pixel[0]
            result.get_pixel(i, j).green = best_pixel[1]
            result.get_pixel(i, j).blue = best_pixel[2]

    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #17
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    print(1)
    pixels = []
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    for x in range(width):  # x
        for y in range(height):  # y
            for img in images:  # the pictures pixel
                pix = img.get_pixel(x, y)
                pixels.append(pix)  # add the pixel in pixels
            best = get_best_pixel(pixels)  # choose the best pixel of all
            pixel = result.get_pixel(x, y)
            pixel.red = best.red
            pixel.green = best.green
            pixel.blue = best.blue
            pixels = []  # clear up pixels
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #18
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    for i in range(width):
        for j in range(height):
            pixels = []
            for k in range(len(images)):
                pixels += [images[k].get_pixel(i, j)]
            best_pixel = get_best_pixel(pixels)
            img = pixels.index(best_pixel)
            result.get_pixel(i, j).red = images[img].get_pixel(i, j).red
            result.get_pixel(i, j).green = images[img].get_pixel(i, j).green
            result.get_pixel(i, j).blue = images[img].get_pixel(i, j).blue
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #19
0
def copy_image(image):  # Creates a copy of the image
    copy = SimpleImage.blank(image.width, image.height)
    for pixel in copy:
        x = pixel.x
        y = pixel.y
        copy.set_pixel(x, y, image.get_pixel(x, y))
    return copy
コード例 #20
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)       # create a blank canvas

    # Write code to populate image and create the 'ghost' effect
    for x in range(result.width):
        for y in range(result.height):
            pixel_lst = []                          # create a blank list to store pixel
            for image in images:                    # for each every image in folder
                pixel = image.get_pixel(x, y)
                pixel_lst.append(pixel)
            best_pixel = get_best_pixel(pixel_lst)  # find the best pixel from all images

            # assign the best rgb to blank canvas
            result.get_pixel(x, y).red = best_pixel.red
            result.get_pixel(x, y).green = best_pixel.green
            result.get_pixel(x, y).blue = best_pixel.blue
    print("Displaying image!")
    result.show()
コード例 #21
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)

    ######## YOUR CODE STARTS HERE #########
    #  compare different photo at same position
    for x in range(width):
        for y in range(height):
            pixels = []  # a list for load the the pixel at same position
            for image in images:
                same_position_pixel = image.get_pixel(x, y)
                pixels.append(same_position_pixel)
            best_pixel = get_best_pixel(pixels)  # choose the best pixel
            result_pixel = result.get_pixel(
                x, y)  # fill the best pixel into the result
            result_pixel.red = best_pixel.red
            result_pixel.green = best_pixel.green
            result_pixel.blue = best_pixel.blue
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #22
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # get a pixel in the result image
    for x in range(result.width):
        for y in range(result.height):
            pixels = []
            result_pixel = result.get_pixel(x, y)
            # create pixels in every image for the specific pixel
            for image in images:
                pixels.append(image.get_pixel(x, y))
            # create colors of the result pixel
            result_pixel.red = get_best_pixel(pixels).red
            result_pixel.green = get_best_pixel(pixels).green
            result_pixel.blue = get_best_pixel(pixels).blue
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #23
0
ファイル: blur.py プロジェクト: iamjerrywu/StanCode-SC101
def blur(img):
    """
    :param img:
    :return: new(blurred) img
    """
    #create new image
    new_img = SimpleImage.blank(img.width, img.height)

    sum_red, sum_green, sum_blue, count = 0, 0, 0, 0
    # frame = 3 x 3, [[x_left_bnd, x_right_bnd], [y_low_bnd, y_up_bnd]]
    frame = [[-1, 2], [-1, 2]]

    for x in range(img.width):
        for y in range(img.height):
            for frame_x in range(frame[0][0], frame[0][1]):
                for frame_y in range(frame[1][0], frame[1][1]):
                    if (img.width > x + frame_x >= 0) & (img.height >
                                                         y + frame_y >= 0):
                        # get old img pixel
                        img_pixel = img.get_pixel(x + frame_x, y + frame_y)
                        # calculate RGB sum
                        sum_red += img_pixel.red
                        sum_green += img_pixel.green
                        sum_blue += img_pixel.blue
                        count += 1
            # average RGB and assign to new image
            new_img.set_rgb(x, y, sum_red // count, sum_green // count,
                            sum_blue // count)

            # reset RGB sum and count
            sum_red, sum_green, sum_blue, count = 0, 0, 0, 0
    return new_img
コード例 #24
0
def reflect(filename):
    """
    This function will create a reflected image in a vertical way
    -----------------------------------------------------------
    :param filename: str, the name of the file
    :return: SimpleImage, the reflected image
    """
    img = SimpleImage(filename)
    new_img = SimpleImage.blank(img.width, img.height * 2)
    for x in range(img.width):
        for y in range(img.height):
            img_pixel = img.get_pixel(x, y)
            # The colored pixel of the original image
            new_img_pixel1 = new_img.get_pixel(x, y)
            # The blanked pixel of the new image counted from the top
            new_img_pixel2 = new_img.get_pixel(x, new_img.height - 1 - y)
            # The blanked pixel of the new image counted from the bottom
            new_img_pixel1.red = img_pixel.red
            new_img_pixel1.green = img_pixel.green
            new_img_pixel1.blue = img_pixel.blue

            new_img_pixel2.red = img_pixel.red
            new_img_pixel2.green = img_pixel.green
            new_img_pixel2.blue = img_pixel.blue
    return new_img
コード例 #25
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    pixel_lst = []
    for j in range(width):
        for k in range(height):
            # This double for loop is here to address every pixel of the process picture
            for i in range(len(images)):
                # This for loop is here to address the number of pictures the user feeds in
                pixel_lst.append(images[i].get_pixel(j, k))
                # This line takes the same pixel from each feeded-in picture and puts it into the list
                best_pixel = get_best_pixel(pixel_lst)
                result_pixel = result.get_pixel(j, k)
                result_pixel.red = best_pixel.red
                result_pixel.green = best_pixel.green
                result_pixel.blue = best_pixel.blue
            pixel_lst = []
            # This line resets the list to continue for next pixel
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #26
0
ファイル: stanCodoshop.py プロジェクト: sickle25/sc-projects
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########

    for x in range(result.width):
        for y in range(result.height):
            result_pixel = result.get_pixel(x, y)

            # Take out the pixels of the same position (x,y) in different pictures to list.
            pixels = []
            for z in range(len(images)):
                pic = images[z]
                pic_pixel = pic.get_pixel(x, y)
                pixels.append(pic_pixel)

            best_pixel = get_best_pixel(pixels)

            result_pixel.red = best_pixel.red
            result_pixel.green = best_pixel.green
            result_pixel.blue = best_pixel.blue

    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #27
0
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width
    height = images[0].height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    # green_pixel = SimpleImage.blank(20, 20, 'green').get_pixel(0, 0)
    # red_pixel = SimpleImage.blank(20, 20, 'red').get_pixel(0, 0)
    # blue_pixel = SimpleImage.blank(20, 20, 'blue').get_pixel(0, 0)
    # best1 = get_best_pixel([green_pixel, blue_pixel, blue_pixel])
    # print(best1.red, best1.green, best1.blue)
    for x in range(width):
        for y in range(height):
            pixel_lst = []
            for i in range(len(images)):
                image = images[i]
                pixel = image.get_pixel(x, y)
                pixel_lst.append(pixel)
            best_pixel = get_best_pixel(pixel_lst)
            result_pixel = result.get_pixel(x, y)
            result_pixel.red = best_pixel.red
            result_pixel.green = best_pixel.green
            result_pixel.blue = best_pixel.blue
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #28
0
ファイル: mirror_lake.py プロジェクト: Rita-Ning/sc-turing
def reflect(filename):
    """
    :param filename: str, the file path of the original image.
    :return: mirror lake picture.
    """
    img = SimpleImage(filename)
    blank_img = SimpleImage.blank(img.width, img.height * 2)

    for y in range(img.height):
        for x in range(img.width):
            # data
            img_pixel = img.get_pixel(x, y)

            # Empty pixel1
            new_pixel1 = blank_img.get_pixel(x, y)
            # create pixel 2
            new_pixel2 = blank_img.get_pixel(x, blank_img.height - 1 - y)

            new_pixel1.red = img_pixel.red
            new_pixel1.green = img_pixel.green
            new_pixel1.blue = img_pixel.blue

            new_pixel2.red = img_pixel.red
            new_pixel2.green = img_pixel.green
            new_pixel2.blue = img_pixel.blue

    return blank_img
コード例 #29
0
ファイル: stanCodoshop.py プロジェクト: fredi5566/SC101
def solve(images):
    """
    Given a list of image objects, compute and display a Ghost solution image
    based on these images. There will be at least 3 images and they will all
    be the same size.

    Input:
        images (List[SimpleImage]): list of images to be processed
    """
    width = images[0].width                              # hoover's width
    height = images[0].height                            # hoover's height
    result = SimpleImage.blank(width, height)
    ######## YOUR CODE STARTS HERE #########
    # Write code to populate image and create the 'ghost' effect
    print(len(images))
    for x in range(width):
        for y in range(height):
            new_pixel = result.get_pixel(x, y)
            pixel_list = []
            for z in range(len(images)):                 # Element of list of images is image, but not pixel!!
                img = images[z]
                pixel_list.append(img.get_pixel(x,y))    # Make a list for using method- get_best_pixel and get_average
            best_pixel = get_best_pixel(pixel_list)
            new_pixel.red = best_pixel.red
            new_pixel.green = best_pixel.green
            new_pixel.blue = best_pixel.blue
    ######## YOUR CODE ENDS HERE ###########
    print("Displaying image!")
    result.show()
コード例 #30
0
def shrink(figure_img):
    # shrink the photo with green background
    small_img = SimpleImage.blank(figure_img.width // 2,
                                  figure_img.height // 2)
    if figure_img.width % 2 == 1 or figure_img.height % 2 == 1:
        for x in range(0, figure_img.width - 1, 2):
            for y in range(0, figure_img.height - 1, 2):
                pixel_1 = figure_img.get_pixel(x, y)
                pixel_2 = figure_img.get_pixel(x + 1, y)
                pixel_3 = figure_img.get_pixel(x, y + 1)
                pixel_4 = figure_img.get_pixel(x + 1, y + 1)
                small_pixel = small_img.get_pixel(x // 2, y // 2)
                small_pixel.red = (pixel_1.red + pixel_2.red + pixel_3.red +
                                   pixel_4.red) // 4
                small_pixel.green = (pixel_1.green + pixel_2.green +
                                     pixel_3.green + pixel_4.green) // 4
                small_pixel.blue = (pixel_1.blue + pixel_2.blue +
                                    pixel_3.blue + pixel_4.blue) // 4
    else:
        for x in range(0, figure_img.width, 2):
            for y in range(0, figure_img.height, 2):
                pixel_1 = figure_img.get_pixel(x, y)
                pixel_2 = figure_img.get_pixel(x + 1, y)
                pixel_3 = figure_img.get_pixel(x, y + 1)
                pixel_4 = figure_img.get_pixel(x + 1, y + 1)
                small_pixel = small_img.get_pixel(x // 2, y // 2)
                small_pixel.red = (pixel_1.red + pixel_2.red + pixel_3.red +
                                   pixel_4.red) // 4
                small_pixel.green = (pixel_1.green + pixel_2.green +
                                     pixel_3.green + pixel_4.green) // 4
                small_pixel.blue = (pixel_1.blue + pixel_2.blue +
                                    pixel_3.blue + pixel_4.blue) // 4
    return small_img