def obamafy(img): '''(Image object) -> Image object Obamafys image by assigning a specific colour per grayscale value ''' img_obamified = image.copy_image(img) #loop over every (x,y) pair for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) gray_value = int(float(r+b+g)/3) #yellow if gray_value>182: img_obamified.set_rgb(x,y, 252, 227, 166) #light blue elif 121<gray_value<182: img_obamified.set_rgb(x,y, 112, 150, 158) #red elif 60<gray_value<121: img_obamified.set_rgb(x,y, 217, 26, 33) #dark blue else: img_obamified.set_rgb(x,y, 0, 51, 76) return img_obamified
def negative(img): '''(Image object) -> Image object Returns a copy of img where the the rgb values are the opposite of what they normally are, meaning they're negative ''' img_negative = image.copy_image(img) for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) #Makes pixels negative r_neg = (r-255) if r_neg<0: r_neg = r_neg*-1 g_neg = (g-255) if g_neg<0: g_neg = g_neg*-1 b_neg = (b-255) if b_neg<0: b_neg = b_neg*-1 #sets the new pixels img_negative.set_rgb(x,y,r_neg,g_neg,b_neg) return img_negative
def increase_contrast(img): '''(Image object) -> Image object Increases the contrast of img ''' img_contrast = image.copy_image(img) for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) #Sets the new rgb values new_r = 0 new_g = 0 new_b = 0 #Checks whether or not to increase contrast, and doesn't allow too big or small values if r == 128: new_r = r elif r>128: new_r = r+(2*(r-128)) if new_r>255: new_r = 255 else: new_r = r-(2*(128-r)) if new_r<0: new_r = 0 if g == 128: new_g = g elif g>128: new_g = g+(2*(g-128)) if new_g>255: new_g = 255 else: new_g = g-(2*(128-g)) if new_g<0: new_g = 0 if b == 128: new_b = b elif b>128: new_b = b+(2*(b-128)) if new_b>255: new_b = 255 else: new_b = b-(2*(128-b)) if new_b<0: new_b = 0 #Sets the real new rgb values img_contrast.set_rgb(x,y,new_r,new_g,new_b) return img_contrast
def detect(self, image): image = copy_image(image) bounding_boxes = detect(image, self.model, self.windows) heatmap = np.zeros_like(image[:, :, 0]).astype(np.float) heatmap = add_heat(heatmap, bounding_boxes) self.heat_history.append(heatmap) self.heat_history = self.heat_history[-3:] heat = np.vstack(self.heat_history) heat = apply_threshold(heat, threshold=1) labels = label(heat) bounding_boxes = labeled_bounding_boxes(image, labels) image = draw_bounding_boxes(image, bounding_boxes) return image
def grayscale(img): '''(Image object) -> Image object Returns a copy of img where the rgb values have been averaged and reassigned, making a happy grey image ''' img_grayer = image.copy_image(img) #loop over every (x,y) pair for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) avg_pix = int(float(r+b+g)/3) img_grayer.set_rgb(x,y,avg_pix,avg_pix,avg_pix) return img_grayer
def color_cycle(img): '''(Image object) -> Image object Returns a copy of img where the red value has been moved to the blue value, the green to the red and the blue to the green ''' img_cycled = image.copy_image(img) #loop over every(x,y) pair for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) #sets the new colors img_cycled.set_red(x,y,g) img_cycled.set_green(x,y,b) img_cycled.set_blue(x,y,r) return img_cycled
def key_out_red(img, img2): '''(Image object) + (Image object) -> Image object Checks to see what pixels in the img are red, and replaces them with pixels from another image ''' img_red = image.copy_image(img) #loop over every (x,y) pair for x in range(img.width()): for y in range(img.height()): #gets the rgbs for each picture at the x and y of the original r,g,b = img.get_rgb(x,y) r1,g1,b1 = img2.get_rgb(x,y) #I found through playing with an online tool that things that are red #generally have an r value which is greater than the other two combined #So this checks if it's true if r>b+g: img_red.set_rgb(x,y,r1,g1,b1) return img_red
def brightness(img): '''(Image object) -> Image object Returns a copy of img where the the brightness is increased or decreased based on the users command ''' br_value = int(raw_input("Please enter a number to adjust the brightness to: ")) img_brightness = image.copy_image(img) for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) #Generates new rgb values and doesn't let them exceed 255 or go below 0 new_r = r + br_value if new_r>255: new_r = 255 elif new_r<0: new_r = 0 else: new_r = new_r new_g = g + br_value if new_g>255: new_g = 255 elif new_g<0: new_g = 0 else: new_g = new_g new_b = b + br_value if new_b>255: new_b = 255 elif new_b<0: new_b = 0 else: new_b = new_b img_brightness.set_rgb(x,y,new_r,new_g,new_b) return img_brightness
def posterize(img): '''(Image object) -> Image object Posterizes the img by reducing the amount of colors ''' img_posterize = image.copy_image(img) for x in range(img.width()): for y in range(img.height()): #gets the rgb and sets it to r, g and b r,g,b = img.get_rgb(x,y) new_r = r new_g = g new_b = b #Sets the new variables if r%32 == 0: new_r = r elif new_r%32 != 0: new_r = r-(r%32) if g%32 == 0: new_g = g elif new_g%32 != 0: new_g = g-(g%32) if b%32 == 0: new_b = b elif new_b%32 != 0: new_b = b-(b%32) #Sets each pixel img_posterize.set_rgb(x,y,new_r,new_g,new_b) return img_posterize
import image img = image.load_from_file('obama.png') obamafy = image.copy_image(img) w = img.width() h = img.height() for x in range(w): # obamafies the image for y in range(h): r, g, b = img.get_rgb(x, y) if (r + g + b)/3 < 60: obamafy.set_rgb(x, y, 0, 51, 76) elif (r + g + b)/3 < 121: obamafy.set_rgb(x, y, 217,26,33) elif (r + g + b)/3 <= 182: obamafy.set_rgb(x, y, 112, 150, 158) else: obamafy.set_rgb(x, y, 252, 227, 166) image.save_to_file(obamafy, 'obamafy.png') image.display_images(img, obamafy)
import image img = image.load_from_file('crayons.png') negative = image.copy_image(img) w = img.width() h = img.height() for x in range(w): # converts image to negative for y in range(h): r, g, b = img.get_rgb(x, y) negative.set_rgb(x, y, abs(r - 255), abs(g - 255), abs(b - 255)) image.save_to_file(negative, 'negative.png') image.display_images(img, negative)
import image img = image.load_from_file('crayons.png') posterize = image.copy_image(img) w = img.width() h = img.height() for x in range(w): # posterizes the image for y in range(h): r, g, b = img.get_rgb(x, y) posterize.set_rgb(x, y, (r/32)*32, (g/32)*32, (b/32)*32) image.save_to_file(posterize, 'posterize.png') image.display_images(img, posterize)
import image # asks user for input brightness_change = int(raw_input("Enter how much you want to change the brightness: ")) img = image.load_from_file('crayons.png') brightness = image.copy_image(img) w = img.width() h = img.height() for x in range(w): # changes brightness by a factor determined by the user for y in range(h): r, g, b = img.get_rgb(x, y) brightness.set_rgb(x, y, max(min(r + brightness_change, 255), 0), max(min(g + brightness_change, 255), 0), max(min(b + brightness_change, 255), 0)) image.save_to_file(brightness, 'brightness.png') image.display_images(img, brightness)
import image img = image.load_from_file('crayons.png') color_cycle = image.copy_image(img) w = img.width() h = img.height() for x in range(w): # converts image through color cycling for y in range(h): r, g, b = img.get_rgb(x, y) color_cycle.set_rgb(x, y, b, r, g) image.save_to_file(color_cycle, 'color cycle.png') image.display_images(img, color_cycle)
import image img = image.load_from_file('crayons.png') grayscale = image.copy_image(img) w = img.width() h = img.height() for x in range(w): # converts image to grayscale for y in range(h): r, g, b = img.get_rgb(x, y) grayscale.set_rgb(x, y, (r + g + b)/3, (r + g + b)/3, (r + g + b)/3) image.save_to_file(grayscale, 'grayscale.png') image.display_images(img, grayscale)
import image # ------------------------------- # EXAMPLE PROGRAM #1: RED FILTER # ------------------------------- img = image.load_from_file('crayons.png') red_only_img = image.copy_image(img) w = img.width() h = img.height() # loop over every (x,y) pair for x in range(w): for y in range(h): # filter out green and blue red_only_img.set_green(x, y, 0) red_only_img.set_blue(x, y, 0) # save the new image to a file image.save_to_file(red_only_img, 'red_crayons.png') # ------------------------------- # EXAMPLE PROGRAM #2: COPY TOP # ------------------------------- img = image.load_from_file('crayons.png') img_copy = image.copy_image(img) w = img.width() h = img.height()
# Feedback: What was the hardest part of this assignment? # obamafy # Feedback: Any suggestions for improving the assignment? # great assignment # ---------------------------------------------------------- import image brightness_change = int(raw_input("Enter how much you want to change the brightness: ")) # establishes user input early on for a later image conversion # ------------------------------- # 1. Grayscale problem # ------------------------------- img = image.load_from_file('crayons.png') grayscale = image.copy_image(img) w = img.width() h = img.height() for x in range(w): for y in range(h): r, g, b = img.get_rgb(x, y) grayscale.set_rgb(x, y, (r + g + b)/3, (r + g + b)/3, (r + g + b)/3) # sets the grayscale value to the average of the red, green and blue values for each pixel image.save_to_file(grayscale, 'grayscale.png') print # -------------------------------