Question

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.

No correct solution

OTHER TIPS

I don't know anything about the method you are trying to implement, but for the rest: Assuming Canvas is of type Picture, you can't get directly the color that way. The color of a pixel can be obtained from a variable of type Pixel:

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:

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)

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 the return statement).

Starting from here, you can compute anything you want by manipulating the RGB values of a pixel (using setRed(), setGreen() and setBlue() functions applied to a Pixel, or col = makeColor(red_val, green_val, blue_val) and apply the returned color to a pixel using setColor(a_pixel, col)).


Example of RGB manipulations here.

Some others here and especially here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top