Friday 15 August 2014

python - Saving changes to newPic when copying an image in jython -


There are similar questions on stack overflow, but I am not doing wrong in my code. Copy copy (file): file = pickAFile () oldPic = makePicture (file) newPic = makeEmptyPicture (getWidth (oldPic), getHeight (oldPic)) for category y (0, getHeight OldPic): in the x category (0, getWidth (oldPic): old pixel = matching pixels (old p, x, y) color = milling (old pixel) newPixel = getPixel (novice, x, y) set collar (new Pixel, Color) (NewPic)

When I use the search (newPic) or show (newPic) function outside I do, then gives a blank white canvas.

Is this because newPic has not been saved? How can I save changes in the new PIC?

This area variable:

when you Define the function (here copyPic () ) All the variables created inside this function are "visible" only by the function Global program does not know anything about its existence

Python interpreter code reads in sequence (sequentially). As the newPic is defined in the first position in the function, it is its and it is deleted after the function is finished. This is the reason why you can not mention newPic after this. You'll need to return the variable (keyword return ) to fix this, so you can call it copyPic () function while calling the main program. P> You must do the following:

  def copyPic (): file = pickAFile () oldPic = makePicture (file) newPic = makeEmptyPicture (getWidth ( Y category (0, getHeight): range for x (0, gatewidth (old pic)): old pixel = matching pixels (old, x, y) color = milling (for oldPic), getHeight (oldPic) (Old pixel) newPixel = getPixel (newPic, y, x) SetColor (newPixel, color) returns newPic # Here is the important thing newPic = copyPic () # We allocate the return value here to a new variable # Ray "related. Exposure (nepic)   

Note: Here, I have used 2 variables equally, which evenly call newPic Are there. Geothon is interpreted as the two different variables by the interpreter:

  • First comes under a function (function scope)
  • The second one is related to the main program (Also called global scope)

    Thus the above code is equivalent to this one:

      def copyPic (): File = PickAFile () ... Return newPic newPic_2 = copyPic () # Here we return "newPic" by another # variable function to "newPic_2" (which is # a "global variable") Es. Explore (newPic_2)   



    Edit:

    Using one of the options, global keywords to do all this, will tell the interpreter that newPic can be found from the global faculty:

     < Code> newPic = None # First declare "Nuperk" at the world Def copyPic (): Tell the function to refer to Global Nupique # Global Variable. ... # Return Nuphak # No need to return anything; Function # Modifies the correct variable CopyPic () Explore (nuepic)   

    Note that mango Speaking on the contrary, try to avoid using a global variable is always better designs, especially with Python, which is clearly object-oriented ...


    I hope that I have made myself clear, if not, then the obvious Do not hesitate to ask hereof.

No comments:

Post a Comment