def erosion(canvas): ''' #TODO: Write what function does. ''' covertGray(canvas) canvas.data.colourPopToHappen = False canvas.data.cropPopToHappen = False canvas.data.drawOn = False if canvas.data.image != None: print(type(canvas.data.image)) # Taking a matrix of size 5 as the kernel image_data = np.asarray(canvas.data.image) kernel = np.ones((5,5), np.uint8) # The first parameter is the original image, # kernel is the matrix with which image is # convolved and third parameter is the number # of iterations, which will determine how much # you want to erode/dilate a given image. dilated_image = cv2.erode(image_data, kernel, iterations=1) print(type(dilated_image)) canvas.data.image = Image.fromarray(dilated_image) print(type(canvas.data.image)) file_op.save(canvas) canvas.data.undoQueue.append(canvas.data.image.copy()) canvas.data.imageForTk=draw.makeImageForTk(canvas) draw.drawImage(canvas)
def flip(canvas): canvas.data.colourPopToHappen = False canvas.data.cropPopToHappen = False canvas.data.drawOn = False if canvas.data.image != None: canvas.data.image = ImageOps.flip(canvas.data.image) file_op.save(canvas) canvas.data.undoQueue.append(canvas.data.image.copy()) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas)
def reset(canvas): canvas.data.colourPopToHappen = False canvas.data.cropPopToHappen = False canvas.data.drawOn = False ### change back to original image if canvas.data.image != None: canvas.data.image = canvas.data.originalImage.copy() file_op.save(canvas) canvas.data.undoQueue.append(canvas.data.image.copy()) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas)
def redo(canvas): if len(canvas.data.redoQueue) > 0: canvas.data.image = canvas.data.redoQueue[0] save(canvas) if len(canvas.data.redoQueue) > 0: # we remove this version from the Redo Deque beacuase it # has become our current image lastImage = canvas.data.redoQueue.popleft() canvas.data.undoQueue.append(lastImage) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas)
def performCrop(event, canvas): canvas.data.image=\ canvas.data.image.crop(\ (int(round((canvas.data.startCropX-canvas.data.imageTopX)*canvas.data.imageScale)), int(round((canvas.data.startCropY-canvas.data.imageTopY)*canvas.data.imageScale)), int(round((canvas.data.endCropX-canvas.data.imageTopX)*canvas.data.imageScale)), int(round((canvas.data.endCropY-canvas.data.imageTopY)*canvas.data.imageScale)))) canvas.data.endCrop = False canvas.data.cropPopToHappen = False file_op.save(canvas) canvas.data.undoQueue.append(canvas.data.image.copy()) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas)
def undo(canvas): if len(canvas.data.undoQueue) > 0: # the last element of the Undo Deque is the # current version of the image lastImage = canvas.data.undoQueue.pop() # we would want the current version if wehit redo after undo canvas.data.redoQueue.appendleft(lastImage) if len(canvas.data.undoQueue) > 0: # the previous version of the image canvas.data.image = canvas.data.undoQueue[-1] file_op.save(canvas) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas)
def rotateFinished(canvas, rotateWindow, rotateSlider, previousAngle): if canvas.data.rotateWindowClose == True: rotateWindow.destroy() canvas.data.rotateWindowClose = False else: if canvas.data.image != None and rotateWindow.winfo_exists(): canvas.data.angleSelected = rotateSlider.get() if canvas.data.angleSelected!= None and \ canvas.data.angleSelected!= previousAngle: canvas.data.image=\ canvas.data.image.rotate(float(canvas.data.angleSelected)) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas) canvas.after(200, lambda:rotateFinished(canvas,\ rotateWindow, rotateSlider, canvas.data.angleSelected) )
def covertGray(canvas): canvas.data.colourPopToHappen=False canvas.data.cropPopToHappen=False canvas.data.drawOn=False #### The existing method to convert to a grayscale image converts the #### #### image mode, so I used my own function to convert #### # value of each channel of a pixel is set to the average of the original # values of the channels if canvas.data.image!=None: data=[] for col in range(canvas.data.image.size[1]): for row in range(canvas.data.image.size[0]): r, g, b= canvas.data.image.getpixel((row, col)) avg= int(round((r + g + b)/3.0)) R, G, B= avg, avg, avg data.append((R, G, B)) canvas.data.image.putdata(data) file_op.save(canvas) canvas.data.undoQueue.append(canvas.data.image.copy()) canvas.data.imageForTk=draw.makeImageForTk(canvas) draw.drawImage(canvas)
def newImage(canvas): imageName = askopenfilename() filetype = "" #make sure it's an image file try: filetype = imghdr.what(imageName) except: messagebox.showinfo(title="Image File",\ message="Choose an Image File!" , parent=canvas.data.mainWindow) # restrict filetypes to .jpg, .bmp, etc. if filetype in ['jpeg', 'bmp', 'png', 'tiff']: canvas.data.imageLocation = imageName im = Image.open(imageName) canvas.data.image = im canvas.data.originalImage = im.copy() canvas.data.undoQueue.append(im.copy()) canvas.data.imageSize = im.size #Original Image dimensions canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas) else: messagebox.showinfo(title="Image File",\ message="Choose an Image File!" , parent=canvas.data.mainWindow)
def transpose(canvas): canvas.data.colourPopToHappen = False canvas.data.cropPopToHappen = False canvas.data.drawOn = False # I treated the image as a continuous list of pixel values row-wise # and simply excnaged the rows and the coloums # in oder to make it rotate clockewise, I reversed all the rows if canvas.data.image != None: imageData = list(canvas.data.image.getdata()) newData = [] newimg=Image.new(canvas.data.image.mode,\ (canvas.data.image.size[1], canvas.data.image.size[0])) for i in range(canvas.data.image.size[0]): addrow = [] for j in range(i, len(imageData), canvas.data.image.size[0]): addrow.append(imageData[j]) addrow.reverse() newData += addrow newimg.putdata(newData) canvas.data.image = newimg.copy() file_op.save(canvas) canvas.data.undoQueue.append(canvas.data.image.copy()) canvas.data.imageForTk = draw.makeImageForTk(canvas) draw.drawImage(canvas)