My goal is to dither an image in JES/Jython using the Floyd-Steinberg method. Here is what I have so far:
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
The goal of the above code is to scan through the image's pixels and process the neighboring pixels needed for Floyd-Steinberg.
What I'm having trouble understanding is how to go about calculating and distributing the differences in R,G,B between the old pixel and the new pixel.
Anything that could point me in the right direction would be greatly appreciated.
I don't know anything about the method you are trying to implement, but for the rest: Assuming
Canvas
is of typePicture
, you can't get directly the color that way. The color of a pixel can be obtained from a variable of typePixel
:Example: Here is the procedure to get the color of each pixels from an image and assign them at the exact same position in a new picture:
Note: The example above applies only if you want to work on a new picture without modifying the old one. If your algorithm requires to modify the old picture on the fly while performing the algorithm, the final
setColor
would have been applied directly to the original pixel (no need for a new picture, neither thereturn
statement).Starting from here, you can compute anything you want by manipulating the RGB values of a pixel (using
setRed()
,setGreen()
andsetBlue()
functions applied to aPixel
, orcol = makeColor(red_val, green_val, blue_val)
and apply the returned color to a pixel usingsetColor(a_pixel, col)
).Example of RGB manipulations here.
Some others here and especially here.