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 # 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') 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 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)
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() # 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):
# 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 # ------------------------------- # 2. Color cycle problem # ------------------------------- color_cycle = image.copy_image(img) for x in range(w): for y in range(h): r, g, b = img.get_rgb(x, y) color_cycle.set_rgb(x, y, b, r, g) # cycles the colors by replacing red with blue, green with red, and blue with green pigments for each pixel image.save_to_file(color_cycle, 'color cycle.png')
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)