def luminanceToGreyscale(lum_array): """Takes in an array of luminances and returns an image array""" for i in range(len(lum_array)): for j in range(len(lum_array[0])): lum_array[i][j] = GImage.createRGBPixel(lum_array[i][j], lum_array[i][j], lum_array[i][j]) return (lum_array)
def blur(image): """Implements a blur function that takes the average of the red, green, and blue of surrounding pixels—needs to be applied several times to notice the effect, usually.""" array = image.getPixelArray() for i in range(len(array)): for j in range(len(array[0])): if (i != 0) and (i != (len(array) - 1)) and (j != 0) and ( j != (len(array[0]) - 1)): # Pick out the RBG components of surrounding pixels i_red = bin(array[i][j])[10:18] i_green = bin(array[i][j])[18:26] i_blue = bin(array[i][j])[26:34] ip1_red = bin(array[i + 1][j])[10:18] ip1_green = bin(array[i + 1][j])[18:26] ip1_blue = bin(array[i + 1][j])[26:34] im1_red = bin(array[i - 1][j])[10:18] im1_green = bin(array[i - 1][j])[18:26] im1_blue = bin(array[i - 1][j])[26:34] jp1_red = bin(array[i][j + 1])[10:18] jp1_green = bin(array[i][j + 1])[18:26] jp1_blue = bin(array[i][j + 1])[26:34] jm1_red = bin(array[i][j - 1])[10:18] jm1_green = bin(array[i][j - 1])[18:26] jm1_blue = bin(array[i][j - 1])[26:34] # Average the RBG components new_red = round( mean([ int(i_red, 2), int(ip1_red, 2), int(im1_red, 2), int(jp1_red, 2), int(jm1_red, 2) ])) new_green = round( mean([ int(i_green, 2), int(ip1_green, 2), int(im1_green, 2), int(jp1_green, 2), int(jm1_green, 2) ])) new_blue = round( mean([ int(i_blue, 2), int(ip1_blue, 2), int(im1_blue, 2), int(jp1_blue, 2), int(jm1_blue, 2) ])) # Make the new pixel with the averages array[i][j] = GImage.createRGBPixel(new_red, new_green, new_blue) return GImage(array)
def equalizeArray(array): length = len(array) width = len(array[0]) cumulative = computeCumulativeHistogram(array) for col in range(length): for row in range(width): pixel = array[col][row] new_lum = (255 * cumulative[luminance(pixel)]) // (length * width) new_pixel = GImage.createRGBPixel(new_lum, new_lum, new_lum) array[col][row] = new_pixel return array
def createGrayscaleImage(image): """ Creates a grayscale image based on the luminance of each pixel """ array = image.getPixelArray() height = len(array) width = len(array[0]) for i in range(height): for j in range(width): gray = luminance(array[i][j]) array[i][j] = GImage.createRGBPixel(gray, gray, gray) return GImage(array)
def equalize(image): array = image.getPixelArray() height = len(array) width = len(array[0]) colors = [0] * 256 cumulative = [0] * 256 total = 0 for i in range(height): for j in range(width): x = luminance(array[i][j]) colors[x] += 1 for i in range(len(colors)): total += colors[i] cumulative[i] = total for i in range(height): for j in range(width): x = luminance(array[i][j]) newLum = (255 * cumulative[x]) // (height * width) array[i][j] = GImage.createRGBPixel(newLum, newLum, newLum) return GImage(array)
def posterize(image): array = image.getPixelArray() for col in range(len(array)): for row in range(len(array[0])): pixel = array[col][row] red = GImage.getRed(pixel) green = GImage.getGreen(pixel) blue = GImage.getBlue(pixel) d_min = 500**2 for i in range(len(PALETTE)): pre_red = PALETTE[i][0] pre_green = PALETTE[i][1] pre_blue = PALETTE[i][2] d = ((2 * (pre_red - red)**2) + (4 * (pre_green - green)**2) + (3 * (pre_blue - blue)**2) + ((((pre_red + red) / 2) * ((pre_red - red)**2) - ((pre_blue - blue)**2)) / 256)) if d < d_min: d_min = d new_pixel = GImage.createRGBPixel(pre_red, pre_green, pre_blue) array[col][row] = new_pixel return GImage(array)