#---------------------------------------------------------------------- # Fleshed-out command processor for image processing. # # (C) Copyright March 19, 2018 -- Dr. William T. Verts # # Old functions have been removed. #---------------------------------------------------------------------- import string #---------------------------------------------------------------------- # Here most of the functions do something to a pixel, but that is it. # The double-loop, getPixel, and repaint have been factored out into # the Process function, which in order to work must be passed as a # parameter (FN) the actual pixel function to be applied. #---------------------------------------------------------------------- def Brighten_Pixel(PX): setRed (PX, getRed (PX)+20) setGreen(PX, getGreen(PX)+20) setBlue (PX, getBlue (PX)+20) return def Darken_Pixel(PX): setRed (PX, getRed (PX)-20) setGreen(PX, getGreen(PX)-20) setBlue (PX, getBlue (PX)-20) return def Monochrome_Pixel(PX): Average = (getRed(PX) + getGreen(PX) + getBlue(PX)) / 3 if Average > 128: setColor(PX,white) else: setColor(PX,black) return def Negate_Pixel(PX): setRed (PX, 255 - getRed(PX)) setGreen(PX, 255 - getGreen(PX)) setBlue (PX, 255 - getBlue(PX)) return def Gray_Pixel(PX): Average = (getRed(PX) + getGreen(PX) + getBlue(PX)) / 3 setRed (PX,Average) setGreen(PX,Average) setBlue (PX,Average) return def Process (Canvas, FN): for Y in range(getHeight(Canvas)): for X in range(getWidth(Canvas)): FN(getPixel(Canvas,X,Y)) repaint(Canvas) return #---------------------------------------------------------------------- # Functions that have to be stand-alone, and cannot be used with # Process. Flip and Mirror have to deal with pairs of pixels at # a time. # # Question: In Mirror, what happens to the middle pixel of each row # when the canvas is an odd number of pixels wide? # Is it ignored, or is it swapped with itself? # # Question: Can we factor out the code to swap pixels? It'll be the # same in both Flip and Mirror. #---------------------------------------------------------------------- def Mirror (Canvas): for Y in range(getHeight(Canvas)): for X in range(getWidth(Canvas)/2): PX1 = getPixel(Canvas, X, Y) PX2 = getPixel(Canvas, getWidth(Canvas) - 1 - X, Y) Color1 = getColor(PX1) Color2 = getColor(PX2) setColor(PX1, Color2) setColor(PX2, Color1) repaint(Canvas) return def Flip (Canvas): # Currently a stub. return #---------------------------------------------------------------------- # The updated command processor. Notice that GrayScale still uses # the explicit function, and hasn't yet been updated to the new # style using Process. All the other image commands call Process, # but must tell Process which pixel function to use as parameter FN. #---------------------------------------------------------------------- def Main(): Message = """Enter a Command from the list: QUIT OPEN SAVE BRIGHTEN DARKEN MONO GRAY NEGATE MIRROR FLIP FOCUS EMBOSS CONTRAST Enter a command --- """ Canvas = makeEmptyPicture(100,100) MoreToDo = True while (MoreToDo): repaint(Canvas) S = string.upper(requestString(Message)) print S if (S == "QUIT" ): MoreToDo = False elif (S == "OPEN" ): Filename = pickAFile() Canvas = makePicture(Filename) elif (S == "SAVE" ): pass elif (S == "BRIGHTEN"): Process(Canvas, Brighten_Pixel) elif (S == "DARKEN" ): Process(Canvas, Darken_Pixel) elif (S == "MONO" ): Process(Canvas, Monochrome_Pixel) elif (S == "NEGATE" ): Process(Canvas, Negate_Pixel) elif (S == "GRAY" ): Process(Canvas, Gray_Pixel) elif (S == "MIRROR" ): Mirror(Canvas) elif (S == "FLIP" ): Flip(Canvas) else: showError("ERROR, " + S + " is not a legal command") return