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 = int(red + red * amount) if newRed > 255 : newRed = 255 elif newRed < 0 : newRed = 0 newGreen = int(green + green * amount) if newGreen > 255 : newGreen = 255 elif newGreen < 0 : newGreen = 0 newBlue = int(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 blackWhite(image): """ Creates and returns a new image that is the original image, but in black and white @param image - the image to be turned black and white @return the new image in black and white """ 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) if red + blue + green >= 382:#if the intensity is >= half full intensity, the new pixel is set to full intensity(white) newRed = 255 newGreen = 255 newBlue = 255 else: #otherwise it is set to 0 intensity(black) newRed = 0 newGreen = 0 newBlue = 0 # Set the pixel in the new image to the new color. newImage.setPixel(row, col, newRed, newGreen, newBlue) return newImage
def histoEqual(image) : image = greyScale(image) width = image.width() height = image.height() newImage = GraphicsImage(width, height) histogram = [] for i in range(256): histogram.append(0) for row in range(height) : for col in range(width) : r, g, b = image.getPixel(row, col) avgIntensity = (r + g + b)//3 histogram[avgIntensity] += 1 cdf = [] #cumulative distribution frequency eqHist = [] for j in range(256): if j > 0: cdf.append(cdf[j-1] + histogram[j]) else: cdf.append(histogram[j]) eqHist.append(255 * cdf[j] // (width*height) ) for row in range(height) : for col in range(width) : r, g, b = image.getPixel(row, col) avgIntensity = (r + g + b)//3 newIntensity = eqHist[avgIntensity] newImage.setPixel(row, col, newIntensity, newIntensity, newIntensity) return newImage
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 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 rotateImage(image, theta): width = image.width() height = image.height() newWidth = int((image.width()**2 + image.height()**2)**.5) newHeight =int((image.width()**2 + image.height()**2)**.5) newImage1 = GraphicsImage(newWidth, newHeight) newImage2 = GraphicsImage(newWidth, newHeight) angle = math.radians(theta) hEdgeDiff = newHeight - height wEdgeDiff = newWidth - width h = (newWidth // 2) k = (newHeight // 2) print(height, width, newHeight, newWidth) for row in range(height): for col in range(width): r,g,b = image.getPixel(row, col) newImage1.setPixel(row + hEdgeDiff//2, col + wEdgeDiff//2, r, g, b) for row in range(newHeight): for col in range(newWidth): x = col - h y = row - k x1 = int(x*math.cos(angle) + y*math.sin(angle)) y1 = int(y*math.cos(angle) - x*math.sin(angle)) x = x1 + h y = y1 + k if (x >= 0 and y >= 0) and (x < newWidth and y < newHeight): r,g,b = newImage1.getPixel(y,x) else: r,g,b = 0,0, 0 newImage2.setPixel(row, col, r, g, b) return newImage2
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
## # This program provides a simple image viewer that loads an image from # a file displays it in a window. # from ezgraphics import GraphicsImage, ImageWindow filename = input("Enter the name of the image file: ") # Load the image from the file. image = GraphicsImage(filename) # Display it in a window and wait for the user to close the window. win = ImageWindow() win.display(image) win.wait()
from ezgraphics import GraphicsWindow, GraphicsImage GAP = 10 NUM_PICTURES = 20 MAX_WIDTH = 720 win = GraphicsWindow(MAX_WIDTH, 700) canvas = win.canvas() pic = GraphicsImage("picture1.gif") canvas.drawImage(0, 0, pic) pic2 = GraphicsImage("picture2.gif") x = pic.width() + GAP canvas.drawImage(x, 0, pic2) pic3 = GraphicsImage("picture3.gif") x = x + pic2.width() + GAP canvas.drawImage(x, 0, pic3) win.wait()
''' Tianqi Yang Lab3 ''' from ezgraphics import GraphicsImage, GraphicsWindow from math import sqrt filename = "jellyfish.gif" #load the orignal 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) #locate the center of the new image if width > height: radius = height // 2 else: 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:
#@Author Xia Hua #@Assigment 2 Due Oct 8 from ezgraphics import GraphicsWindow from ezgraphics import GraphicsImage from math import sqrt import os ''' Use the os dir in order to save files at same folder ''' __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) ''' Constructors ''' picture = GraphicsImage(os.path.join( __location__, 'queen-mary.gif')) #Change if you want another pic #check size if (picture.height() > picture.width()): WIDTH = picture.height() HEIGHT = picture.width() 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 '''
from ezgraphics import GraphicsWindow, GraphicsImage GAP = 10 NUM_PICTURES = 20 MAX_WIDTH = 720 win = GraphicsWindow(MAX_WIDTH, 700) canvas = win.canvas() pic = GraphicsImage("picture1.gif") canvas.drawImage(0, 0, pic) win.wait()
## # This program processes a digital image by creating a negative of # the original. # from ezgraphics import GraphicsImage, GraphicsWindow filename = input("Enter the name of the image file: ") # 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.
total = 0 print(total) # 4. Textbook R4.30, R. # Given the flipImage.py program of Section 4.10 # change the code to flip the image horizontally instead of vertically # This program creates a new flipped version of a GIF image. # from ezgraphics import GraphicsImage, GraphicsWindow filename = "queen-mary.gif" # 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
## # This program arranges a collection of pictures into rows by lining # them up along the top edges and separating them with small gaps. # from ezgraphics import GraphicsImage, GraphicsWindow GAP = 10 NUM_PICTURES = 20 MAX_WIDTH = 720 win = GraphicsWindow(MAX_WIDTH, 700) canvas = win.canvas() pic = GraphicsImage("picture1.gif") canvas.drawImage(0, 0, pic) x = 0 y = 0 maxY = 0 for i in range(2, NUM_PICTURES + 1) : maxY = max(maxY, pic.height()) previous = pic filename = "picture%d.gif" % i pic = GraphicsImage(filename) x = x + previous.width() + GAP if x + pic.width() < MAX_WIDTH : canvas.drawImage(x, y, pic) else : x = 0 y = y + maxY + GAP
from ezgraphics import GraphicsWindow, GraphicsImage GAP = 10 NUM_PICTURES = 20 MAX_WIDTH = 720 win = GraphicsWindow(MAX_WIDTH, 700) canvas = win.canvas() pic = GraphicsImage("picture1.gif") canvas.drawImage(0, 0, pic) pic2 = GraphicsImage("picture2.gif") x = pic.width() canvas.drawImage(x, 0, pic2) win.wait()
from ezgraphics import GraphicsWindow, GraphicsImage GAP = 10 NUM_PICTURES = 20 MAX_WIDTH = 720 win = GraphicsWindow(MAX_WIDTH, 700) canvas = win.canvas() pic = GraphicsImage("picture1.gif") canvas.drawImage(0, 0, pic) x = 0 for i in range(2, NUM_PICTURES + 1): previous = pic filename = "picture%d.gif" % i pic = GraphicsImage(filename) x = x + previous.width() + GAP canvas.drawImage(x, 0, pic) win.wait()
## # This program will process an image and add color filter to establish a "sunset effect" by increase the level of red by 30% # from ezgraphics import GraphicsWindow, GraphicsImage # input image file filename = "input image/Golden Bridge.gif" #input directory origImage = GraphicsImage(filename) win = GraphicsWindow() 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)