def escala_cinzentos(imagem_fich): """ Transforma para escala de cinzentos a imagem.""" imagem = cImage.FileImage(imagem_fich) largura = imagem.getWidth() altura = imagem.getHeight() janela = cImage.ImageWin('Escala de cinzentos', 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) novo_pixel = pixel_cinzento(pixel) nova_imagem.setPixel(coluna, linha, novo_pixel) nova_imagem.setPosition(largura + 1, 0) nova_imagem.draw(janela) janela.exitOnClick()
def negativo_imagem(imagem_ficheiro): imagem_velha = cImage.FileImage(imagem_ficheiro) largura = imagem_velha.getWidth() altura = imagem_velha.getHeight() janela = cImage.ImageWin('Negativos',2*largura,altura) imagem_velha.draw(janela) imagem_nova = cImage.EmptyImage(largura,altura) for coluna in range(largura): for linha in range(altura): pixel_original = imagem_velha.getPixel(coluna,linha) novo_pixel = negativo_pixel(pixel_original) imagem_nova.setPixel(coluna,linha,novo_pixel) imagem_nova.setPosition(largura+1,0) imagem_nova.draw(janela) janela.exitOnClick()
def negative(): img = image.Image(input("Enter image name, including file extension: ")) newimg = image.EmptyImage(img.getWidth(), img.getHeight()) for x in range(img.getWidth()): for y in range(img.getHeight()): p = img.getPixel(x, y) r = 255 - p[ 0] # using index[0] to access red intensity, instead of newred = 255 - p.getRed() g = 255 - p[1] b = 255 - p[2] newPixel = image.Pixel(r, g, b) newimg.setPixel(x, y, newPixel) win = image.ImageWin() newimg.draw(win) win.exitonclick()
def arco(x, y, raio, amplitude): """ Desenha (um arco de circunferência) com centro em (x,y) e raio. """ janela = cImage.ImageWin('Janela', 600, 400) imagem = cImage.EmptyImage(600, 400) pixel_branco = cImage.Pixel(255, 255, 255) for coluna in range(600): for linha in range(400): imagem.setPixel(coluna, linha, pixel_branco) #imagem.setPosition(x,y) pixel = cria_random_pixel() for angulo in range(amplitude): cordx = (raio * math.cos((math.pi / float(180)) * angulo)) + x cordy = (raio * math.sin((math.pi / float(180)) * angulo)) + y imagem.setPixel(cordx, cordy, pixel) imagem.draw(janela) janela.exitOnClick()
def test_filter(image_file, func_name): ''' This function will display an original image next to its modified image. Parameters: image_file (string) - the name of a file containing an image. func_name (function) - the name of a function in this module that takes in an Image Object and returns a modified image object. Returns: None ''' orig_image = cImage.FileImage(image_file) new_image = func_name(orig_image) win = cImage.ImageWin("Image Processing", orig_image.getWidth() * 2, orig_image.getHeight()) orig_image.draw(win) new_image.setPosition(orig_image.getWidth()+ 1,0) new_image.draw(win) win.exitOnClick()
def noRed(img): """Remove all the red values from an image""" img = image.Image(img) newimg = image.EmptyImage(img.getWidth(), img.getHeight()) win = image.ImageWin() for col in range(img.getWidth()): for row in range(img.getHeight()): p = img.getPixel(col, row) noR = 0 g = p.getGreen() b = p.getBlue() newp = image.Pixel(noR, g, b) newimg.setPixel(col, row, newp) newimg.draw(win) win.exitonclick()
def distorcer(imagem, factor_x, factor_y): """ Distorce uma imagem de acordo com os factores indicados. Cada pixel vai darorigem a um rectângulo de dimensões factor_x X factor_y. """ # Cria imagens img = cImage.FileImage(imagem) nova_img = altera(img, factor_x,factor_y) # Cria janela largura = img.getWidth() altura = img.getHeight() janela = cImage.ImageWin('Distorce', factor_x * (largura+1) , factor_y * (altura+1)) # Coloca imagens img.setPosition(0,0) nova_img.setPosition(largura + 1,0) img.draw(janela) nova_img.draw(janela) # Termina janela.exitOnClick()
def displayImage(image): """ Display an image in an appropriately sized window Arguments: image: a graphics image Returns: None """ # Create a window of the correct dimensions width = image.getWidth() height = image.getHeight() imageWindow = cImage.ImageWin("Image Viewer", width, height) # Display the image in this window image.draw(imageWindow) # Wait for a mouse click to close the window imageWindow.exitOnClick()
def smooth(): img = image.Image(input("Enter image name, including file extension: ")) newimg = image.EmptyImage(img.getWidth(), img.getHeight()) for x in range(img.getWidth() - 1): for y in range(img.getHeight() - 1): p0 = img.getPixel(x, y) p1 = img.getPixel(x + 1, y) newRed = int(((p0.getRed()) + (p1.getRed())) / 2) newGreen = int(((p0.getGreen()) + (p1.getGreen())) / 2) newBlue = int(((p0.getBlue()) + (p1.getBlue())) / 2) newPixel = image.Pixel(newRed, newGreen, newBlue) newimg.setPixel(x, y, newPixel) win = image.ImageWin() newimg.draw(win) win.exitonclick()
def desenha_linha(p_1,p_2): """Desenha uma linha entre dois pontos.""" x_1 = p_1[0] y_1 = p_1[1] x_2 = p_2[0] y_2 = p_2[1] janela = cImage.ImageWin('Linha', 3* abs(x_2 - x_1), 3*abs(y_2 - y_1)) janela.setBackground((0,255,0)) imagem = cImage.EmptyImage(abs(x_2 - x_1),abs(y_2 - y_1)) print(imagem.getWidth(), imagem.getHeight()) pix = cImage.Pixel(255,0,0) for x in range(0, imagem.getWidth()): for k in range(0, imagem.getHeight()): imagem.setPixel(x,k,cImage.Pixel(0,255,0)) y = int(x * ((y_2 - y_1)/(x_2 - x_1)) + y_1 - x_1 * ((y_2 - y_1)/ (x_2 - x_1))) imagem.setPixel(x,y,pix) imagem.setPosition(janela.getWidth()//2 - imagem.getWidth()//2,janela.getHeight()//2 - imagem.getHeight()//2) imagem.draw(janela) janela.exitOnClick()
def black_and_white(): img = image.Image(input("Enter image name, including file extension: ")) newimg = image.EmptyImage(img.getWidth(), img.getHeight()) for x in range(img.getWidth()): for y in range(img.getHeight()): p = img.getPixel(x, y) avg_pixel = int((p[0] + p[1] + p[2]) / 3) if avg_pixel > 255 / 2: newPixel = image.Pixel(255, 255, 255) else: newPixel = image.Pixel(0, 0, 0) newimg.setPixel(x, y, newPixel) win = image.ImageWin() newimg.draw(win) win.exitonclick()
def main(): """ () -> NoneType Main Program that load image(s) from file(s) and performs transformations to those images as required for HW 03. The images are then displayed. """ original_img = image.Image('pres_casey.gif') red_image = red_filter(original_img) win = image.ImageWin(original_img.getWidth(), original_img.getHeight()) red_image.draw(win) grayscale_img = grayscale(original_img) grayscale_img.draw(win) cycle_colors_img = cycle_colors(original_img) cycle_colors_img.draw(win) negative_img = negative(original_img) negative_img.draw(win) brightness_img = brightness(original_img, 90) brightness_img.draw(win) increase_contrast_img = increase_contrast(original_img) increase_contrast_img.draw(win) vertical_flip_image = vertical_flip(original_img) vertical_flip_image.draw(win) posterize_image = posterize(original_img) posterize_image.draw(win) scroll_image = scroll(original_img, 10) scroll_image.draw(win) horizontal_mirror_image = horizontal_mirror(original_img) horizontal_mirror_image.draw(win) obamafy_image = obamafy(original_img) obamafy_image.draw(win)
def neg(file_name): """Displays the negative version of the chosen picture. The parameter is the file name.""" import cImage img = cImage.Image(file_name) width = img.getWidth() height = img.getHeight() win = cImage.ImageWin(width, height) img.draw(win) for row in range(height): for col in range(width): p = img.getPixel(col, row) neg_red = 255 - p.getRed() neg_green = 255 - p.getGreen() neg_blue = 255 - p.getBlue() newpixel = cImage.Pixel(neg_red, neg_green, neg_blue) img.setPixel(col, row, newpixel) return img.draw(win)
def green_line_along_column(column): img = image.Image(input("Enter image name, including file extension: ")) newimg = image.EmptyImage(img.getWidth(), img.getHeight()) for x in range(img.getWidth()): for y in range(img.getHeight()): p = img.getPixel(x, y) r = p[0] if x == column: g = 255 else: g = p[1] b = p[2] newPixel = image.Pixel(r, g, b) newimg.setPixel(x, y, newPixel) win = image.ImageWin() newimg.draw(win) win.exitonclick()
def intensifica_vermelho(imagem_fich, valor): """ Intensifica vermelho. Valor = número entre 0 e 100.""" imagem = cImage.FileImage(imagem_fich) largura = imagem.getWidth() altura = imagem.getHeight() janela = cImage.ImageWin('Sem Vermelho', 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() novo_r = max(r, r + ((valor * r) % 255)) novo_pixel = cImage.Pixel(novo_r, pixel.getGreen(), pixel.getBlue()) nova_imagem.setPixel(coluna, linha, novo_pixel) nova_imagem.setPosition(largura + 1, 0) nova_imagem.draw(janela) janela.exitOnClick()
def makeGrey(imageFile): oldImage = cImage.FileImage(imageFile) width = oldImage.getWidth() hight = oldImage.getHeight() myimagewindow = cImage.ImageWin("Image Process", width, hight) oldImage.draw(myimagewindow) newImage = cImage.EmptyImage(width, hight) out = [] for row in range(hight): for col in range(width): originalPixel = oldImage.getPixel(col, row) for x in originalPixel: out.append(x) aveRGB = originalPixel.getRed() + originalPixel.getGreen( ) + originalPixel.getBlue() // 3 newImage.setPixel(col, row, cImage.Pixel(aveRGB, aveRGB, aveRGB)) newImage.setPosition(width + 1, 0) newImage.draw(myimagewindow) myimagewindow.exitOnClick() return out, width, hight
def enlarge2(): file = input("Enter the image name, including extension: ") img = image.Image(file) imgw = img.getWidth() imgh = img.getHeight() newimg = image.EmptyImage(imgw * 2, imgh * 2) for x in range(imgw): for y in range(imgh): pixel = img.getPixel(x, y) newimg.setPixel(2 * x, 2 * y, pixel) newimg.setPixel(2 * x + 1, 2 * y, pixel) newimg.setPixel(2 * x, 2 * y + 1, pixel) newimg.setPixel(2 * x + 1, 2 * y + 1, pixel) win = image.ImageWin(title=("{0}_enlarge2".format(file)), width=(imgw * 2), height=(imgh * 2)) newimg.draw(win) win.exitonclick()
def preto_branco(imagem_fich, limiar): """ Transforma para imagem a preto e branco.""" imagem = cImage.FileImage(imagem_fich) largura = imagem.getWidth() altura = imagem.getHeight() janela = cImage.ImageWin('Preto e Branco', 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) pixel_aux = pixel_cinzento(pixel) if pixel_aux.getRed() < limiar: novo_pixel = cImage.Pixel(0, 0, 0) else: novo_pixel = cImage.Pixel(255, 255, 255) nova_imagem.setPixel(coluna, linha, novo_pixel) nova_imagem.setPosition(largura + 1, 0) nova_imagem.draw(janela) janela.exitOnClick()
def espelho_4_e_s(imagem_fich): """Faz o espelho em quatro direc��es. Usa a parte inferior esquerda.""" imagem = cImage.FileImage(imagem_fich) largura = imagem.getWidth() altura = imagem.getHeight() janela = cImage.ImageWin('Espelho 4', 2*largura,altura) imagem.draw(janela) nova_imagem = cImage.EmptyImage(largura,altura) for coluna in range(largura/2): for linha in range(altura/2): pixel = imagem.getPixel(coluna, linha) nova_imagem.setPixel(coluna,linha,pixel) # inferior esquerda nova_imagem.setPixel(coluna,altura - linha - 1,pixel) # superior direita nova_imagem.setPixel(largura - coluna -1,linha,pixel) # inferior direita nova_imagem.setPixel(largura - coluna -1,altura - linha - 1,pixel) nova_imagem.setPosition(largura + 1, 0) nova_imagem.draw(janela) janela.exitOnClick()
def graysc(file_name): """Displays the gray scale version of the chosen picture. The parameter is the file name.""" import cImage from myfunctions import median img = cImage.Image(file_name) width = img.getWidth() height = img.getHeight() win = cImage.ImageWin(width, height) img.draw(win) for row in range(height): for col in range(width): p = img.getPixel(col, row) new_red = median(p.getRed(), p.getGreen(), p.getBlue()) new_green = median(p.getRed(), p.getGreen(), p.getBlue()) new_blue = median(p.getRed(), p.getGreen(), p.getBlue()) newpixel = cImage.Pixel(new_red, new_green, new_blue) img.setPixel(col, row, newpixel) return img.draw(win)
def hidden_message(file_name): """Displays the negative version of the chosen picture. The parameter is the file name.""" import cImage img = cImage.Image(file_name) width = img.getWidth() height = img.getHeight() win = cImage.ImageWin(width, height) img.draw(win) for row in range(height): for col in range(width): p = img.getPixel(col, row) red_value = p.getRed() if red_value % 2 == 0: newpixel = cImage.Pixel(0, 0, 0) img.setPixel(col, row, newpixel) else: newpixel = cImage.Pixel(255, 255, 255) img.setPixel(col, row, newpixel) return img.draw(win)
def sepiaTone(img): """Convert image to sepia tone""" img = image.Image(img) newimg = image.EmptyImage(img.getWidth(), img.getHeight()) win = image.ImageWin() for col in range(img.getWidth()): for row in range(img.getHeight()): p = img.getPixel(col, row) r = p.getRed() g = p.getGreen() b = p.getBlue() newR = int(r * 0.393 + g * 0.769 + b * 0.189) newG = int(r * 0.349 + g * 0.686 + b * 0.168) newB = int(r * 0.272 + g * 0.534 + b * 0.131) newp = image.Pixel(newR, newG, newB) newimg.setPixel(col, row, newp) newimg.draw(win) win.exitonclick()
def julia(c, res=600, ofile=''): import cImage, math, colorsys myimagewindow = cImage.ImageWin("julia", res, res) numOfColors = 50 scale = 1.0 / (res / 3) center = (1.5, 1.5) image = cImage.EmptyImage(res, res) palette = [0] * numOfColors for i in range(numOfColors): f = 1 - abs((float(i) / numOfColors - 1)**15) r, g, b = colorsys.hsv_to_rgb(.66 + f / 3, 1 - f / 2, f) palette[i] = (int(b * 255), int(g * 255), int(r * 255)) c = complex(c[0], c[1]) for x in range(res): for y in range(res): re = (x * scale - center[0]) im = (y * scale - center[1]) z = re + im * 1j v = 1 for i in range(128): if abs(z) > 2.0: v = i / 129 break z = z * z + c rgb = palette[int(v * (numOfColors - 1))] pixel = cImage.Pixel(rgb[0], rgb[1], rgb[2]) image.setPixel(x, y, pixel) image.setPosition(0, 0) image.draw(myimagewindow) if ofile != '': image.save(ofile) myimagewindow.exitOnClick()
def setbit(oldbyte, newbit): print newbit if newbit: return oldbyte | newbit else: return oldbyte & 0b11111110 for row in range(fg.getHeight()): for col in range(fg.getWidth()): fgpix = fg.getPixel(col, row) fgr = fgpix.getRed() fgg = fgpix.getGreen() fgb = fgpix.getBlue() redbit = bitstream.next() fgr = setbit(fgr, redbit) greenbit = bitstream.next() fgg = setbit(fgg, greenbit) bluebit = bitstream.next() fgb = setbit(fgb, bluebit) newPix = myimage.Pixel(fgr, fgg, fgb) newIm.setPixel(col, row, newPix) win = myimage.ImageWin("test", 500, 400) newIm.draw(win) newIm.save('ascii_secret.png') win.exitOnClick()
def main1(nx, ny, lado, cores): janela = cImage.ImageWin('Ladrilhos', nx * lado, ny * lado) cozinha_poli(nx, ny, lado, cores, janela) janela.exitOnClick()
#Return the width of the image in pixels w = img.getWidth() #Return the height of the image in pixels h = img.getHeight() col = 0 row = 0 for col in range(w): for row in range(h): pixel = img.getPixel(col, row) #RGB values of the pixel red = pixel.getRed() green = pixel.getGreen() blue = pixel.getBlue() # Add Green at multiples of 25 into the pixel newPixel = image.Pixel(0, green * 25, 0) redCopy.setPixel(col, row, newPixel) row = row + 1 col = col + 1 row = 0 win = image.ImageWin(redCopy.getWidth(), redCopy.getHeight()) redCopy.draw(win) win.exitonclick() redCopy.save('scramble.gif')
def imageWindowFromFile(fileName): '''Given the name of an image file, returns an image window for it. You should have only one of these file-based image windows active in your program at any given time.''' image = cImage.FileImage(fileName) window = cImage.ImageWin(fileName, image.getWidth() + 16, image.getHeight()) return [image, window]
import cImage as image img = image.Image("testimage.gif") newimg = image.EmptyImage(img.getWidth(), img.getHeight()) win = image.ImageWin() for col in range(img.getWidth()): for row in range(img.getHeight()): p = img.getPixel(col, row) red = int(p.getRed() / 3) green = int(p.getGreen() / 3) blue = int(p.getBlue() / 3) newpixel = image.Pixel(red, green, blue) newimg.setPixel(col, row, newpixel) newimg.draw(win) win.exitonclick()
width = oldimage.getWidth() height = oldimage.getHeight() newim = cImage.EmptyImage(width, height) for row in range(height): for col in range(width): originalpixel = oldimage.getPixel(col, row) newpixel = rgbFunction(originalpixel) newim.setPixel(col, row, newpixel) # Why does return work when I copy code, but not when I write it myself? return newim def graypixel(oldpixel): intensitysum = oldpixel.getRed() + oldpixel.getGreen() + oldpixel.getBlue() aveRGB = intensitysum // 3 newPixel = cImage.Pixel(aveRGB, aveRGB, aveRGB) # Same question return newPixel win = cImage.ImageWin() img = cImage.Image("luther.jpg") newim = pixelMapper(img, graypixel) newim.draw(win) win.exitonclick()
def cria_imagem_preto(): janela = cImage.ImageWin('teste', 600, 400) imagem = cImage.EmptyImage(600, 400) imagem.draw(janela) janela.exitOnClick()