def convertBlackWhite(input_image): grayscale_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight()) for col in range(input_image.getWidth()): for row in range(input_image.getHeight()): p = input_image.getPixel(col, row) red = p.getRed() green = p.getGreen() blue = p.getBlue() avg = (red + green + blue) / 3.0 newpixel = image.Pixel(avg, avg, avg) grayscale_image.setPixel(col, row, newpixel) blackwhite_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight()) for col in range(input_image.getWidth()): for row in range(input_image.getHeight()): p = grayscale_image.getPixel(col, row) red = p.getRed() if red > 140: val = 255 else: val = 0 newpixel = image.Pixel(val, val, val) blackwhite_image.setPixel(col, row, newpixel) return blackwhite_image
def preto_branco_pixel(pixel): pixel_aux = cinzento_pixel(pixel) if pixel_aux.getRed() < 128 : novo_pixel = cImage.Pixel(0,0,0) else: novo_pixel = cImage.Pixel(255,255,255) return novo_pixel
def main(): answer = input( "Would you like to Red Channel, Grayscale, or Invert the images pixels? red/gray/invert: " ) for row in range(img.getHeight()): for col in range(img.getWidth()): p = img.getPixel(col, row) if answer.lower() == 'red': R = p.getRed() noG = (p.getRed() * 0 + p.getGreen() * 0 + p.getBlue() * 0) noB = (p.getRed() * 0 + p.getGreen() * 0 + p.getBlue() * 0) newpixel = image.Pixel(R, noG, noB) elif answer.lower() == 'gray': grayR = ((p.getRed() + p.getGreen() + p.getBlue()) / 3) grayG = ((p.getRed() + p.getGreen() + p.getBlue()) / 3) grayB = ((p.getRed() + p.getGreen() + p.getBlue()) / 3) newpixel = image.Pixel(grayR, grayG, grayB) else: invR = (255 - p.getRed()) invG = (255 - p.getGreen()) invB = (255 - p.getBlue()) newpixel = image.Pixel(invR, invG, invB) img.setPixel(col, row, newpixel)
def convolution(filename): img = image.Image(filename) win = image.ImageWin(img.getWidth(), img.getHeight()) for row in range(img.getHeight()): for col in range(img.getWidth()): p = img.getPixel(col, row) oldpixel = image.Pixel(p.getRed(), p.getGreen(), p.getBlue()) pixel_list = [] if ((col and row) > 0) and (col + 1 < img.getWidth()) and ( row + 1 < img.getHeight() ): #working with all but the edges for simplicity for nearcol in range(col, col + 2): for nearrow in range(row, row + 2): nearp = img.getPixel(nearcol, nearrow) greynearp = greyscale(nearp) greypvalue = greynearp.getRed() pixel_list = pixel_list + [greypvalue] print('start', pixel_list, 'end') nearmatrix = int(pixel_list[0]) + int(pixel_list[1]) * 0 - int( pixel_list[2]) print(nearmatrix) outputpixel = image.Pixel(255 - nearmatrix, 255 - nearmatrix, 255 - nearmatrix) #print(outputpixel) img.setPixel(col, row, outputpixel) #print(pixel_list) else: img.setPixel(col, row, oldpixel) img.draw(win) win.exitonclick()
def change_pixels(): img = image.Image("Husky.gif") img2 = image.EmptyImage(img.getWidth(), img.getHeight()) win = image.ImageWin() win2 = image.ImageWin() for y in range(img.getHeight()): for x in range(img.getWidth()): pix = img.getPixel(x, y) red = pix.getRed() green = pix.getGreen() blue = pix.getBlue() brightness = int(red + green + blue) if brightness >= 470: new_pix = image.Pixel( 254, 228, 169) #converts pixels to new color scheme elif brightness >= 370: # depending on brightness. new_pix = image.Pixel(112, 152, 162) elif brightness >= 270: new_pix = image.Pixel(215, 26, 33) else: new_pix = image.Pixel(0, 50, 77) img2.setPixel(x, y, new_pix) #puts appropriate color into pixel img.draw(win) img2.draw(win2) win.exitonclick() win2.exitonclick()
def pixelMapper(image, pMapping): img = cImage.Image(image) newimg = cImage.EmptyImage(img.getWidth(), img.getHeight()) win = cImage.ImageWin() for col in range(img.getWidth()): for row in range(img.getHeight()): p = img.getPixel(col, row) red = p.getRed() green = p.getGreen() blue = p.getBlue() new_value = (red + green + blue) / 3 white = cImage.Pixel(255, 255, 255) black = cImage.Pixel(0, 0, 0) pMapping = cImage.Pixel(128, 128, 128) if new_value < (255 / 3): newimg.setPixel(col, row, black) elif new_value > (255 / 3) and new_value < (255 * 2) / 3: newimg.setPixel(col, row, pMapping) else: newimg.setPixel(col, row, white) newimg.draw(win)
def obamafy(img): ''' ( ) -> NoneType Description here. ''' obamafy_image = img.copy() w = img.getWidth() h = img.getHeight() for x in range(w): for y in range(h): pixel = img.getPixel(x, y) red = pixel.getRed() green = pixel.getGreen() blue = pixel.getBlue() total_color = int((red + green + blue) / 3) yellow = image.Pixel(252, 227, 166) darkblue = image.Pixel(0, 51, 76) lightblue = image.Pixel(112, 150, 158) obamared = image.Pixel(217, 26, 33) if total_color <= 121 and total_color >= 61: obamafy_image.setPixel(x, y, obamared) if total_color >= 182: obamafy_image.setPixel(x, y, yellow) if total_color <= 60: obamafy_image.setPixel(x, y, darkblue) if total_color <= 182 and total_color >= 122: obamafy_image.setPixel(x, y, lightblue) return obamafy_image
def prepara_pixel(immagine): new_pixel = image.Pixel(255, 255, 255) riga = immagine.getWidth() colonna = immagine.getHeight() while riga % 4 != 0: riga -= 1 while colonna % 4 != 0: colonna -= 1 riga_def = (riga // 4) - 1 colonna_def = (colonna // 4) - 1 win = image.ImageWin(height=colonna - 1, width=riga - 1) immagine.draw(win) lista_pos = [] lista_punti = [] lista_verifica = [] for col in range(colonna): for row in range(riga): flag = 0 if row == 0 or row == riga_def or row == (riga_def * 2) or row == ( riga_def * 3) or row == (riga_def * 4): immagine.setPixel(row, col, new_pixel) flag += 1 if col == 0 or col == colonna_def or col == ( colonna_def * 2) or col == (colonna_def * 3) or col == (colonna_def * 4): immagine.setPixel(row, col, new_pixel) flag += 1 if flag == 2: lista_pos.append((row, col)) for i in range(len(lista_pos) - 6): if not ((lista_pos[i][0] == 0 and lista_pos[i][1] == colonna_def * 4) or lista_pos[i][0] == riga_def * 4): lista_punti.append([(lista_pos[i][0] + 1, lista_pos[i][1] + 1), (lista_pos[i + 6][0] - 1, lista_pos[i + 6][1] - 1), False]) lista_verifica.append([(lista_pos[i][0] + 1, lista_pos[i][1] + 1), (lista_pos[i + 6][0] - 1, lista_pos[i + 6][1] - 1), False]) print(lista_punti) immagine.draw(win) i = random.randrange(0, 16, 1) new_pixel = image.Pixel(255, 255, 255) for col in range(colonna): for row in range(riga): if (lista_punti[i][0][0] < row < lista_punti[i][1][0] and lista_punti[i][0][1] < col < lista_punti[i][1][1]): immagine.setPixel(row, col, new_pixel) lista_punti[i][2] = True lista_verifica[i][2] = True immagine.draw(win) return [ immagine, lista_punti, colonna_def, riga_def, win, lista_verifica, riga, colonna ]
def preto_branco_pixel(pixel, limiar): #pixel_aux = pixel_cinzento(pixel) preto = cImage.Pixel(0, 0, 0) branco = cImage.Pixel(255, 255, 255) if pixel.getRed() < limiar: novo_pixel = branco else: novo_pixel = preto return novo_pixel
def preto_branco_pixel(pixel, limiar): """Admite pixel na escala de cinzentos.""" preto = cImage.Pixel(0, 0, 0) branco = cImage.Pixel(255, 255, 255) if pixel.getRed() < limiar: novo_pixel = preto else: novo_pixel = branco return novo_pixel
def cria_forma_1(largura,altura, r,g,b): imagem = cImage.EmptyImage(largura,altura) pixel = cImage.Pixel(r,g,b) for linha in range(altura): pixel_preto=cImage.Pixel(0,0,0) imagem.setPixel(0,linha,pixel_preto) for coluna in range(1,largura): for linha in range(altura): imagem.setPixel(coluna,linha,pixel) return imagem
def distancia_palete(cor, palete): """Qual a cor mais próxima.""" distancia_min = distancia_cores(cImage.Pixel(0,0,0), cImage.Pixel(255,255,255)) cor_min = cor for pal_cor in palete: cor_pixel = cImage.Pixel(pal_cor[0],pal_cor[1], pal_cor[2]) distancia = distancia_cores(cor,cor_pixel) if distancia < distancia_min: distancia_min = distancia cor_min = cor_pixel return cor_min
def DrawLine(img): #win = cImage.ImageWin('test',300,300) im = cImage.EmptyImage(300,300) p = cImage.Pixel(255,0,0) for i in range(300): im.setPixel(50,i,p) im.draw(img) p2 = cImage.Pixel(234,154,125) for l in range(250): im.setPixel(150,l,p2) im.draw(img)
def preto_branco(pixel): """Converte para preto e branco. Limiar fixo.""" r = pixel.getRed() g = pixel.getGreen() b = pixel.getBlue() cor_media = (r + g + b) / 3 if cor_media < 128: novo_pixel = cImage.Pixel(0, 0, 0) else: novo_pixel = cImage.Pixel(255, 255, 255) return novo_pixel
def convolucao_pb(x, y, imagem, filtro, limiar): """Imagem em escala de cinzentos.""" index = len(filtro) // 2 r = 0 for i in range(-index, index + 1): for j in range(-index, index + 1): pixel = imagem.getPixel(x + j, y + i) r += pixel.getRed() * filtro[j + index][i + index] r = restringe(int(r), 0, 255) if r < limiar: novo_pixel = cImage.Pixel(255, 255, 255) else: novo_pixel = cImage.Pixel(0, 0, 0) return novo_pixel
def main(): img = cImage.Image("summer.jpg") newimg = cImage.EmptyImage(img.getWidth(), img.getHeight()) win = cImage.ImageWin() for col in range(int((img.getWidth() / 2))): for row in range(int((img.getHeight() / 2))): p = img.getPixel(col, row) green = 255 - p.getGreen() blue = 255 - p.getBlue() red = 255 - p.getRed() newpixel = cImage.Pixel(red, green, blue) newimg.setPixel(col, row, newpixel) for col in range(int((img.getWidth() / 2)), img.getWidth()): for row in range(int((img.getHeight() / 2))): p = img.getPixel(col, row) green = p.getGreen() blue = p.getBlue() red = p.getRed() newcolor = int((red + green + blue) / 3) newpixel = cImage.Pixel(newcolor, newcolor, newcolor) newimg.setPixel(col, row, newpixel) for col in range(int((img.getWidth() / 2)), img.getWidth()): for row in range(int((img.getHeight() / 2)), img.getHeight()): p = img.getPixel(col, row) green = p.getGreen() blue = p.getBlue() red = p.getRed() newcolor = int((red + green + blue) / 3) newpixel = cImage.Pixel(newcolor, newcolor, newcolor) newimg.setPixel(col, row, newpixel) for col in range(int((img.getWidth() / 2)), img.getWidth()): for row in range(int((img.getHeight() / 2)), img.getHeight()): p = img.getPixel(col, row) red = p.getRed() if red > 140: val = 255 else: val = 0 newpixel = cImage.Pixel(val, val, val) newimg.setPixel(col, row, newpixel) for col in range(int((img.getWidth() / 2))): for row in range(int((img.getHeight() / 2)), img.getHeight()): p = img.getPixel(col, row) green = 0 blue = p.getBlue() red = p.getRed() newpixel = cImage.Pixel(red, green, blue) newimg.setPixel(col, row, newpixel) newimg.draw(win) win.exitonclick()
def pixel_negativo(pixel): """Passa ao negativo.""" r = 255 - pixel.getRed() g = 255 - pixel.getGreen() b = 255 - pixel.getBlue() pixel = cImage.Pixel(r, g, b) return pixel
def cria_arco(raio, amplitude, pos): """ Desenha (um arco de circunfer�ncia) com amplitude e raio. """ largura = raio + 1 altura = raio + 1 imagem = cImage.EmptyImage(largura, altura) # define a cor foregroung como branco pixel_branco = cImage.Pixel(255, 255, 255) for coluna in range(largura): for linha in range(altura): imagem.setPixel(coluna, linha, pixel_branco) # define a cor do pixel pixel = cria_random_pixel() # posiciona imagem.setPosition(pos[0], pos[1]) # cria for angulo in range(amplitude): cordx = int(raio * math.cos(math.radians(angulo))) cordy = int(raio * math.sin(math.radians(angulo))) imagem.setPixel(cordx, cordy, pixel) return imagem
def __init__(self, problem, imageFilename): '''Takes a SlidingPuzzle and the filename for a GIF image and initializes the display.''' self.__problem = problem self.__totalScore = 0 numRows, numCols = problem.getDim() image = cImage.FileImage(imageFilename) self.__tileHeight = image.getHeight() // numRows self.__tileWidth = image.getWidth() // numCols self.__tileList = [] for i in range(numRows * numCols): self.__tileList.append( cImage.EmptyImage(self.__tileWidth, self.__tileHeight)) t = 0 for r in range(numRows): for c in range(numCols): for x in range(self.__tileWidth): for y in range(self.__tileHeight): self.__tileList[t].setPixel( x, y, image.getPixel(c * self.__tileWidth + x, r * self.__tileHeight + y)) t = t + 1 for x in range(self.__tileWidth): for y in range(self.__tileHeight): self.__tileList[self.__problem.getBlank()].setPixel( x, y, cImage.Pixel(255, 255, 255)) self.__win = cImage.ImageWin("Sliding Puzzle!", numCols * self.__tileWidth, numRows * self.__tileHeight) self.update()
def Pixelize(astring, r, g, b): f = str(ord(astring)) if (len(f) < 3): f = "0" + f r = str(r) if (len(r) < 3): r = "0" + r g = str(g) if (len(g) < 3): g = "0" + g b = str(b) if (len(b) < 3): b = "0" + b r = r[:2] + f[0] g = g[:2] + f[1] b = b[:2] + f[2] r = int(r) if (r > 255): r = r - 10 g = int(g) if (g > 255): g = g - 10 b = int(b) if (b > 255): b = b - 10 p = cImage.Pixel(r, g, b) return p
def load(img): img = image.Image(img) iDim = img.getWidth(), img.getHeight() newimg = image.EmptyImage(iDim[0], iDim[1]) win = image.ImageWin(iDim[0], iDim[1]) # newimg.setDelay(0) #no animation # newimg.setDelay(1,100) #animated print "Loading..." for y in range(iDim[1]): for x in range(iDim[0]): #pix = img.getPixel(x, y) # try calling a function here to change the rgb values # maybe call function from image.Pixel newb = Filter.invert(img, x, y) newpixel = image.Pixel(newb[0], newb[1], newb[2]) newimg.setPixel(x, y, newpixel) newimg.draw( win ) #remove this for no animation, chrome sees it as frozen (is not) sys('cls') newimg.draw(win) win.exitonclick()
def sepia(): img = cImage.Image("elliot.gif") for row in range(img.getHeight()): for col in range(img.getWidth()): p = img.getPixel(col, row) newRed = int((.393 * p.getRed()) + (.769 * p.getGreen()) + (.189 * p.getBlue())) newGreen = int((.349 * p.getRed()) + (.686 * p.getGreen()) + (.168 * p.getBlue())) newBlue = int((.272 * p.getRed()) + (.534 * p.getGreen()) + (.131 * p.getBlue())) if newRed > 255: newRed = 255 elif newRed < 0: newRed = 0 if newGreen > 255: newGreen = 255 elif newGreen < 0: newGreen = 0 if newBlue > 255: newBlue = 255 elif newBlue < 0: newBlue = 0 newPixel = cImage.Pixel(newRed, newGreen, newBlue) img.setPixel(col, row, newPixel) img.draw(win)
def cria_forma(largura, altura, r, g, b): imagem = cImage.EmptyImage(largura, altura) pixel = cImage.Pixel(r, g, b) for coluna in range(largura): for linha in range(altura): imagem.setPixel(coluna, linha, pixel) return imagem
def taglio(img, win): new_pixel = image.Pixel(255, 255, 255) click_1 = win.getMouse() click_2 = win.getMouse() click_1_x = click_1[0] click_1_y = click_1[1] click_2_x = click_2[0] click_2_y = click_2[1] print(click_1) print(click_2) for col in range(img.getHeight()): for row in range(img.getWidth()): if click_1_x <= click_2_x and click_1_y <= click_2_y: if not (click_1_x < row < click_2_x and click_1_y < col < click_2_y): img.setPixel(row, col, new_pixel) if click_1_x >= click_2_x and click_1_y <= click_2_y: if not (click_2_x < row < click_1_x and click_1_y < col < click_2_y): img.setPixel(row, col, new_pixel) if click_1_x >= click_2_x and click_1_y >= click_2_y: if not (click_2_x < row < click_1_x and click_2_y < col < click_1_y): img.setPixel(row, col, new_pixel) if click_1_x <= click_2_x and click_1_y >= click_2_y: if not (click_1_x < row < click_2_x and click_2_y < col < click_1_y): img.setPixel(row, col, new_pixel) img.draw(win)
def sepiaPixel(oldPixel): """ Convert a given pixel to its sepia equivalent value Arguments: oldPixel: one pixel of a graphics image Returns: The sepia tone version of the given pixel """ r = oldPixel.getRed() g = oldPixel.getGreen() b = oldPixel.getBlue() newRed = 0.393 * r + 0.769 * g + 0.189 * b newGreen = 0.349 * r + 0.686 * g + 0.168 * b newBlue = 0.272 * r + 0.534 * g + 0.131 * b if newRed > 255: newRed = 255 if newGreen > 255: newGreen = 255 if newBlue > 255: newBlue = 255 newPixel = cImage.Pixel(int(newRed), int(newGreen), int(newBlue)) return newPixel
def manipula_cor(imagem_fich, val_r, val_g, val_b): """ Manipula cores. Altera de modo independente os três canais de cor.""" imagem = cImage.FileImage(imagem_fich) largura = imagem.getWidth() altura = imagem.getHeight() janela = cImage.ImageWin('Manipula cores', 2 * largura, altura) imagem.draw(janela) nova_imagem = cImage.EmptyImage(largura, altura) for coluna in range(largura): for linha in range(altura): pixel = imagem.getPixel(coluna, linha) r = pixel.getRed() g = pixel.getGreen() b = pixel.getBlue() novo_r = max(r, r + ((val_r * r) % 255)) novo_g = max(g, g + ((val_g * g) % 255)) novo_b = max(r, r + ((val_b * b) % 255)) novo_pixel = cImage.Pixel(novo_r, novo_g, novo_b) nova_imagem.setPixel(coluna, linha, novo_pixel) nova_imagem.setPosition(largura + 1, 0) nova_imagem.draw(janela) janela.exitOnClick()
def noise(im, width, height, param={}): import cImage, random newImage = cImage.EmptyImage(width, height) mag = param.get('Mag', 0.05) magType = param.get('Type', "multiplicative") for row in range(height): for col in range(width): old_pixel = im.getPixel(col, row) if magType == "additive": red = old_pixel.getRed() + (random.random() - 0.5) * mag * 255 green = old_pixel.getGreen() + (random.random() - 0.5) * mag * 255 blue = old_pixel.getBlue() + (random.random() - 0.5) * mag * 255 else: red = old_pixel.getRed() * (1 + (random.random() - 0.5) * mag) green = old_pixel.getGreen() * (1 + (random.random() - 0.5) * mag) blue = old_pixel.getBlue() * (1 + (random.random() - 0.5) * mag) rgbList = [int(red), int(green), int(blue)] for i in range(len(rgbList)): rgb = rgbList[i] if rgb < 0: rgb = 0 elif rgb > 255: rgb = 255 rgbList[i] = rgb new_pixel = cImage.Pixel(rgbList[0], rgbList[1], rgbList[2]) newImage.setPixel(col, row, new_pixel) return newImage
def blur(input_image): new_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight()) #loop over all pixels in input_image for col in range(1, input_image.getWidth() - 1): for row in range(1, input_image.getHeight() - 1): #get this pixel and its neighbors pixel = input_image.getPixel(col, row) left = input_image.getPixel(col - 1, row) right = input_image.getPixel(col + 1, row) up = input_image.getPixel(col, row - 1) down = input_image.getPixel(col, row + 1) #compute the new color components new_r = int((pixel.getRed() + left.getRed() + right.getRed() + up.getRed() + down.getRed()) / 5) new_g = int( (pixel.getGreen() + left.getGreen() + right.getGreen() + up.getGreen() + down.getGreen()) / 5) new_b = int((pixel.getBlue() + left.getBlue() + right.getBlue() + up.getBlue() + down.getBlue()) / 5) #set the pixel in the new image new_pixel = image.Pixel(new_r, new_g, new_b) new_image.setPixel(col, row, new_pixel) return new_image
def muda_pixel(pixel): """ Muda pixel de modo aleatório.""" r = (pixel.getRed() + random.randint(0, 25)) % 255 g = (pixel.getGreen() + random.randint(0, 25)) % 255 b = (pixel.getBlue() + random.randint(0, 25)) % 255 novo_pixel = cImage.Pixel(r, g, b) return novo_pixel
def pixel_cinzento(pixel): """ Converte um pixel para escala de cinzentos tendo em atenção a diferença dos canais.""" vermelho = pixel.getRed() verde = pixel.getGreen() azul = pixel.getBlue() int_media = (vermelho + verde + azul) // 3 novo_pixel = cImage.Pixel(int_media, int_media, int_media) return novo_pixel