我的目标是使用 Floyd-Steinberg 方法在 JES/Jython 中抖动图像。这是我目前所拥有的:
def Dither_RGB (Canvas):
for Y in range(getHeight(Canvas)):
for X in range(getWidth(Canvas)):
P = getColor(Canvas,X,Y)
E = getColor(Canvas,X+1,Y)
SW = getColor(Canvas,X-1,Y+1)
S = getColor(Canvas,X,Y+1)
SE = getColor(Canvas,X+1,Y+1)
return
上述代码的目标是扫描图像的像素并处理 Floyd-Steinberg 所需的相邻像素。
我无法理解的是如何计算和分配旧像素和新像素之间的 R、G、B 差异。
任何能为我指明正确方向的信息都将不胜感激。
最佳答案
我对您尝试实现的方法一无所知,但对于其余部分:假设 Canvas
类型为 Picture
,你不能那样直接得到颜色。像素的颜色可以从 Pixel
类型的变量中获得。 :
示例:这是从图像中获取每个像素的颜色并将它们分配到新图片中完全相同的位置的过程:
def copy(old_picture):
# Create a picture to be returned, of the exact same size than the source one
new_picture = makeEmptyPicture(old_picture.getWidth(), old_picture.getHeight())
# Process copy pixel by pixel
for x in xrange(old_picture.getWidth()):
for y in xrange(old_picture.getHeight()):
# Get the source pixel at (x,y)
old_pixel = getPixel(old_picture, x, y)
# Get the pixel at (x,y) from the resulting new picture
# which remains blank until you assign it a color
new_pixel = getPixel(new_picture, x, y)
# Grab the color of the previously selected source pixel
# and assign it to the resulting new picture
setColor(new_pixel, getColor(old_pixel))
return new_picture
file = pickAFile()
old_pic = makePicture(file)
new_pic = copy(old_pic)
注意:上面的示例仅适用于您希望在不修改旧图片的情况下处理新图片的情况。如果您的算法需要在执行算法时即时修改旧图片,则最后的 setColor
会直接应用于原始像素(不需要新图片,return
语句也不需要)。
从这里开始,您可以通过操纵像素的 RGB 值来计算任何您想要的东西(使用应用于 setRed()
的 setGreen()
、 setBlue()
和 Pixel
函数,或 col = makeColor(red_val, green_val, blue_val)
并使用 setColor(a_pixel, col)
将返回的颜色应用于像素。
RGB 操作示例 here .
关于python - JES/Jython 中的抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712454/