Jython将图片转换为灰度,然后取反 [英] Jython convert picture to grayscale and then negate it
问题描述
请多多包涵,几周前我才开始使用python.
Please bear with me, I've only started python a few weeks ago.
我正在使用JES.
我已经实现了将图片转换为灰度的功能.我为每种颜色r和r1,g和g1,b和b1创建了两个名称.其背后的想法是将原始值保留在内存中,以便图片可以恢复为原始颜色.
I have made a function to convert a picture to grayscale. I created two names for each color r and r1, g and g1, b and b1. The idea behind this, was to keep the original values in memory, so the picture could be restored to it's original color.
def grayScale(pic):
for p in getPixels(pic):
r = int(getRed(p))
g = int(getGreen(p))
b = int(getBlue(p))//I have tried this with and without the int()
r1=r
g1=g
b1=b
new = (r + g + b)/3
color= makeColor(new,new,new)
setColor(p, color)
def restoreColor(pic):
for p in getPixels(pic):
setColor (p, makeColor(r1,g1,b1))
它不起作用. The error: "local or global name could not be found."
我了解为什么会收到此错误.
I understand why I am getting this error.
但是,如果我尝试在restoreColor中定义它们,它将给出灰度值.
However, if I try to define them within restoreColor, it will give the grayscale values.
我了解为什么会收到此错误,但不知道如何格式化代码以保存名称值.我研究了有关局部和全局变量/名称的问题;但我无法在所学的基本语法范围内解决该问题.
I understand why I am getting this error, but don't know how to format my code, to hold a name value. I have looked at questions about local and global variables/names; but I cannot work out, within the rudimentary syntax I have learnt, how to do this.
问题是:
如何为原始名称(红色,绿色,蓝色)创建名称并获取其值,然后在以后的其他功能中使用它们?我尝试过的所有操作都返回了更改后的(灰度)值. thnx
How to I create names and get their values for the original (red, green, blue) that I can then use later in another function? Everything I have tried, has returned the altered (grayscale) values. thnx
推荐答案
只需添加一种艺术性"的观点:
Just to add an "artistic" point of view:
您在程序中使用的是(r + g + b)/3 ,但是还有其他算法:
You are using (r + g + b) / 3 in your program, but there is other algorithms:
1) lightness method
平均最突出和最不突出的颜色:
1) The lightness method
averages the most prominent and least prominent colors:
(max(R, G, B) + min(R, G, B)) / 2
2) average method
(您的)只是对这些值求平均值:
2) The average method
(yours) simply averages the values:
(R + G + B) / 3
3) luminosity method
是平均值方法的更复杂的版本.它还对这些值取平均值,但形成一个加权平均值以说明人的感知.我们对绿色的敏感度高于其他颜色,因此绿色的权重最大.发光度的公式为:
3) The luminosity method
is a more sophisticated version of the average method. It also averages the values, but it forms a weighted average to account for human perception. We’re more sensitive to green than other colors, so green is weighted most heavily. The formula for luminosity is:
0.21 R + 0.71 G + 0.07 B
这可以带来很大的不同(亮度之间的差异要大得多):
This can make a big difference (luminosity is way far more contrasted):
original | average | luminosity
....... .......... ........ ............ ....... ........
....................................................
代码:
Code :
px = getPixels(pic)
level = int(0.21 * getRed(px) + 0.71 * getGreen(px) + 0.07 * getBlue(px))
color = makeColor(level, level, level)
要求反/求反,只需执行以下操作:
And to negate / invert, simply do:
level = 255 - level
给予:
def greyScaleAndNegate(pic):
for px in getPixels(pic):
level = 255 - int(0.21*getRed(px) + 0.71*getGreen(px) +0.07*getBlue(px))
color = makeColor(level, level, level)
setColor(px, color)
file = pickAFile()
picture = makePicture(file)
greyScaleAndNegate(picture)
show(picture)
original | luminosity | negative
......... ......... .............. ...... ................... . .....
...................................................................
这篇关于Jython将图片转换为灰度,然后取反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!