def main(): """ () -> NoneType Main Program that load image(s) from file(s) and performs transformations! """ #Loads the original image original_img = image.load_from_file('crayons.png') obamanation = image.load_from_file('obama.png') stopschild = image.load_from_file('stop_sign.png') blaetter = image.load_from_file('leaves.png') #Assign a bunch of variables and transforms more than Optimus Prime color_cycled = color_cycle(original_img) grayscaled = grayscale(original_img) negated = negative(original_img) brightered = brightness(original_img) contrasted = increase_contrast(original_img) posterized = posterize(original_img) obamafied = obamafy(obamanation) redded = key_out_red(stopschild, blaetter) #displays all images image.display_images(color_cycled, grayscaled, negated, brightered, contrasted, posterized) image.display_images(obamafied, redded)
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)
#Leo Ascenzi import image img = image.load_from_file('landmark_puzzle.png') h = img.height() w = img.width() for x in range(w): for y in range(h): r, g, b = img.get_rgb(x,y) img.set_rgb(x, y,0,g*25,0) image.display_images(img)
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)
# 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() # loop over every x value... for x in range(w): # ... but only loop over y values in top half for y in range(h/2): # get rgb from top half of original image r, g, b = img.get_rgb(x, y) # copy over rgb from top half to bottom half img_copy.set_rgb(x, y + h/2, r, g, b) # save the new image to a file image.save_to_file(img_copy, 'copy_top.png') # create window that displays both the original and modified images image.display_images(img, red_only_img, img_copy)
if (r + g + b)/3 < 60: # if the grayscale value is less than 60, make the pixel dark blue obamafy.set_rgb(x, y, 0, 51, 76) elif (r + g + b)/3 < 121: # if the grayscale is 60 <= value < 121, make the pixel red obamafy.set_rgb(x, y, 217,26,33) elif (r + g + b)/3 <= 182: # if the grayscale is 121 <= value < 182, make the pixel light blue obamafy.set_rgb(x, y, 112, 150, 158) else: # if the grayscale value is greater than or equal to 182, make the pixel yellow obamafy.set_rgb(x, y, 252, 227, 166) image.save_to_file(obamafy, 'obamafy.png') # ------------------------------- # 8. Challenge problem # ------------------------------- # ------------------------------- # Lastly, display everything. One call # to image.display_images will do it. Just # pass all your image objects as parameters # to that function. # ------------------------------- # image.display_images( ) image.display_images(img, grayscale, color_cycle, negative) # NOTE: the user must quit out of each set of images for the next set to be displayed image.display_images(brightness, contrast, posterize) image.display_images(imgtwo, obamafy)
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)