def sepiaFilter(image): width = image.width() height = image.height() # Create a new image that is the same size as the original. newImage = GraphicsImage(width, height) for row in range(height) : for col in range(width) : # Get the color of the pixel in the original image. red = image.getRed(row, col) green = image.getGreen(row, col) blue = image.getBlue(row, col) # Filter the pixel. newRed = int((0.393*red) + (0.796*green) + (0.198*blue)) if newRed > 255: newRed = 255 newGreen = int((0.349*red) + (0.686*green) + (0.168*blue)) if newGreen > 255: newGreen = 255 newBlue = int((0.272*red) + (0.534*green) + (0.131*blue)) if newBlue > 255: newBlue = 255 # Set the pixel in the new image to the new color. newImage.setPixel(row, col, newRed, newGreen, newBlue) return newImage
def adjustBrightness(image, amount): width = image.width() height = image.height() # Create a new image that is the same size as the original. newImage = GraphicsImage(width, height) for row in range(height): for col in range(width): # Get the color of the pixel in the original image. red = image.getRed(row, col) green = image.getGreen(row, col) blue = image.getBlue(row, col) # Adjust the brightness and cap the colors. newRed = red + red * amount if newRed > 255: newRed = 255 elif newRed < 0: newRed = 0 newGreen = green + green * amount if newGreen > 255: newGreen = 255 elif newGreen < 0: newGreen = 0 newBlue = blue + blue * amount if newBlue > 255: newBlue = 255 elif newBlue < 0: newBlue = 0 # Set the pixel in the new image to the new color. newImage.setPixel(row, col, newRed, newGreen, newBlue) return newImage
def edgeDetection(image): """ creates a new image that highlights drastics changes in intensity from one pixel to the next @params image-image to have the edges shown @returns new image highlighting the edges """ width = image.width() height = image.height() # Create a new image that is the same size as the original. newImage = GraphicsImage(width, height) for row in range(1, height - 1) : for col in range(1, width - 1) : reds = [image.getRed(row - 1, col - 1), image.getRed(row, col - 1), image.getRed(row + 1, col - 1), image.getRed(row-1, col), image.getRed(row+1, col), image.getRed(row - 1, col + 1), image.getRed(row, col + 1), image.getRed(row + 1, col + 1)] #list of green values around the pixel greens = [image.getRed(row - 1, col - 1), image.getGreen(row, col - 1), image.getGreen(row + 1, col - 1), image.getGreen(row-1, col), image.getGreen(row+1, col), image.getGreen(row - 1, col + 1), image.getGreen(row, col + 1), image.getGreen(row + 1, col + 1)] #list of blue values around the pixel blues = [image.getRed(row - 1, col - 1), image.getBlue(row, col - 1), image.getBlue(row + 1, col - 1), image.getBlue(row-1, col), image.getBlue(row+1, col), image.getBlue(row - 1, col + 1), image.getBlue(row, col + 1), image.getBlue(row + 1, col + 1)] aveIntensity = [] for x in range(8): aveIntensity.append((reds[x] + greens[x] + blues[x]) // 3) gX = -1 * (aveIntensity[0] + aveIntensity[2]) + (aveIntensity[5] + aveIntensity[7]) + (2 * aveIntensity[6]) - (2 * aveIntensity[1]) gY = -1 * (aveIntensity[0] + aveIntensity[5]) + (aveIntensity[2] + aveIntensity[7]) + (2 * aveIntensity[4]) - (2 * aveIntensity[3]) tG = int((gX**2 + gY**2)**.5) if tG >= 128: tG = 255 else: tG = 0 newImage.setPixel(row, col, tG, tG, tG) return newImage
def smooth(image): """ enhances grainy images by setting each pixel to the median value of all the pixels around it @param image - the image to be enhanced @return the new enhanced image """ width = image.width() height = image.height() # Create a new image that is the same size as the original. newImage = GraphicsImage(width, height) for row in range(1, height - 1) : for col in range(1, width - 1) : #list of red values around the pixel reds = [image.getRed(row, col -1), image.getRed(row, col+1), image.getRed(row+1, col), image.getRed(row-1, col), image.getRed(row+1, col +1), image.getRed(row+1, col-1), image.getRed(row-1, col-1), image.getRed(row-1, col+1)] #list of green values around the pixel greens = [image.getGreen(row, col -1), image.getGreen(row, col+1), image.getGreen(row+1, col), image.getGreen(row-1, col), image.getGreen(row+1, col +1), image.getGreen(row+1, col-1), image.getGreen(row-1, col-1), image.getGreen(row-1, col+1)] #list of blue values around the pixel blues = [image.getBlue(row, col -1), image.getBlue(row, col+1), image.getBlue(row+1, col), image.getBlue(row-1, col), image.getBlue(row+1, col +1), image.getBlue(row+1, col-1), image.getBlue(row-1, col-1), image.getBlue(row-1, col+1)] newRed = int(median(reds))#median value of the reds list newGreen = int(median(greens))#median value of the greens list newBlue = int(median(blues))#median value of the blues list # Set the pixel in the new image to the new color. newImage.setPixel(row, col, newRed, newGreen, newBlue) return newImage
def rotateLeft(image) : # Create a new image whose dimensions are the opposite of the original. width = image.width() height = image.height() newImage = GraphicsImage(height, width) # Rotate the image. for row in range(height) : newCol = row for col in range(width) : newRow = col pixel = image.getPixel(row, col) newImage.setPixel(newRow, newCol, pixel) return newImage
def flipVertically(image) : # Create a new image that is the same size as the original. width = image.width() height = image.height() newImage = GraphicsImage(width, height) # Flip the image vertically. newRow = height - 1 for row in range(height) : for col in range(width) : newCol = col pixel = image.getPixel(row, col) newImage.setPixel(newRow, newCol, pixel) newRow = newRow - 1 return newImage
def colorHistoEqual(image): width = image.width() height = image.height() newImage = GraphicsImage(width, height) redHisto = [] greenHisto = [] blueHisto = [] for i in range(256): redHisto.append(0) greenHisto.append(0) blueHisto.append(0) for row in range(height): for col in range(width): r, g, b = image.getPixel(row, col) redHisto[r] += 1 greenHisto[g] += 1 blueHisto[b] += 1 redCDF = [] greenCDF = [] blueCDF = [] redEQ = [] greenEQ = [] blueEQ = [] for j in range(256): if j > 0: redCDF.append(redCDF[j-1] + redHisto[j]) greenCDF.append(greenCDF[j-1] + greenHisto[j]) blueCDF.append(blueCDF[j-1] + blueHisto[j]) else: redCDF.append(redHisto[j]) greenCDF.append(greenHisto[j]) blueCDF.append(blueHisto[j]) redEQ.append(255*redCDF[j] // (width * height)) greenEQ.append(255*greenCDF[j] // (width * height)) blueEQ.append(255*blueCDF[j] // (width * height)) for row in range(height) : for col in range(width) : r, g, b = image.getPixel(row, col) newRed = redEQ[r] newGreen = greenEQ[g] newBlue = blueEQ[b] newImage.setPixel(row, col, newRed, newGreen, newBlue) return newImage
def greyScale(image): width = image.width() height = image.height() newImage = GraphicsImage(width, height) for row in range(height): for col in range(width): red = image.getRed(row, col) green = image.getGreen(row, col) blue = image.getBlue(row, col) # Filter the pixel. newRed = int(0.21 * red) newGreen = int(0.72 * green) newBlue = int(0.07 * blue) grey = newRed + newGreen + newBlue # Set the pixel in the new image to the new color. newImage.setPixel(row, col, grey, grey, grey) return newImage
def shrink(image): width = image.width() height = image.height() # Create a new image that is 1/4 the size as the original. newImage = GraphicsImage(width//2, height//2) newRow = 0 for row in range(0,height-1, 2) : newCol = 0 for col in range(0,width-1, 2) : # _ _ #|1|2| #|4|3| #-----This is the manner in which each pixel is evaluated each iteration, then increments by 2 in the col's and row's to evaluate the next grid square #list of red values in each grid reds = [image.getRed(row, col), image.getRed(row, col+1), image.getRed(row+1, col+1), image.getRed(row+1, col)] #list of green values in each grid greens = [image.getGreen(row, col), image.getGreen(row, col+1), image.getGreen(row+1, col+1), image.getGreen(row+1, col)] #list of blue values in each grid blues = [image.getBlue(row, col), image.getBlue(row, col+1), image.getBlue(row+1, col+1), image.getBlue(row+1, col)] newRed = int(median(reds))#median value of the reds in each grid newGreen = int(median(greens))#median value of the green in each grid newBlue = int(median(blues))#median value of the blue in each grid # Set the pixel in the new image to the new color. newImage.setPixel(newRow, newCol, newRed, newGreen, newBlue) newCol = newCol + 1 newRow = newRow + 1 return newImage
def createNegative(image) : width = image.width() height = image.height() # Create a new image that is the same size as the original. newImage = GraphicsImage(width, height) for row in range(height) : for col in range(width) : # Get the color of the pixel in the original image. red = image.getRed(row, col) green = image.getGreen(row, col) blue = image.getBlue(row, col) # Filter the pixel. newRed = 255 - red newGreen = 255 - green newBlue = 255 - blue # Set the pixel in the new image to the new color. newImage.setPixel(row, col, newRed, newGreen, newBlue) return newImage
def warpImage(image): '''This function in complete''' width = image.width() height = image.height() newImage = GraphicsImage(width, height) for row in range(height): for col in range(width//6): r,g,b = image.getPixel(row, col) newImage.setPixel(row, col, r, g, b) for row in range(height): offset = 0 for col in range(width//6, width//3): if col % 2 == 1: r,g,b = image.getPixel(row, col) newImage.setPixel(row, col - offset, r, g, b) offset+=1 for row in range(height): offset = 0 for col in range(width//3, width//2): if col % 2 == 1: r,g,b = image.getPixel(row, col) offset+=2 newImage.setPixel(row, col - offset, r, g, b) for row in range(height): offset = (2*width//3 - width//2)//2 for col in range(width//2, 2*width//3): if col % 2 == 1: r,g,b = image.getPixel(row, col) offset -= 2 newImage.setPixel(row, col + offset, r, g, b) for row in range(height): offset = (5*width//6 - 2*width//3)//2 for col in range(2*width//3, 5*width//6): if col % 2 == 1: r,g,b = image.getPixel(row, col) offset-=1 newImage.setPixel(row, col + offset, r, g, b) for row in range(height): for col in range(5*width//6, width): r,g,b = image.getPixel(row, col) newImage.setPixel(row, col, r, g, b) return newImage
# Load the original image. origImage = GraphicsImage(filename) # Create an empty image that will contain the new flipped image. width = origImage.width() height = origImage.height() newImage = GraphicsImage(width, height) # Iterate over the image and copy the pixels to the new image to # produce the flipped image (vertical flip to an upside down image) newRow = height - 1 for row in range(height): for col in range(width): newCol = col pixel = origImage.getPixel(row, col) newImage.setPixel(newRow, newCol, pixel) newRow = newRow - 1 # Save the new image with a new name. newImage.save("flipped-" + filename) # 5. Textbook R.29 # show only the green component of each pixel of the image to green # Draw the image win = GraphicsWindow() canvas = win.canvas() canvas.drawImage(newImage) win.wait()
# Load the image from the file. image = GraphicsImage(filename) # Process the image. width = image.width() height = image.height() for row in range(height): for col in range(width): # Get the current pixel color. red = image.getRed(row, col) green = image.getGreen(row, col) blue = image.getBlue(row, col) # Filter the pixel. newRed = 255 - red newGreen = 255 - green newBlue = 255 - blue # Set the pixel to the new color. image.setPixel(row, col, newRed, newGreen, newBlue) # Display the image on screen. win = GraphicsWindow() canvas = win.canvas() canvas.drawImage(image) win.wait() # Save the new image with a new name. image.save("negative-" + filename)
radius = width // 2 #set up the effect color to white(255,255,255) newRed = 255 newBlue = 255 newGreen = 255 # newRow is row number of new image newRow = 0 for row in range(height): #go through every row # outer loop is top-down for col in range(width): #go through column #inner loop is left-to-right newCol = col if sqrt((width / 2 - col)**2 + (height / 2 - row)**2) <= radius: green = int(0.7152 * origImage.getGreen(row, col)) red = int(0.2126 * origImage.getRed(row, col)) blue = int(0.0722 * origImage.getBlue(row, col)) gray = green + red + blue newImage.setPixel(newRow, newCol, gray, gray, gray) #paste to new image else: newImage.setPixel(newRow, newCol, newRed, newGreen, newBlue) # paste to new image newRow = newRow + 1 # Save the new image with "Black". newImage.save("black-" + filename) # Draw the telescoped image win = GraphicsWindow() canvas = win.canvas() canvas.drawImage(newImage) win.wait()
else: HEIGHT = picture.height() WIDTH = picture.width() #Find middle point HeightRow = HEIGHT // 2 WidthCol = WIDTH // 2 newimage = GraphicsImage(WIDTH, HEIGHT) #For future use #To do : make a square pic ''' Main function ''' print("Processing Images....Please Wait...") #Find pixel for row in range(HEIGHT): for col in range(WIDTH): if sqrt((row - HeightRow)**2 + (col - WidthCol)**2) <= HEIGHT // 2: red = round(picture.getRed(row, col) * 0.2126) green = round(picture.getGreen(row, col) * 0.07152) blue = round(picture.getGreen(row, col) * 0.0722) gray = red + green + blue newimage.setPixel(row, col, gray, gray, gray) else: newimage.setPixel(row, col, 255, 255, 255) newimage.save(os.path.join( __location__, 'queen-mary_lab3.gif')) #Change if you want another pic print("Done") print("Output image has been put into same folder name:'queen-mary_lab3.gif'")
canvas = win.canvas() imageWidth = origImage.width() imageHeight = origImage.height() # define new image dimensions newImage = GraphicsImage(imageWidth, imageHeight) # output the size of image print(imageWidth) print(imageHeight) # edit the image for row in range(imageHeight): for col in range(imageWidth): red = origImage.getRed(row, col) newRed = int(red * 1.3) # if the red exceed the RGB range, it will set to the maximum if newRed > 255: newRed = 255 green = origImage.getGreen(row, col) blue = origImage.getBlue(row, col) # set the colors of the image newImage.setPixel(row, col, newRed, green, blue) # display image canvas.drawImage(origImage) canvas.drawImage(newImage) win.wait() # output image file newImage.save("Golden Bridge Sunset.gif")