def combine_cage(figure_img): """ Using the coordinates to build the cage. :param figure_img: SimpleImage, the image of me. :return: SimpleImage, the image that I'm locked in the stock market. """ cage_row_img = SimpleImage("image_contest/cage_row.jpg") cage_column_img = SimpleImage("image_contest/cage_column.jpg") # 5 columns for x in range(figure_img.width): for y in range(figure_img.height): for i in range(1, 6, 1): if x == figure_img.width // 2 // 3 * i: for cage_x in range(cage_column_img.width): figure_pixel = figure_img.get_pixel(x + cage_x, y) cage_column_pixel = cage_column_img.get_pixel( cage_x, y) reassign_rgb_from_pixel(figure_pixel, cage_column_pixel) # 4 rows for x in range(figure_img.width): for y in range(figure_img.height): if y == 0 or y == figure_img.height // 2 // 2 or y == figure_img.height // 2 // 2 * 3 or y == figure_img.height - cage_row_img.height - 1: for cage_y in range(cage_row_img.height): figure_pixel = figure_img.get_pixel(x, y + cage_y) cage_row_pixel = cage_row_img.get_pixel(x, cage_y) reassign_rgb_from_pixel(figure_pixel, cage_row_pixel) return figure_img
def make_triptych(filename): image = SimpleImage(filename) width = image.width height = image.height #Creates new image to contain triptych triptych = SimpleImage.blank(width + 20, height) #Creates panel one of triptych image = SimpleImage(USER_DIRECTORY + IMAGE_NAME + '_1.JPG') width1 = image.width height1 = image.height for y in range(height1): for x in range(width1): pixel = image.get_pixel(x,y) triptych.set_pixel(x,y,pixel) #Creates panel two of triptych image = SimpleImage(USER_DIRECTORY + IMAGE_NAME + '_2.JPG') width2 = image.width height2 = image.height for y in range(height2): for x in range(width2): pixel = image.get_pixel(x, y) triptych.set_pixel(x + ((width/4) + 10), y, pixel) #Creates panel three of triptych image = SimpleImage(USER_DIRECTORY + IMAGE_NAME + '_3.JPG') width3 = image.width height3 = image.height for y in range(height3): for x in range(width3): pixel = image.get_pixel(x, y) triptych.set_pixel(x + (((width/4)*3) + 20), y, pixel) return triptych
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()
def shrink(filename): """ :param filename: str, the file path of the original image. :return img: SimpleImage, a quarter size og the original image. """ original = SimpleImage("images/poppy.png") # Make a quarter size blank image of the original image. shrink_img = original.blank(original.width // 2, original.height // 2) for y in range(original.height): for x in range(original.width): # Get 4 pixels each times in original image. if x * 2 < original.width and y * 2 < original.height: # Get pixels in shrunk image. shrink_img_pixel = shrink_img.get_pixel(x, y) # Put average RGB values of 4 pixels each times into one pixel of shrunk image. quarter_red = ( original.get_pixel(x * 2, y * 2).red + original.get_pixel(x * 2 + 1, y * 2).red + original.get_pixel(x * 2, y * 2 + 1).red + original.get_pixel(x * 2 + 1, y * 2 + 1).red) / 4 quarter_green = ( original.get_pixel(x * 2, y * 2).green + original.get_pixel(x * 2 + 1, y * 2).green + original.get_pixel(x * 2, y * 2 + 1).green + original.get_pixel(x * 2 + 1, y * 2 + 1).green) / 4 quarter_blue = ( original.get_pixel(x * 2, y * 2).blue + original.get_pixel(x * 2 + 1, y * 2).blue + original.get_pixel(x * 2, y * 2 + 1).blue + original.get_pixel(x * 2 + 1, y * 2 + 1).blue) / 4 shrink_img_pixel.red = quarter_red shrink_img_pixel.green = quarter_green shrink_img_pixel.blue = quarter_blue return shrink_img
def reflect(filename): """ Vertically "reflects" an image over the bottom horizontal axis and returns an image of twice the original height. Input: filename (string): name of image file to be read in Returns: reflected (SimpleImage): reflected image, twice original height """ image = SimpleImage(filename) reflected = SimpleImage.blank(width=image.width, height=2 * image.height) for y in range(image.height): for x in range(image.width): old_pixel = image.get_pixel(x, y) # top image top_image = reflected.get_pixel(x, y) top_image.red = old_pixel.red top_image.green = old_pixel.green top_image.blue = old_pixel.blue # bottom image bottom_image = reflected.get_pixel(x, 2 * image.height - 1 - y) bottom_image.red = old_pixel.red bottom_image.green = old_pixel.green bottom_image.blue = old_pixel.blue return reflected
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
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
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
def main(): """ This function a mirrored image of poppy.png by inserting old pixels into the empty one """ original_mt = SimpleImage('images/mt-rainier.jpg') original_mt.show() blank_img = SimpleImage.blank(original_mt.width, original_mt.height * 2) for x in range(original_mt.width): for y in range(original_mt.height): # colored pixel old_pixel = original_mt.get_pixel(x, y) # empty pixel 1 new_pixel1 = blank_img.get_pixel(x, y) # empty pixel 2 new_pixel2 = blank_img.get_pixel(x, blank_img.height - y - 1) # insert pixel 1 new_pixel1.red = old_pixel.red new_pixel1.green = old_pixel.green new_pixel1.blue = old_pixel.blue # insert pixel 2 new_pixel2.red = old_pixel.red new_pixel2.green = old_pixel.green new_pixel2.blue = old_pixel.blue blank_img.show()
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
def shrink(filename): """ :param filename: str, :return img: SimpleImage using the concept of the checker board. select pixels we need and reorganize into a new image """ img = SimpleImage(filename) after_shrink = SimpleImage.blank(img.width // 2, img.height // 2) for y in range(img.height): for x in range(img.width): p_org = img.get_pixel(x, y) if (x + y) % 2 == 0: if x % 2 == 0: p_shrink = after_shrink.get_pixel(x // 2, y // 2) p_shrink.red = p_org.red p_shrink.green = p_org.green p_shrink.blue = p_org.blue else: p_shrink = after_shrink.get_pixel((x - 1) // 2, (y - 1) // 2) p_shrink.red = p_org.red p_shrink.green = p_org.green p_shrink.blue = p_org.blue return after_shrink
def blurred(filename): old_img = SimpleImage(filename) new_img = SimpleImage.blank(old_img.width, old_img.height) for x in range(old_img.width): for y in range(old_img.height): r_sum = 0 g_sum = 0 b_sum = 0 count = 0 # find 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: old_img_p = old_img.get_pixel(pixel_x, pixel_y) r_sum += old_img_p.red g_sum += old_img_p.green b_sum += old_img_p.blue count += 1 new_img_p = new_img.get_pixel(x, y) new_img_p.red = r_sum / count new_img_p.blue = b_sum / count new_img_p.green = g_sum / count return new_img
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
def main(): # Load original image img = SimpleImage(FILE_DIR) width = img.width height = img.height # Image center xc = width // 2 yc = height // 2 # Generate distorted image distorted = SimpleImage.blank(width, height) for pixel in distorted: x = pixel.x y = pixel.y xu, yu = get_coordinates(x, y, xc, yc, K1, width, height) if 0 <= xu < width and 0 <= yu < height: distorted.set_pixel(x, y, img.get_pixel(xu, yu)) else: # Set to white color pixel.red = RGB_MAX pixel.green = RGB_MAX pixel.blue = RGB_MAX # Display images img.show() distorted.show()
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
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
def main(): foreground = SimpleImage('images/tiefighter.jpg') background = SimpleImage('images/quad.jpg') for px in foreground: if blue_enough(px): foreground.set_pixel(px.x, px.y, background.get_pixel(px.x, px.y)) background.show() foreground.show()
def main(): image_name = input('enter an image name: ') image = SimpleImage('images/' + image_name) image.show() for y in range(image.height): for x in range(image.width): pixel = image.get_pixel(x, y) sepia_pixel(pixel) image.show()
def redscreen(filename1, filename2): Image = SimpleImage(filename1) back = SimpleImage(filename2) for pixel in Image: average = (pixel.red + pixel.green + pixel.blue) // 3 if pixel.red >= average * INTENSITY_THRESHOLD: x = pixel.x y = pixel.y Image.set_pixel(x, y, back.get_pixel(x, y)) return Image
def greenscreen(main_file, back_file): image = SimpleImage(main_file) back = SimpleImage(back_file) for px in image: average = (px.red + px.green + px.blue) // 3 if px.green >= average * INTENSITY_TH: x = px.x y = px.y image.set_pixel(x, y, back.get_pixel(x, y)) return image
def squeeze_width(filename, n): image = SimpleImage(filename) width = image.width height = image.height image_squeeze = SimpleImage.blank(width // SQUEEZE_FACTOR, height) for y in range(height): for x in range(width // n): pixel = image.get_pixel(x * n, y) image_squeeze.set_pixel(x, y, pixel) image_squeeze.show()
def main(): pic1 = input("Enter image: ") pic = SimpleImage('images/' + pic1) pic.show() for y in range(pic.height): for x in range(pic.width): pixel = pic.get_pixel(x, y) sepia_pixel(pixel) pic.show()
def make_reflected(filename): image = SimpleImage(filename) new_blank = SimpleImage.blank(image.width, image.height * 2) for y in range(image.height): for x in range(image.width): px = image.get_pixel(x, y) new_blank.set_pixel(x, y, px) y_coordinate_mirror = (2 * (image.height - 1)) - y new_blank.set_pixel(x, y_coordinate_mirror, px) return new_blank
def make_reflected(filename): image = SimpleImage(filename) original_width = image.width original_height = image.height reflection = SimpleImage.blank(original_width, original_height * 2) for x in range(original_width): for y in range(original_height): pixel = image.get_pixel(x, y) reflection.set_pixel(x, y, pixel) reflection.set_pixel(x, (original_height * 2) - (y + 1), pixel) return reflection
def make_reflected(filename): image = SimpleImage(filename) width = image.width height = image.height mirror = SimpleImage.blank(width, image.height * 2) for y in range(height): for x in range(width): pixel = image.get_pixel(x, y) mirror.set_pixel(x, y, pixel) mirror.set_pixel(x, (height * 2) - (y + 1), pixel) return mirror
def main(): image = SimpleImage(PATCH_NAME) final_image = SimpleImage.blank(WIDTH, HEIGHT) for y in range(PATCH_SIZE): for x in range(PATCH_SIZE): pix = image.get_pixel(x, y) do_magic(pix, x, y, final_image) # This is an example which should generate a pinkish patch patch = make_recolored_patch(1.5, 0, 1.5) final_image.show()
def filter_course(): image = SimpleImage(course) width = image.width height = image.height mirror = SimpleImage.blank(width, height) for y in range(height): for x in range(width): pixel = image.get_pixel(x, y) mirror.set_pixel(x, y, pixel) mirror.set_pixel(x, height - (y + 1), pixel) mirror.show()
def make_reflected(filename): image = SimpleImage(filename) width = image.width height = image.height reflected = SimpleImage.blank(width, height * 2) for y in range(height): for x in range(width): pixel = image.get_pixel(x, y) reflected.set_pixel(x, y, pixel) reflected.set_pixel(x, height * 2 - (y + 1), pixel) return reflected
def double_left(filename): """ Takes the left half of image, and copies it on top of the right half. """ image = SimpleImage(filename) width = image.width height = image.height for y in range(height): for x in range(width // 2): pixel = image.get_pixel(x, y) image.set_pixel((x + (width // 2)), y, pixel) image.show()
def main(): """ TODO:ps own figure """ img = SimpleImage('image_contest/P_20201127_235437_001.jpg') bg = SimpleImage('image_contest/moon.jpg') print(img.height, img.width) print(bg.height, bg.width) bg.make_as_big_as(img) for y in range(img.height): for x in range(img.width): pixel = img.get_pixel(x, y) bg_pixel = bg.get_pixel(x, y) avg = (pixel.red + pixel.green + pixel.blue) / 3 if 500 > x or x > 642: if 400 > y or y > 500: if avg > 142: pixel.red = bg_pixel.red pixel.blue = bg_pixel.blue pixel.green = bg_pixel.green img.show()