在jython中复制图像时,将更改保存到newPic [英] Saving changes to newPic when copying an image in jython

查看:152
本文介绍了在jython中复制图像时,将更改保存到newPic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack Overflow上有类似的问题,但我在我的代码中找不到我做错了。

There are similar questions on Stack Overflow, but I cannot find what I am doing wrong in my code.

def copyPic():
  file=pickAFile()
  oldPic=makePicture(file)
  newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
  for y in range(0,getHeight(oldPic)):
    for x in range(0,getWidth(oldPic)):
      oldPixel=getPixel(oldPic,x,y)
      colour=getColor(oldPixel)
      newPixel=getPixel(newPic,x,y)
      setColor(newPixel,colour)
explore(newPic)

当我使用 explore(newPic) show(newPic)函数,给出一个空白的白色画布。

When I use explore(newPic) or show(newPic) outside of the function, gives a blank white canvas.

这是因为newPic不保存?如何保存对newPic的更改?

Is this because the newPic is not saved? How do I 'save' the changes to newPic?

推荐答案

code>的变量:

当你定义一个函数(这里 copyPic()在此函数内创建的变量仅由函数可见。全球计划不知道它的存在。

When you define a function (here copyPic()) all the variables created inside this function are "visible" by the function only. The global program does not know anything about its existence.

Python解释器按照它编写的顺序(顺序)读取代码。
As newPic 首先定义在函数中,它属于它,并在函数终止后被销毁。
这就是为什么你以后不能引用 newPic
要解决这个问题,您必须返回变量(关键字 return ),以便在调用 copyPic时可以从主程序中获取()函数。

The Python interpreter reads the code in the order it is written (sequentially). As newPic is defined in the function in the first place, it belong to it and is destroyed once the function terminates. That is why you can't refer to newPic afterward. To fix this you have to return the variable (keyword return) such that you can get it from the main program when calling the copyPic() function.

您必须执行以下操作:

def copyPic():
  file=pickAFile()
  oldPic=makePicture(file)
  newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
  for y in range(0,getHeight(oldPic)):
    for x in range(0,getWidth(oldPic)):
      oldPixel=getPixel(oldPic,x,y)
      colour=getColor(oldPixel)
      newPixel=getPixel(newPic,y,x)
      setColor(newPixel,colour)

  return newPic      # HERE IS THE IMPORTANT THING

newPic = copyPic()   # Here we assign the returned value to a new variable
                     # which belong to the "global scope".
explore(newPic)

注意: 2个变量等同地调用 newPic 。这些被Jython解释器视为两个不同的变量:

Note : Here, I used 2 variables called identically newPic. Those are considered as two different variables by the Jython Interpreter :


  • 第一个属于函数(函数范围)

  • 第二个属于主程序(也称为全局作用域)

  • The first one belongs to the function (function scope)
  • The second one belongs to the main program (also called global scope)

因此,上面的代码完全等价于下面的代码:

Thus the code above is exactly equivalent to this one :

def copyPic():
  file=pickAFile()
  ...
  return newPic    

newPic_2 = copyPic()    # Here we store / assign the "newPic" returned by the 
                        # copy function into another variable "newPic_2" (which
                        # is a "global variable").
explore(newPic_2)


编辑:

EDIT :

所有这一切都将使用全局关键字告诉解释器 newPic 是从全局范围:

An alternative to all of this would have been to use the global keyword to tell the interpreter that newPic was to be found from the global scope :

newPic = None           # First declare "newPic" globally.

def copyPic():
  global newPic         # Tell the function to refer to the global variable.
  ...
  #return newPic        # No need to return anything, the function is 
                        # modifying the right variable.

copyPic()
explore(newPic)

>注意一般来说,一个尝试避免使用全局变量。总是有更好的设计,特别是用Python面向对象...

明白了。如果没有,请不要犹豫要求进一步解释。

I hope I made myself clear. If not, do not hesitate to ask further explanations.

这篇关于在jython中复制图像时,将更改保存到newPic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆