def convertFile(self): try: if self._pictureThere: # Converts the argument image to black and white from images import Image image = Image(self._textField.getText()) blackPixel = (0, 0, 0) whitePixel = (255, 255, 255) for y in range(image.getHeight()): for x in range(image.getWidth()): (r, g, b) = image.getPixel(x, y) average = (r + g + b) / 3 if average < 128: image.setPixel(x, y, blackPixel) else: image.setPixel(x, y, whitePixel) image.draw() else: print("uhhhhhhhh no image there") except Exception: self.messageBox( title="ERROR", message= "THERE IS NO FILE INPUTTED. \n PLEASE ENTER A FILE NAME.")
def enlarge(image, factor): """Builds and returns a copy of the image which is larger by the factor.""" oldWidth = image.getWidth() oldHeight = image.getHeight() new = Image(oldWidth * factor, oldHeight * factor) oldY = 0 newY = 0 while oldY < oldHeight: for countY in range(factor): oldLeft = 0 oldRight = oldWidth - 1 newLeft = 0 newRight = new.getWidth() - 1 while oldLeft < oldRight: leftPixel = image.getPixel(oldLeft, oldY) rightPixel = image.getPixel(oldRight, oldY) for count in range(factor): new.setPixel(newLeft, newY, leftPixel) new.setPixel(newRight, newY, rightPixel) newLeft += 1 newRight -= 1 oldLeft += 1 oldRight -= 1 newY += 1 oldY += 1 return new
def rotateRight(image): """Rotates the image 90 degrees """ newImage = Image(image.getHeight(), image.getWidth()) for y in range(image.getHeight()): for x in range(image.getWidth()): (r, g, b) = image.getPixel(x, y) newImage.setPixel(image.getHeight() - y - 1, x, (r, g, b)) return newImage
def blackandWhite(Image): blackPixel = (0, 0, 0) whitePixel = (255, 255, 255) # for y in range(Image.getHeight()): for x in range(Image.getWidth()): (r, g, b) = Image.getPixel(x, y) average = (r + g + b) // 3 # if average < 128: Image.setPixel(x, y, blackPixel) else: Image.setPixel(x, y, whitePixel)
def sepia(): picture = Image("smokey.gif") for y in range(picture.getHeight()): for x in range(picture.getWidth()): (red,green,blue) = picture.getPixel(x, y) if red < 63: red = int(red * 1.1) blue = int(blue * 0.9) elif red < 192 red = int(red * 1.15) blue = int(blue * 0.85) else: red = min(int(red * 1.08), 255) blue = int(blue * 0.93) picture.setPixel(x,y,(red,green,blue))
def sepia(): picture = Image("smokey.gif") for y in range(picture.getHeight()): for x in range(picture.getWidth()): (r, g, b) = picture.getPixel(x, y) newR = (0.393 * r + 0.769 * g + 0.189 * b) newG = (0.349 * r + 0.686 * g + 0.168 * b) newB = (0.272 * r + 0.534 * g + 0.131 * b) if newR > 255: newR = 255 if newG > 255: newG = 255 if newB > 255: newB = 255 picture.setPixel(x, y, (newR, newG, newB)) picture.draw()
def shrink(image, factor): """Builds and returns a new image which is smaller copy of the argument image, by the factor argument.""" width = image.getWidth() height = image.getHeight() new = Image(width / factor, height / factor) oldY = 0 newY = 0 while oldY < height - factor: oldX = 0 newX = 0 while oldX < width - factor: oldP = image.getPixel(oldX, oldY) new.setPixel(newX, newY, oldP) oldX += factor newX += 1 oldY += factor newY += 1 return new
def enlarge(image, factor): """Builds and returns a copy of the image which is larger by the factor.""" width = image.getWidth() height = image.getHeight() new_image = Image(width * factor, height * factor) for y in range(height): for x in range(width): color = image.getPixel(x, y) new_image.setPixel(factor * x, factor * y, color) new_image.setPixel(factor * x + 1, factor * y, color) new_image.setPixel(factor * x, factor * y + 1, color) new_image.setPixel(factor * x + 1, factor * y + 1, color) return new_image
from images import Image image = Image("smokey.gif") image.draw() print(image.getWidth()) print(image.getHeight()) print(image) print(image.getPixel(0, 0)) image = Image(150, 150) image.draw() blue = (0, 0, 255) y = image.getHeight() // 2 for x in range(image.getWidth()): image.setPixel(x, y - 1, blue) image.setPixel(x, y, blue) image.setPixel(x, y + 1, blue) image.draw() image.save("horizontal.gif")
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:30:28) [MSC v.1926 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> from images import Image >>> image = Image("smokey.gif") >>> (r, g, b) = image.getPixel(0, 0) >>> r 194 >>> g 221 >>> b 114 >>> image.setPixel(0, 0, (r + 10, g + 10, b + 10)) >>> def average(triple): (a, b, c) = triple return(a + b + c) // 3 >>> average((40, 50, 60)) 50 >>>